First:Dao层实现
//添加单个用户
void addUser(User user);
//删除用户
void delUserById(int id);
Second:Mapper.xml实现
Tips:在里添加下面代码
<!--添加单个用户-->
<insert id="addUser" parameterType="com.code.bean.User">
insert into user(id,username,password) values(#{id},#{username},#{password})
</insert>
<!--删除用户-->
<delete id="delUserById" parameterType="Integer">
DELETE from user where id = #{id}
</delete>
Third:Service层实现
接口(同dao层):
void addUser(User user);
void delUserById(int id);
实现方法:
@Override
public void addUser(User user) {
userDao.addUser(user);
}
@Override
public void delUserById(int id) {
userDao.delUserById(id);
}
Fourth:Controller层实现
@RequestMapping("/addUser.do")
public String addUser(User user){
userService.addUser(user);
System.out.println("添加成功");
return "redirect:findAll.do";
}
@RequestMapping("/delUserById")
public String delUserById(int id){
userService.delUserById(id);
System.out.println("删除成功");
return "redirect:findAll.do";
}
Fifth:修改jsp页面参数
<a href="${pageContext.request.contextPath}/user/delUserById.do?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
<form action="${pageContext.request.contextPath}/user/addUser.do"
method="post">
。。。省略代码。。。
</form>