在上一篇的基础上,我们扩展一下功能。将增删改的功能加上。
add.jsp
<%--
Created by IntelliJ IDEA.
User: soft
Date: 2019/7/13
Time: 19:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增数据</title>
</head>
<body>
<form action="/insert">
姓名:
<input type="text" name="uname"/>
性别
<input type="text" name="usex"/>
<input type="submit" />
<a href="/select">返回</a>
</form>
</body>
</html>
update.jsp
<%--
Created by IntelliJ IDEA.
User: soft
Date: 2019/7/13
Time: 20:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/updateSubmit">
<input type="hidden" name="uid" value="${myuser.uid}">
姓名:
<input type="text" name="uname" value="${myuser.uname}"/>
性别
<input type="text" name="usex" value="${myuser.usex}"/>
<input type="submit" />
<a href="/select">返回</a>
</form>
</body>
</html>
userList.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: soft
Date: 2019/7/13
Time: 13:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/add">列表</a>
用户信息<br>
<table>
<thead>
<th>id</th>
<th>姓名</th>
<th>性别</th>
<th>操作</th>
</thead>
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.uid} </td>
<td>${user.uname} </td>
<td>${user.usex} <br></td>
<td><a href="/del?uid=${user.uid}">删除</a> <a href="/update?uid=${user.uid}">修改</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
userController
package com.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.mysql.fabric.Response;
import com.po.MapUser;
import com.po.SelectUserParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.dao.UserDao;
import com.po.MyUser;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/select")
public String select(MyUser user, Model model){
List<MyUser> list=userDao.selectUserByUname(user);
model.addAttribute("userList",list);
return "userList";
}
@RequestMapping("/add")
public String add(){
return "add";
}
@RequestMapping("/insert")
public String add(MyUser user, Model model){
int intadd=userDao.addUser(user);
return "add";
}
@RequestMapping("/del")
public String del(MyUser user, Model model){
int intadd=userDao.deleteUser(user.getUid());
return "userList";
}
@RequestMapping("/update")
public String update(MyUser user, Model model){
MyUser myUser=userDao.selectUserById(user.getUid());
model.addAttribute("myuser",myUser);
return "update";
}
@RequestMapping("/updateSubmit")
public String updateSubmit(MyUser user, Model model){
int intadd=userDao.updateUser(user);
return "userList";
}
}
UserDao
package com.dao;
import com.po.MapUser;
import com.po.MyUser;
import com.po.SelectUserParam;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository("userDao")//Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发。@Repository注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean。具体只需将该注解标注在 DAO类上即可。同时,为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用Bean 的自动扫描功能
@Mapper
/*
使用Spring自动扫描MyBatis的接口并装配
*/
public interface UserDao {
/*
接口方法对应SQL映射文件UserMapper.xml中的ID
*/
public MyUser selectUserById(Integer uid);
/*
使用Map的查询方法
*/
/* public List<MyUser> selectAllUser(Map<String,Object> param);*/
/*
直接查询
*/
/*public List<MyUser> selectAllUser();*/
/*
使用JavaBean查询
*/
public List<MyUser> selectAllUser(SelectUserParam param);
public int addUser(MyUser user);
public int updateUser(MyUser user);
public int deleteUser(Integer integer);
public List<MapUser> selectResultMap();
public List<MyUser> selectUserByUname(MyUser user);
}
userService
package com.service;
import java.util.List;
import com.po.MyUser;
public interface UserService {
public List<MyUser> selectUserByUname(MyUser user);
public int addUser(MyUser user);
public int deleteUser(Integer integer);
public int updateUser(MyUser user);
}
userServiceImpl
package com.service;
import java.util.List;
import com.dao.UserDao;
import com.po.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<MyUser> selectUserByUname(MyUser user){
return userDao.selectUserByUname(user);
}
@Override
public int addUser(MyUser user){
return userDao.addUser(user);
}
@Override
public int deleteUser(Integer integer){
return userDao.deleteUser(integer);
}
@Override
public int updateUser(MyUser user){
return userDao.updateUser(user);
}
}