转载:https://blog.csdn.net/sinat_42338962/article/details/84314428
https://blog.csdn.net/zhijun0901/article/details/88084357
1.PageInfo属性表
1 当前页 2 private int pageNum; 3 每页的数量 4 private int pageSize; 5 当前页的数量 6 private int size; 7 //由于startRow和endRow不常用,这里说个具体的用法 8 //可以在页面中"显示startRow到endRow 共size条数据" 9 10 当前页面第一个元素在数据库中的行号 11 private int startRow; 12 当前页面最后一个元素在数据库中的行号 13 private int endRow; 14 总记录数 15 private long total; 16 总页数 17 private int pages; 18 结果集 19 private List<T> list; 20 21 第一页 22 private int firstPage; 23 前一页 24 private int prePage; 25 26 是否为第一页 27 private boolean isFirstPage = false; 28 是否为最后一页 29 private boolean isLastPage = false; 30 是否有前一页 31 private boolean hasPreviousPage = false; 32 是否有下一页 33 private boolean hasNextPage = false; 34 导航页码数 35 private int navigatePages; 36 所有导航页号 37 private int[] navigatepageNums; 38 后台分页 39 40 服务器端 41 service 42 public PageInfo<T> methodName(int pageNum, int pageSize) { 43 //1 设置分页 44 PageHelper.startPage(pageNum, pageSize); 45 //2 查询 46 List<T> list =TMapper.mapperMethod(); 47 //3 返回 48 return new PageInfo<>(list); 49 } 50 Web 51 public @ResponseBody DataGridResultInfo methodName (Vovo){ 52 //1 查询 53 PageInfo<T> pageInfo = service. methodName (vo.getPage(), vo.getRows()); 54 //2 封装 55 return new DataGridBean(pageInfo.getTotal() , pageInfo.getList() ); 56 } 57 58 浏览器端 59 Datagrid 60 61 $(function(){ 62 //绘制datagrid 63 //1 准备数据 64 // 1.1 列列表 65 var columnArr = [[ 66 {field:'字段名1',title:'标题1',width:80}, 67 {field:'字段名2',title:'标题2',width:80, 68 formatter:function(value,rows,index){ 69 //filed匹配值(当前的值),当前行,当前行号 70 return value.info; 71 } 72 } 73 ]]; 74 // 1.2 工具条 75 var toolbarArr = [ 76 { 77 iconCls: 'icon-add',//按钮图标 78 text : '添加用户', 79 handler: showadduser//方法名 80 } 81 ]; 82 // 1.3 请求路径 83 var url = "……"; 84 85 //2 准备参数 86 var options = { 87 "columns":columnArr, 88 "toolbar":toolbarArr, 89 "striped":true, //隔行换色 90 "idField":"id", //标识字段 91 "url":url, //请求路径 92 "pagination":true, 93 "rownumbers":true, 94 "pageSize":2, 95 "pageList":[2,4,6,8] 96 }; 97 98 //3 绘制 99 $("#id值").datagrid( options ); 100 101 }); 102 103 感觉这个很简单,没什么好写的 104 不用工具 105 <c:forEach items="${pageInfo.list}" var="p"> 106 <tr> 107 <td>${p.属性1}</td> 108 <td>${p.属性2}</td> 109 <td>${p.属性…}</td> 110 </tr> 111 </c:forEach>
2.PageInfo类的使用——Java Page分页显示
1 //entity层实体类 2 3 import java.util.List; 4 5 //分页展示 6 //相关属性:当前页,页大小(每页显示的条数),总页数,总条数,数据 7 //select * from t_user limit 3,3 8 public class Page { 9 private Integer currentPage; //当前页 10 private Integer pageSize; //页大小 11 private Integer pageCount; //页数量 12 private Integer totalCount; //总条数 13 private List<?> list; //数据 14 private String url; //参数路径 15 public Integer getCurrentPage() { 16 return currentPage; 17 } 18 public void setCurrentPage(Integer currentPage) { 19 this.currentPage = currentPage; 20 } 21 public Integer getPageSize() { 22 return pageSize; 23 } 24 public void setPageSize(Integer pageSize) { 25 this.pageSize = pageSize; 26 } 27 public Integer getPageCount() { 28 return pageCount; 29 } 30 public void setPageCount(Integer pageCount) { 31 this.pageCount = pageCount; 32 } 33 public Integer getTotalCount() { 34 return totalCount; 35 } 36 public void setTotalCount(Integer totalCount) { 37 this.totalCount = totalCount; 38 } 39 public List<?> getList() { 40 return list; 41 } 42 public void setList(List<?> list) { 43 this.list = list; 44 } 45 public String getUrl() { 46 return url; 47 } 48 public void setUrl(String url) { 49 this.url = url; 50 } 51 52 @Override 53 public String toString() { 54 return "Page [currentPage=" + currentPage + ", pageSize=" + pageSize + ", pageCount=" + pageCount 55 + ", totalCount=" + totalCount + ", list=" + list + ", url=" + url + "]"; 56 } 57 58 public Page(Integer currentPage, Integer pageSize, Integer pageCount, Integer totalCount, List<?> list, 59 String url) { 60 this.currentPage = currentPage; 61 this.pageSize = pageSize; 62 this.pageCount = pageCount; 63 this.totalCount = totalCount; 64 this.list = list; 65 this.url = url; 66 } 67 68 public Page() { 69 } 70 71 public Page(Integer currentPage, Integer pageSize) { 72 this.currentPage = currentPage; 73 this.pageSize = pageSize; 74 } 75 }
1 // servlet层 (分页相关代码) 2 //-----直接做分页展示----- 3 String current = request.getParameter("currentPage"); // 获取前端传入的当前页 4 Page page = goodsInfoService.getPage(current); // 在业务层给page对象赋值 5 request.setAttribute("page", page);
1 //service层相关代码 2 public Page getPage(String current) { 3 Integer currentPage = 1; // 默认为第一页 4 Integer pageSize = 5; // 每页显示5条记录 5 if(current != null){ 6 currentPage = Integer.parseInt(current); 7 } 8 Page page = new Page(currentPage, pageSize); //当前页和页大小的赋值 9 10 Integer totalCount = goodsInfoDao.getTotalCount(); 11 page.setTotalCount(totalCount); //设置总条数 12 13 // 页数量==总条数/页大小--如果整除就是该值,否则+1 14 Integer pageCount = totalCount/pageSize; 15 pageCount=totalCount%pageSize==0?pageCount:pageCount+1; 16 page.setPageCount(pageCount); //设置页数量 17 18 Integer startIndex = (currentPage-1)*pageSize; // 起始下标为(当前页-1)*页大小 19 List<GoodsInfo> list = goodsInfoDao.getGoodsInfoListPage(startIndex, pageSize); 20 page.setList(list); //设置数据 21 22 page.setUrl("GoodsInfoServlet?action=goodsInfoList"); //设置url 23 24 return page; 25 }
1 //Dao相关分页代码 2 public int getTotalCount() { // 获取总记录数 3 String sql="select count(1) from t_goods_info"; 4 return CommonUtils.getTotalCount(sql); 5 }
1 //jquery 相关分页代码 2 <a href="${page.url}¤tPage=1">首页</a> 3 <c:if test="${page.currentPage!=1}"> 4 <a href="${page.url}¤tPage=${page.currentPage-1}">上一页</a> 5 </c:if> 6 <c:if test="${page.currentPage!=page.pageCount}"> 7 <a href="${page.url}¤tPage=${page.currentPage+1}">下一页</a> 8 </c:if> 9 <a href="${page.url}¤tPage=${page.pageCount}">尾页</a> 10 共${page.currentPage}/${page.pageCount}页
3.MyBatis分页插件-PageHelper的配置与应用
以下转载于:https://www.cnblogs.com/li150dan/p/9706585.html
下载地址:https://github.com/pagehelper/Mybatis-PageHelper
Pagehelper 下载地址:
http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/
jsqlparser 下载地址:
http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/
使用步骤:
pom.xml 引入依赖:
<!-- pagehelper :分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.2</version> </dependency> <!-- pagehelper的依赖包:jsqlparser --> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>0.9.5</version> </dependency>
1、导入相关包 pagehelper-x.x.x.jar 和 jsqlparser-x.x.x.jar。
2、在MyBatis全局配置文件中配置分页插件。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- 注意 <plugins> 在xml文件中的位置,必须要符合 http://mybatis.org/dtd/mybatis-3-config.dtd 中指定的顺序:--> <!-- configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers?) --> <configuration> <!-- 为SQL定义部分的parameterType或resultType属性指定自定义类型的别名 --> <typeAliases> <typeAlias alias="ServiceStation" type="com.mybatis.models.ServiceStation" /> <typeAlias alias="InspectorInfo" type="com.mybatis.models.InspectorInfo" /> <typeAlias alias="StationInspector" type="com.mybatis.models.StationInspector" /> </typeAliases> <!-- 配置分页拦截器 --> <plugins> <!-- 配置分页插件 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> <!-- com.mybatis.util为PageHelper类所在包名 --> <!-- <plugin interceptor="com.mybatis.util.PagePlugin"> --> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <!-- <property name="dialect" value="SQLite" /> --> <!-- <property name="pageSqlId" value=".*Page.*" /> --> <!-- </plugin> --> </plugins> <!-- 设置数据库连接参数 --> <!-- 与spring 集成之后,这些可以完全删除,数据库连接的管理交给 spring 去管理 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="jdbc:sqlserver://mssql-rw-cyp-coopbusiness.vip.test.suixinhuan.com;DatabaseName=CYP_CoopBusiness" /> <property name="username" value="Umanager" /> <property name="password" value="ASD123asd!1" /> </dataSource> </environment> </environments> <!-- 加载SQL定义文件 --> <!-- 这里交给sqlSessionFactory 的 mapperLocations属性去得到所有配置信息 --> <mappers> <mapper resource="com/mybatis/sql/ServiceStation.xml" /> <mapper resource="com/mybatis/sql/InspectorInfo.xml" /> <mapper resource="com/mybatis/sql/StationInspector.xml" /> </mappers> </configuration>
3、使用PageHelper提供的方法进行分页
1 package com.mybatis; 2 3 import java.util.List; 4 5 import org.apache.ibatis.session.SqlSession; 6 7 import com.github.pagehelper.Page; 8 import com.github.pagehelper.PageHelper; 9 import com.github.pagehelper.PageInfo; 10 import com.mybatis.dao.InspectorInfoMapper; 11 import com.mybatis.dao.MyBatisUtil; 12 import com.mybatis.dao.ServiceStationMapper; 13 import com.mybatis.dao.StationInspectorMapper; 14 import com.mybatis.models.ServiceStation; 15 import com.mybatis.models.InspectorInfo; 16 import com.mybatis.models.StationInspector; 17 //import com.mybatis.util.Page; 18 19 /* 20 * 测试类 21 */ 22 public class HelloMyBatisProgram { 23 public static void main(String[] args) { 24 SqlSession session = MyBatisUtil.getSession(); 25 //由框架生成ServiceStationMapper接口实现对象 26 ServiceStationMapper ssDaoMaper = session.getMapper(ServiceStationMapper.class); 27 InspectorInfoMapper iiDaoMaper = session.getMapper(InspectorInfoMapper.class); 28 StationInspectorMapper siDaoMaper = session.getMapper(StationInspectorMapper.class); 29 //System.out.println(ssDaoMaper.getClass().getName()); 30 System.out.println("===========分页获取所有服务站列表============"); 31 32 //分页设置放在查询之前 33 Page<Object> page = PageHelper.startPage(1, 5, "StationName"); 34 List<ServiceStation> listPage = ssDaoMaper.findAllPage("110100"); 35 for(ServiceStation item:listPage) { 36 System.out.println(item.getStationName()+" "+item.getCityCode()+" "+item.getCityName()); 37 } 38 System.out.println("当前页码:"+page.getPageNum()); 39 System.out.println("每页的记录数:"+page.getPageSize()); 40 System.out.println("总记录数:"+page.getTotal()); 41 System.out.println("总页码:"+page.getPages()); 42 } 43 }
4、可以使用更强大的PageInfo封装返回结果
1 package com.mybatis; 2 3 import java.util.List; 4 5 import org.apache.ibatis.session.SqlSession; 6 7 import com.github.pagehelper.Page; 8 import com.github.pagehelper.PageHelper; 9 import com.github.pagehelper.PageInfo; 10 import com.mybatis.dao.InspectorInfoMapper; 11 import com.mybatis.dao.MyBatisUtil; 12 import com.mybatis.dao.ServiceStationMapper; 13 import com.mybatis.dao.StationInspectorMapper; 14 import com.mybatis.models.ServiceStation; 15 import com.mybatis.models.InspectorInfo; 16 import com.mybatis.models.StationInspector; 17 //import com.mybatis.util.Page; 18 19 /* 20 * 测试类 21 */ 22 public class HelloMyBatisProgram { 23 public static void main(String[] args) { 24 SqlSession session = MyBatisUtil.getSession(); 25 //由框架生成ServiceStationMapper接口实现对象 26 ServiceStationMapper ssDaoMaper = session.getMapper(ServiceStationMapper.class); 27 InspectorInfoMapper iiDaoMaper = session.getMapper(InspectorInfoMapper.class); 28 StationInspectorMapper siDaoMaper = session.getMapper(StationInspectorMapper.class); 29 //System.out.println(ssDaoMaper.getClass().getName()); 30 System.out.println("===========分页获取所有服务站列表============"); 31 32 //分页设置放在查询之前 33 page = PageHelper.startPage(1, 3, "StationName desc"); 34 List<ServiceStation> list = ssDaoMaper.findAll(); 35 for(ServiceStation item:list) { 36 System.out.println(item.getStationName()+" "+item.getCityCode()+" "+item.getCityName()); 37 } 38 PageInfo<ServiceStation> info = new PageInfo<ServiceStation>(list, 3); 39 System.out.println("当前页码:"+info.getPageNum()); 40 System.out.println("每页的记录数:"+info.getPageSize()); 41 System.out.println("总记录数:"+info.getTotal()); 42 System.out.println("总页码:"+info.getPages()); 43 System.out.println("是否第一页:"+info.isIsFirstPage()); 44 System.out.println("连续显示的页码:"); 45 int[] nums = info.getNavigatepageNums(); 46 for (int i = 0; i < nums.length; i++) { 47 System.out.println(nums[i]); 48 } 49 } 50 }
今天的文章PageInfo的介绍与使用分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/51091.html