1.pageInfo 5
currentpage size list totalpage totalpage
2.dao
1.查询所要展现的数据 2.查询总条数
3.service
给pageinfo 赋值
4.controller
把pageinfo 传递给页面
5.在页面展现
pagehelper
1.导jar包
2.applicationcontext里面告诉数据库我们用了pagehelper这个插件
3.service 在执行具体sql前
PageHelper.startPage(pageNum,size);
4.controller
PageInfo pageInfo=new PageInfo<>();
pageInfo.setList(userService.findAll(pageNum,size));
基于“基于spring+mybatis框架的管理系统:简单实现增、删、改、查。(二)”基础进行修改与添加
模糊查询:
1.UserMapper.xml
<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>
2.IUserDao :
List<User> findAll(@Param("searchname") String searchname);
3.IUserService:
List<User> findAll(int currentPage,int size,String searchname);
4.UserService:
@Override
public List<User> findAll(int currentPage,int size,String searchname) {
PageHelper.startPage(currentPage,size);
return userDao.findAll(searchname);
}
5.在zhongruan包新建一个filter包,新建LoginFilter.java
package com.zhongruan.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
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() {
}
}
6.UserController
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "5") int size,
String searchname, HttpSession session){
ModelAndView modelAndView=new ModelAndView();
String username= (String) session.getAttribute("username");
if (username==null){
modelAndView.setViewName("failure.jsp");
return 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;
}