模糊查询
结构
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,
String searchname, HttpSession session){
ModelAndView modelAndView=new ModelAndView();
if (searchname!=null){
session.setAttribute("searchname",searchname);
} else {
searchname= (String) session.getAttribute("searchname");
}
List<User> users=userService.findAll(pageNum, size,searchname);
PageInfo<User> pageInfo=new PageInfo<>(users);
modelAndView.setViewName("allUser.jsp");
modelAndView.addObject("pageInfo",pageInfo);
return modelAndView;
}
@RequestMapping("/login.do")
public String login(User user,HttpSession session){
Boolean flag=userService.login(user.getUsername(),user.getPassword());
if (flag){
session.setAttribute("username",user.getUsername());
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(@Param("searchname") String searchname);
User selectByUserName(String name);
void deleteById(int id);
void add(User user);
void update(User user);
User selectById(int id);
}
新建了一个包filter和java文件LoginFilter
LoginFilter
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request= (HttpServletRequest) servletRequest;
HttpServletResponse response= (HttpServletResponse) servletResponse;
HttpSession session=request.getSession();
if (session.getAttribute("username")==null&&
request.getRequestURI().indexOf("/login.do")==-1){
response.sendRedirect("index.jsp");
}else {
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
}
}
UserService
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<User> findAll(int pageNum,int size,String searchname) {
PageHelper.startPage(pageNum, size);
return userDao.findAll(searchname);
}
@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,String searchname);
Boolean login(String username,String password);
void deleteById(int id);
void add(User user);
void update(User user);
User selectById(int id);
}
UserMapper1.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhongruan.dao.IUserDao">
<select id="findAll" resultType="com.zhongruan.bean.User" parameterType="String">
select * from tb_user
<where>
<if test="searchname!=null">
username like "%"#{searchname}"%"
</if>
</where>
</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>
allUser
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: xcdn
Date: 2019/9/26
Time: 10:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<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>
index
<%--
Created by IntelliJ IDEA.
User: xcdn
Date: 2019/9/25
Time: 17:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<title>后台登录</title>
<link href="css/login.css" type="text/css" rel="stylesheet">
</head>
<body>
<div class="login">
<div class="message">用户登录</div>
<div id="darkbannerwrap"></div>
<form method="post" action="/login.do">
<input name="action" value="login" type="hidden">
<input name="username" placeholder="用户名" required="" type="text">
<hr class="hr15">
<input name="password" placeholder="密码" required="" type="password">
<hr class="hr15">
<input value="登录" style="width:100%;" type="submit">
<hr class="hr20">
<!-- 帮助 <a onClick="alert('请联系管理员')">忘记密码</a> -->
</form>
</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>