mybatis分页插件的使用

一、在maven中引入pagehelper相关依赖包

  1. <dependency>
  2. <groupId>com.github.pagehelper</groupId>
  3. <artifactId>pagehelper</artifactId>
  4. <version>5.1.2</version>
  5. </dependency>

二、applicationContext.xml中注册mybatis插件(mybatis要与spring整合)



  1. <!-- 5.配置mybatis -->
  2. <bean id= "sqlSessionFactory"
  3. class= "org.mybatis.spring.SqlSessionFactoryBean">
  4. <property name= "dataSource" ref= "dataSource" />
  5. <!-- mapperLocations:指定mapper配置文件的位置(相当于Dao的实现类),根据自己项目修改 -->
  6. <property name= "mapperLocations" value= "classpath:mapper/*xml"></property>
  7. <property name= "plugins">
  8. <array>
  9. <bean class= "com.github.pagehelper.PageInterceptor">
  10. <property name= "properties">
  11. <!--使用下面的方式配置参数,一行配置一个 -->
  12. <value>
  13. helperDialect=mysql
  14. </value>
  15. </property>
  16. </bean>
  17. </array>
  18. </property>
  19. </bean>
  20. <!-- 扫描Mapper(Dao)接口,根据自己的项目修改-->
  21. <mybatis-spring:scan base- package= "com.tansen.selecting.mapper"/>

三、编写mapper映射文件(Dao接口的实现类,Dao接口以及Service省略)

  1. <?xml version= "1.0" encoding= "UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--namespace 写接口的全类名 ,根据自己项目修改 -->
  6. <mapper namespace= "com.tansen.selecting.mapper.CourseMapper">
  7. <!-- List<Course> getAllCourses(); -->
  8. <!-- resultType为返回值类型,集合则返回集合的实体类型 -->
  9. <select id= "getCourseNameLike"
  10. resultType= "com.tansen.selecting.entities.Course">
  11. select * from course where name like "%"#{name} "%"
  12. </select>
  13. </mapper>

四、控制器基本代码

  1. @RequestMapping( "/findByNameLike") //模糊查询
  2. public ModelAndView findByNameLike(@RequestParam(value="pn",required=false,defaultValue="1") Integer pn, @RequestParam("name") String name) {
  3. PageHelper.startPage(pn, 5); //第一个参数表示第几页,第二个参数表示每页显示的记录数
  4. List<Course> courses = courseService.getCourseNameLike(name); //查询
  5. PageInfo pageInfo = new PageInfo<>(courses, 5); //用PageInfo对结果进行包装,第二个参数表示前端显示的页数
  6. ModelAndView view = new ModelAndView( "teacher_main");
  7. view.addObject( "name", name);
  8. view.addObject( "pageInfo", pageInfo);
  9. view.addObject( "pn",pageInfo.getPageNum());
  10. return view;
  11. }

五、编写JSP页面

  1. <%@ page language= "java" contentType= "text/html; charset=UTF-8"
  2. pageEncoding= "UTF-8"%>
  3. <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <script type= "text/javascript">
  11. function function1() {
  12. window.location.href = "${pageContext.request.contextPath}/course/addPage";
  13. }
  14. </script>
  15. <body>
  16. <button οnclick= "function1()">新增课程</button>
  17. <br>
  18. <form action= "${pageContext.request.contextPath}/course/findByNameLike"
  19. method= "post">
  20. <input type= "text" name= "name"> <input type= "submit"
  21. value= "查找课程">
  22. </form>
  23. <br>
  24. <table border= "1">
  25. <tr>
  26. <th>课程号</th>
  27. <th>课程名称</th>
  28. <th>课程授课老师</th>
  29. <th>教师容量</th>
  30. <th>当前选课人数</th>
  31. <th>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp操作&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>
  32. </tr>
  33. <c:forEach items= "${pageInfo.list}" var= "pageInfo">
  34. <tr>
  35. <td>${pageInfo.id}</td>
  36. <td>${pageInfo.name}</td>
  37. <td>${pageInfo.teacher}</td>
  38. <td>${pageInfo.capacity}</td>
  39. <td>${pageInfo.number}</td>
  40. <td><a
  41. href= "${pageContext.request.contextPath}/course/editPage?id=${pageInfo.id}&name=${pageInfo.name}&teacher=${pageInfo.teacher}">修改</a></td>
  42. <td><a
  43. href= "${pageContext.request.contextPath}/course/delete?id=${pageInfo.id}&pn=${pageNum}">删除</a></td>
  44. <td><a href= "">管理当前选课</a></td>
  45. </tr>
  46. </c:forEach>
  47. </table>
  48. <c:choose>
  49. <c:when test= "${pageInfo.hasPreviousPage}">
  50. <a
  51. href= "${pageContext.request.contextPath}/course/findByNameLike?pn=${pageInfo.pageNum-1}&name=${name}">上一页</a>
  52. </c:when>
  53. <c:otherwise>
  54. <a href= "javascript:void(0);">上一页</a>
  55. </c:otherwise>
  56. </c:choose>
  57. <c:forEach items= "${pageInfo.navigatepageNums}" var= "page_num">
  58. <c: if test= "${page_num == pageInfo.pageNum}">
  59. <a href= " ">${page_num}</a>
  60. </c: if>
  61. <c: if test= "${page_num != pageInfo.pageNum}">
  62. <a
  63. href= "${pageContext.request.contextPath}/course/teacherFindAll?pn=${page_num}">${page_num}</a>
  64. </c: if>
  65. </c:forEach>
  66. <c:choose>
  67. <c:when test= "${pageInfo.hasNextPage}">
  68. <a
  69. href= "${pageContext.request.contextPath}/course/findByNameLike?pn=${pageInfo.pageNum+1}&name=${name}">下一页</a>
  70. </c:when>
  71. <c:otherwise>
  72. <a href= "javascript:void(0);">下一页</a>
  73. </c:otherwise>
  74. </c:choose>
  75. 共${pageInfo.pages}页,一共${pageInfo.total}条记录,到第
  76. <form
  77. action= "${pageContext.request.contextPath}/course/teacherFindAll"
  78. method= "post">
  79. <input type= "text" name= "pn">页 <input type= "submit"
  80. value= "确定">
  81. </form>
  82. </body>
  83. </html>

