Bootstrap

ssm-4修改

一、思路
点击update,跳转至controller的/update根据id查数据,将数据传给update.html,在此页面修改完成后,提交表单至controller的/save然后根据id修改数据
二、根据id查数据
对应sql:

select * from k_user where ID = #{id}
  • mapper接口
    返回值类型:KUser (有查找结果集 )
    参数列表:int id
public KUser findByUserNo(int id);
  • service接口(同上)
  • service实现类(调用mapper接口的save方法)
@Override
    public KUser findByUserNo(int id) {
        return kUserMapper.findByUserNo(id);
    }
  • controller类(调用service接口的save方法)
@RequestMapping("/update")
    public String findAll(int id, Model model){
        KUser user = kUserService.findByUserNo(id);
        model.addAttribute("user",user);
        return "update";//跳转至update.html
    }

三、根据id修改数据
对应sql:

update k_user set PWD=#{pwd},USERNAME=#{userName},AGE=#{age},SEX=#{sex},DEPTNO=#{deptNo},POSITION=#{position} WHERE ID=#{id}
  • mapper接口
    返回值类型:void (无结果集 )
    参数列表:KUser kUser
public void updateByUserNo(KUser kUser);
  • service接口(使用save接口,这里其实将增和改的service接口合并)
  • service实现类(调用mapper接口的updateByUserNo方法)
@Override
    public void save(KUser user) {
        if(user.getId() != null){//修改会传id
            kUserMapper.updateByUserNo(user);
        }else{//新增没有id
            kUserMapper.save(user);
        }
    }
  • controller类(update.html将表单提交至/save)
@RequestMapping("/save")
    public String save(KUser user){
        kUserService.save(user);
        return "forward:/findAll";
    }

四、前端展示

<a th:href="@{'/update?id='+${u.id}}">update</a>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>修改账户信息表单</h1>
<form th:action="@{/save}" method="post">
    用户id:<input name="id" th:value="${user.id}" readonly>
    用户编号:<input name="userNO" th:value="${user.userNo}"><br>
    用户密码<input name="pwd" th:value="${user.pwd}"><br/>
    用户姓名<input name="userName" th:value="${user.userName}"><br/>
    用户性别<input name="sex" th:value="${user.sex}"><br/>
    用户年龄<input name="age" th:value="${user.age}"><br/>
    用户部门编号<input  name="deptNo" th:value="${user.deptNo}"><br/>
    用户职位<input  name="position" th:value="${user.position}"><br/>
    <input type="submit" value="保存"><br/>
</form>
</body>
</html>
;