ssm框架做增删改查
UserController
@Controller
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue="1") int pageNum, @RequestParam(defaultValue="5")int size){
List<User> users=userService.findAll(pageNum, size);
PageInfo<User> pageInfo=new PageInfo<>(users);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("allUser.jsp");
modelAndView.addObject("pageInfo",pageInfo);
return modelAndView;
}
@RequestMapping("/login.do")
public String login(User user){
Boolean flag=userService.login(user.getUsername(),user.getPassword());
if (flag){
return "redirect:/findAll.do";
}else {
return "fail.jsp";
}
}
@RequestMapping("/delete.do")
public String deleteById(int id){
userService.deleteById(id);
return "redirect:/findAll.do";
}
@RequestMapping("/add.do")
public String add(User user){
userService.add(user);
return "redirect:/findAll.do";
}
@RequestMapping("/toupdate.do")
public ModelAndView toupdate(int id){
User user=userService.selectById(id);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("UpdateUser.jsp");
return modelAndView;
}
@RequestMapping("/update.do")
public String update(User user){
userService.update(user);
return "redirect:/findAll.do";
}
}
IUserDao
public interface IUserDao {
List<User> findAll();
User selectByUserName(String name);
void deleteById(int id);
void add(User user);
void update(User user);
User selectById(int id);
}
UserService
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<User> findAll(int pageNum,int size) {
PageHelper.startPage(pageNum, size);
return userDao.findAll();
}
@Override
public Boolean login(String username, String password) {
User user=userDao.selectByUserName(username);
if (user !=null && password.equals(user.getPassword())){
return true;
}
return false;
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public void update(User user) {
userDao.update(user);
}
@Override
public User selectById(int id) {
return userDao.selectById(id);
}
}
IUserService
public interface IUserService {
List<User> findAll(int pageNum,int size);
Boolean login(String username,String password);
void deleteById(int id);
void add(User user);
void update(User user);
User selectById(int id);
}
UserMapper1.xml
<mapper namespace="com.zhongruan.dao.IUserDao">
<select id="findAll" resultType="com.zhongruan.bean.User">
select * from tb_user
</select>
<select id="selectByUserName" resultType="com.zhongruan.bean.User">
select * from tb_user where username=#{username}
</select>
<delete id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</delete>
<insert id="add" parameterType="com.zhongruan.bean.User">
insert into tb_user(username,password) values (#{username},#{password})
</insert>
<update id="update" parameterType="com.zhongruan.bean.User">
update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
<select id="selectById" parameterType="int" resultType="com.zhongruan.bean.User">
select * from tb_user where id=#{id}
</select>
</mapper>
applicationContext.xml
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
addUser
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>新增用户</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body style="background-image: url(images/p2.jpg);background-size: 100%">
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
基于servlet+jsp框架的管理系统:简单实现增、删、改、查。
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>新增用户</small>
</h1>
</div>
</div>
</div>
<form action="/add.do" method="post">
<br><br><br>
用户姓名:<input type="text" name="username"><br><br><br>
用户密码:<input type="text" name="password"><br><br><br>
<input type="submit" value="添加" >
</form>
</div>
</body>
</html>
allUser中代码修改为以下代码
<html>
<head>
<title>user列表</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body style="background-image: url(images/p2.jpg);background-size: 100%">
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
基于servlet+jsp框架的管理系统:简单实现增、删、改、查。
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>用户列表 —— 显示所有用户</small>
</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 column">
<a class="btn btn-primary" href="addUser.jsp">新增</a>
</div>
<form action="/findAll.do" method="post">
<input name="searchname" type="text">
<input type="submit" value="搜索">
</form>
</div>
<br/>
<br/>
<div class="row clearfix">
<div class="col-md-12 column">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>id</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${pageInfo.list}">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td><a href="/delete.do?id=${user.id}">删除</a>|
<a href="/toupdate.do?id=${user.id}">修改</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
<div>
<a href="/findAll.do?pageNum=1&size=5">首页</a>
<a href="/findAll.do?pageNum=${pageInfo.pageNum-1}&size=5">上一页</a>
<c:forEach begin="1" end="${pageInfo.pages}" var="i">
<a href="/findAll.do?pageNum=${i}&size=5">${i}</a>
</c:forEach>
<a href="/findAll.do?pageNum=${pageInfo.pageNum+1}&size=5">下一页</a>
<a href="/findAll.do?pageNum=${pageInfo.pages}&size=5">尾页</a>
</div>
</div>
</body>
</html>
updateUser
<%--
Created by IntelliJ IDEA.
User: xcdn
Date: 2019/9/29
Time: 11:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<<head>
<title>更新用户</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body style="background-image: url(images/p2.jpg);background-size: 100%">
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
基于servlet+jsp框架的管理系统:简单实现增、删、改、查。
</h1>
</div>
</div>
</div>
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1>
<small>更新用户</small>
</h1>
</div>
</div>
</div>
<form action="/update.do" method="post">
<input type="hidden" name="id" value="${user.id}"><br><br><br>
用户姓名:<input type="text" name="username" value="${user.username}"><br><br><br>
用户密码:<input type="text" name="password" value="${user.password}"><br><br><br>
<input type="submit" value="更新" >
</form>
</div>
</body>
</html>