First:dao层实现
//查询修改页面
User updateCheckedById(int id);
//修改
void updateUser(User user);
Second:mapper.xml实现
<select id="updateCheckedById" parameterType="Integer" resultType="com.code.bean.User">
SELECT * from user WHERE id = #{id}
</select>
<update id="updateUser" parameterType="com.code.bean.User">
update user set username = #{username},password = #{password} where id = #{id}
</update>
Third:Service层实现
接口(同dao层):
//查询修改页面
User updateCheckedById(int id);
//修改
void updateUser(User user);
实现方法:
@Override
public User updateCheckedById(int id) {
return userDao.updateCheckedById(id);
}
@Override
public void updateUser(User user) {
userDao.updateUser(user);
}
Fourth:Controller层的实现
@RequestMapping("/updateCheckedById.do")
public ModelAndView updateCheckedById(int id){
User user = userService.updateCheckedById(id);
ModelAndView mv = new ModelAndView();
mv.addObject("user",user);
mv.setViewName("user-update");
return mv;
}
@RequestMapping("/updateUser.do")
public String updateUserById(User user){
userService.updateUser(user);
System.out.println("修改成功");
return "redirect:findAll.do";
}
Fifth:修改jsp页面参数
user-list:
<a href="${pageContext.request.contextPath}/user/updateCheckedById.do?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
user-update:
<form action="${pageContext.request.contextPath}/user/updateUser.do"
method="post">
。。。省略代码。。。
</form>