注意事项:前端页面中${pageInfo.属性}中的属性必须和数据库中的字段用驼峰命名法或者resultMap建立映射关系,否则在前端是获取不到值的。

六、PageInfo类的说明

  1. public class PageInfo<T> implements Serializable {
  2. private static final long serialVersionUID = 1L;
  3. //当前页
  4. private int pageNum;
  5. //每页的数量
  6. private int pageSize;
  7. //当前页的数量
  8. private int size;
  9. //由于startRow和endRow不常用,这里说个具体的用法
  10. //可以在页面中"显示startRow到endRow 共size条数据"
  11. //当前页面第一个元素在数据库中的行号
  12. private int startRow;
  13. //当前页面最后一个元素在数据库中的行号
  14. private int endRow;
  15. //总记录数
  16. private long total;
  17. //总页数
  18. private int pages;
  19. //结果集
  20. private List<T> list;
  21. //前一页
  22. private int prePage;
  23. //下一页
  24. private int nextPage;
  25. //是否为第一页
  26. private boolean isFirstPage = false;
  27. //是否为最后一页
  28. private boolean isLastPage = false;
  29. //是否有前一页
  30. private boolean hasPreviousPage = false;
  31. //是否有下一页
  32. private boolean hasNextPage = false;
  33. //导航页码数
  34. private int navigatePages;
  35. //所有导航页号
  36. private int[] navigatepageNums;
  37. //导航条上的第一页
  38. private int navigateFirstPage;
  39. //导航条上的最后一页
  40. private int navigateLastPage;
  41. public PageInfo() {
  42. }
  43. /**
  44. * 包装Page对象
  45. *
  46. * @param list
  47. */
  48. public PageInfo(List<T> list) {
  49. this(list, 8);
  50. }
  51. /**
  52. * 包装Page对象
  53. *
  54. * @param list page结果
  55. * @param navigatePages 页码数量
  56. */
  57. public PageInfo(List<T> list, int navigatePages) {
  58. if (list instanceof Page) {
  59. Page page = (Page) list;
  60. this.pageNum = page.getPageNum();
  61. this.pageSize = page.getPageSize();
  62. this.pages = page.getPages();
  63. this.list = page;
  64. this.size = page.size();
  65. this.total = page.getTotal();
  66. //由于结果是>startRow的,所以实际的需要+1
  67. if ( this.size == 0) {
  68. this.startRow = 0;
  69. this.endRow = 0;
  70. } else {
  71. this.startRow = page.getStartRow() + 1;
  72. //计算实际的endRow(最后一页的时候特殊)
  73. this.endRow = this.startRow - 1 + this.size;
  74. }
  75. } else if (list instanceof Collection) {
  76. this.pageNum = 1;
  77. this.pageSize = list.size();
  78. this.pages = this.pageSize > 0 ? 1 : 0;
  79. this.list = list;
  80. this.size = list.size();
  81. this.total = list.size();
  82. this.startRow = 0;
  83. this.endRow = list.size() > 0 ? list.size() - 1 : 0;
  84. }
  85. if (list instanceof Collection) {
  86. this.navigatePages = navigatePages;
  87. //计算导航页
  88. calcNavigatepageNums();
  89. //计算前后页,第一页,最后一页
  90. calcPage();
  91. //判断页面边界
  92. judgePageBoudary();
  93. }
  94. }
  95. .......
  96. }


**原文地址:https://blog.csdn.net/laogay_tansen/article/details/80898913

  1. <!-- 5.配置mybatis -->
  2. <bean id= "sqlSessionFactory"
  3. class= "org.mybatis.spring.SqlSessionFactoryBean">
  4. <property name= "dataSource" ref= "dataSource" />
  5. <!-- mapperLocations:指定mapper配置文件的位置(相当于Dao的实现类),根据自己项目修改 -->
  6. <property name= "mapperLocations" value= "classpath:mapper/*xml"></property>
  7. <property name= "plugins">
  8. <array>
  9. <bean class= "com.github.pagehelper.PageInterceptor">
  10. <property name= "properties">
  11. <!--使用下面的方式配置参数,一行配置一个 -->
  12. <value>
  13. helperDialect=mysql
  14. </value>
  15. </property>
  16. </bean>
  17. </array>
  18. </property>
  19. </bean>
  20. <!-- 扫描Mapper(Dao)接口,根据自己的项目修改-->
  21. <mybatis-spring:scan base- package= "com.tansen.selecting.mapper"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值