欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SSM整合版本一之普通的CRUD

程序员文章站 2022-07-03 19:48:27
项目简介(1)项目的经典模块-CRUD功能(2)什么是CRUDCRUD:Create( 创 建 )Retrieve(查询)Update(更新)Delete(删除)使用的技术栈:ssm:SpringMVC+Spring+MyBatis本篇为V1:V1 : ssmV2 : ssm+AjaxV3 : ssm+Ajax+bootstrapSSM环境搭建参考V2版本测试后台代码是否正确@RunWith(SpringJUnit4ClassRunner.class)@ContextConf...

项目简介

  • (1)项目的经典模块-CRUD功能
  • (2)什么是CRUD
    CRUD:Create( 创 建 )Retrieve(查询)Update(更新)Delete(删除)
    使用的技术栈:ssm:SpringMVC+Spring+MyBatis
  • 本篇为V1:
    V1 : ssm
    V2 : ssm+Ajax
    V3 : ssm+Ajax+bootstrap

SSM环境搭建参考V2版本

  • 测试后台代码是否正确
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestUserService {
    private static final Logger log = Logger.getLogger(TestUserService.class.getName());
    @Autowired
    UserService userService;
    @Test
    public void test01() {
        List<User> list = userService.findAllUser();
        log.info("list"+list);

    }
}
  • User
    private int id;
    private String username;
    private String password;

    public User() {
    }
     @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
  • UserServiceImpl(方法已写)
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;

    @Override
    public void saveUser(User user) {
        userDao.save(user);
    }

    @Override
    public void deleteUser(int id) {
        userDao.deleteById(id);
    }

    @Override
    public int login(User u) {
        return 0;
    }

    @Override
    public List<User> findAllUser() {
        List<User> list = userDao.finAllUser();
        return list;
    }

    @Override
    public User findByUserId(int id) {
        return userDao.findById(id);

    }

    @Override
    public void updateUserById(User user) {
       userDao.update(user);
    }
}
  • UserDao
    List<User> finAllUser();

    void save(User user);

    void deleteById(int id);

    void update(User user);

     User findById(int id);
  • UserDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--        User findByName(String username);-->
<mapper namespace="cn.htl.dao.UserDao">
    <select id="finAllUser" resultType="user">
        select * from user order by id asc;
    </select>
    <insert id="save" parameterType="user">
        insert into user (username,password) values (#{username},#{password}) ;
    </insert>
    <delete id="deleteById" parameterType="int">
        delete from user where id =#{id};
    </delete>
    <update id="update" parameterType="user">
        update user set password = #{password} where id = #{id};
    </update>
    <select id="findById" parameterType="int" resultType="user">
        select * from user where id = #{id};
    </select>
</mapper>

Controller层:代码显示

  • DepartmentController(方法已写)
@Controller
@RequestMapping("/user")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @RequestMapping(path = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        List<User> users = userService.findAllUser();
        log.info("list=" + users);
        model.addAttribute("users", users);
        return "list_user";
    }

    @RequestMapping(path = "/addUser", method = RequestMethod.GET)
    public String AddUser() {
        log.info("AddUser");
        return "add_user";
    }
    @RequestMapping(path = "/addUser", method = RequestMethod.POST)
    public String SaveUser(User user, Model model) {
        if (user.getUsername() != null && !"".equals(user.getUsername())) {
            userService.saveUser(user);
            log.info("AddUser" + user);
            return "redirect:/user/list";
        } else {
            model.addAttribute("error_msg","添加用户不能为空");
            return "forward:/error";
        }
    }

    @RequestMapping(path = "/deleteUser", method = RequestMethod.GET)
    public String DeleteUser(Integer id) {
        log.info("DeleteUser" + id);
        userService.deleteUser(id);
        return "redirect:/user/list";
    }
    @RequestMapping(path = "/updateUser", method = RequestMethod.GET)
    public String Update(Integer id,Model model) {
        log.info("UpdateUser"+id);
        User user = userService.findByUserId(id);
        model.addAttribute("user",user);
        return "update_user";
    }
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    public String UpdateUser(User user) {
        log.info("UpdateUser  user"+user);
        userService.updateUserById(user);

        return "redirect:/user/list";
    }
}

前台页面代码

  • add_user.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/user/addUser">
    <input type="text" name="username"/><br/>
    <input type="text" name="password"/><br/>
    <input type="submit" value="保存"/><br/>
</form>

</body>
</html>
  • list_user.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${users}
<table border="1px" width="100%">
    <tr>
        <td>编号</td>
        <td>用户名</td>
        <td>密码</td>
        <td>操作</td>

    </tr>

    <c:forEach items="${users}" var="user">
        <tr>
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td>${user.password}</td>
            <td><a href="${pageContext.request.contextPath}/user/deleteUser?id=${user.id}">删除</a>
                <a href="${pageContext.request.contextPath}/user/updateUser?id=${user.id}">修改</a></td>
        </tr>
    </c:forEach>
</table>
</body>
</html>
  • update_user.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form method="post" action="${pageContext.request.contextPath}/user/update">
    <input type="hidden" name="id" value="${user.id}" >

    <input type="text"  value="${user.username}" disabled="disabled"/><br/>
    <input type="text"  value="${user.password}" disabled="disabled"/><br/>
    <input type="text" name="password" value="${user.password}"/><br/>
    <input type="submit" value="修改密码"/><br/>
</form>
</body>
</html>
  • form表单 不会将 标记为disabled="disabled"作为参数提交到后台
  • 所以,必须增加一个type="hidden"的input标签

本文地址:https://blog.csdn.net/weixin_45114005/article/details/109259807