SSM整合

2018-08-10  本文已影响47人  TiredHu

Mybatis介绍

MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。

SSM整合

准备材料:

image.png

单个实体CRUD

首先,建表:tb_user

image.png

利用自动代码生成工具生成代码:

image.png

service层代码

package com.huhong.service;

import com.huhong.model.User;

import java.util.List;

/**
 * Created by Tired on 2018/4/16.
 */
public interface UserService {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

    /**
     * 查询所有对象
     * @return
     */
    List<User> findAll();
}

import com.huhong.dao.UserMapper;
import com.huhong.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Tired on 2018/4/16.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userDao;
    public int deleteByPrimaryKey(Integer id) {
        return userDao.deleteByPrimaryKey(id);
    }

    public int insert(User record) {
        return userDao.insert(record);
    }

    public int insertSelective(User record) {
        return userDao.insertSelective(record);
    }

    public User selectByPrimaryKey(Integer id) {
        return userDao.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(User record) {
        return userDao.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(User record) {
        return userDao.updateByPrimaryKey(record);
    }

    public List<User> findAll() {
        return userDao.findAll();
    }
}

控制层代码
UserController.java

package com.huhong.controller;

import com.huhong.model.User;
import com.huhong.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tired on 2018/4/16.
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/showUserInfo")
    public ModelAndView getUserInfo() {
        ModelAndView mv = findAll();
        return mv;
    }

    private ModelAndView findAll() {
        ModelAndView mv = new ModelAndView("user");
        List<User> users = new ArrayList<User>();
        users = userService.findAll();
        mv.addObject("users", users);
        return mv;

    }

    @RequestMapping("/toAddUser")
    public String toAddUser() {
        return "addUser";
    }

    @RequestMapping("/addUser")
    public ModelAndView addUser(User user) {
        userService.insertSelective(user);
        ModelAndView mv = findAll();
        return mv;
    }
    @RequestMapping("/deleteUser")
    public ModelAndView deleteUser(@RequestParam("userId")  Integer userId)
    {
        //调用接口删除相应对象
        userService.deleteByPrimaryKey(userId);
        //重新查出所有用户,并返回到页面
        ModelAndView mv = findAll();
        return mv;
    }
    @RequestMapping("/toUpdateUser")
    public  ModelAndView toUpdateUser(@RequestParam("userId")  Integer userId)
    {
        User user = userService.selectByPrimaryKey(userId);
        ModelAndView mv = new ModelAndView("updateUser");
        mv.addObject("updateUser",user);
        return mv;
    }

    @RequestMapping("/updateUser")
    public  ModelAndView  updateUser(User user)
    {
        userService.updateByPrimaryKeySelective(user);
        ModelAndView mv = findAll();
        return mv;
    }
}

user.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page isELIgnored="false" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<div style="text-align: center">
    <h2>User Info</h2>
    <h3>用户列表</h3>

    <a href="toAddUser">添加</a><br>
    <table border="1" cellpadding="3" cellspacing="0"
           style="width: 60%;margin: auto">
        <tr>
            <th>用户名</th>
            <th>密码</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
        </tr>
        <c:forEach items="${users}" var="user">
            <tr>
                <td>${user.name}</td>
                <td>${user.password}</td>
                <td>${user.age}</td>
                <td>${user.sex}</td>
                <td><a href="deleteUser?userId=${user.id}">删除</a>
                    <a href="toUpdateUser?userId=${user.id}">修改</a></td>
            </tr>
        </c:forEach>
    </table>
</div>
</body>
</html>

addUser.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<html>
<head>
    <title>Title</title>
</head>
<body>
        <form action="addUser" method="post">
            <div class="">
                <label class="">用户名:</label>
                <div class="">
                    <input type="text" name="name" required   placeholder="请输入姓名"
                           autocomplete="off" >
                </div>
            </div>
            <div class="">
                <label class="">密码:</label>
                <div class="">
                    <input type="password" name="password" required  placeholder="请输入密码"
                           autocomplete="off" >
                </div>
            </div>
            <div class="">
                <label class="">年龄:</label>
                <div class="">
                    <input type="text" name="age" required  placeholder="请输入年龄"
                           autocomplete="off">
                </div>
            </div>
            <div class="">
                <label class="">性别:</label>
                <div class="">
                    <input type="text" name="sex" required   placeholder="请输入性别"
                           autocomplete="off" >
                </div>
            </div>

                <div class="">
                <input type="submit" value="提交">
            </div>
        </form>
</body>
</html>

updateUser.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<%@ page isELIgnored ="false" %>
<html>
<head>
    <title>UpdateUser</title>
</head>
<body>
        <form action="updateUser" method="post">
            <div style="display:none;">
                <label class="">ID:</label>
                <div class="">
                    <input type="text" name="id"
                           value="${updateUser.id}">
                </div>
            </div>
            <div class="">
                <label class="">用户名:</label>
                <div class="">
                    <input type="text" name="name" required   placeholder="请输入姓名"
                           value="${updateUser.name}">
                </div>
            </div>
            <div class="">
                <label class="">密码:</label>
                <div class="">
                    <input type="text" name="password" required  placeholder="请输入密码"
                           value="${updateUser.password}">
                </div>
            </div>
            <div class="">
                <label class="">年龄:</label>
                <div class="">
                    <input type="text" name="age" required  placeholder="请输入年龄"
                           value="${updateUser.age}">
                </div>
            </div>
            <div class="">
                <label class="">性别:</label>
                <div class="">
                    <input type="text" name="sex" required   placeholder="请输入性别"
                           value="${updateUser.sex}">
                </div>
            </div>

            <div class="">
                <div class="">
                <input type="submit" value="修改">
                </div>
            </div>
        </form>
</body>
</html>
<!-- 解决配置文件不拷贝的问题 -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <excludes>
          <exclude>generatorConfig.xml</exclude>
        </excludes>
        <filtering>false</filtering>
      </resource>
    </resources>
上一篇下一篇

猜你喜欢

热点阅读