PageInfo的介绍与使⽤
1.PageInfo属性表
1当前页
2private int pageNum;
3每页的数量
4private int pageSize;
5当前页的数量
6private int size;
7//由于startRow和endRow不常⽤,这⾥说个具体的⽤法
8//可以在页⾯中"显⽰startRow到endRow 共size条数据"
9
10当前页⾯第⼀个元素在数据库中的⾏号
11private int startRow;
12当前页⾯最后⼀个元素在数据库中的⾏号
13private int endRow;
14总记录数
15private long total;
16总页数
17private int pages;
18结果集
19private List<T> list;
20
21第⼀页
22private int firstPage;
23前⼀页
24private int prePage;
25
26是否为第⼀页
27private boolean isFirstPage = false;
28是否为最后⼀页
29private boolean isLastPage = false;
30是否有前⼀页
31private boolean hasPreviousPage = false;
32是否有下⼀页
33private boolean hasNextPage = false;
34导航页码数
35private int navigatePages;
36所有导航页号
37private int[] navigatepageNums;
38后台分页
39
40服务器端
41 service
42public PageInfo<T> methodName(int pageNum, int pageSize) {
43//1 设置分页
44 PageHelper.startPage(pageNum, pageSize);
45//2 查询
46 List<T> list =TMapper.mapperMethod();
47//3 返回
48return new PageInfo<>(list);
49 }
50 Web
51public @ResponseBody DataGridResultInfo methodName (Vovo){
52//1 查询
53 PageInfo<T> pageInfo = service. methodName (vo.getPage(), vo.getRows());
54//2 封装
55return new Total() , List() );
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匹配值(当前的值),当前⾏,当前⾏号
70return 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
3import java.util.List;
4
5//分页展⽰
6//相关属性:当前页,页⼤⼩(每页显⽰的条数),总页数,总条数,数据
7//select * from t_user limit 3,3
8public class Page {
9private Integer currentPage; //当前页
10private Integer pageSize; //页⼤⼩
11private Integer pageCount; //页数量
12private Integer totalCount; //总条数
13private List<?> list; //数据
14private String url; //参数路径
15public Integer getCurrentPage() {
16return currentPage;
17 }
18public void setCurrentPage(Integer currentPage) {
19this.currentPage = currentPage;
20 }
21public Integer getPageSize() {
22return pageSize;
23 }
24public void setPageSize(Integer pageSize) {
25this.pageSize = pageSize;
26 }
27public Integer getPageCount() {
28return pageCount;
29 }
30public void setPageCount(Integer pageCount) {
31this.pageCount = pageCount;
32 }
33public Integer getTotalCount() {
34return totalCount;
35 }
36public void setTotalCount(Integer totalCount) {
38 }
39public List<?> getList() {
40return list;
41 }
42public void setList(List<?> list) {
43this.list = list;
44 }
45public String getUrl() {
46return url;
47 }
48public void setUrl(String url) {
49this.url = url;
50 }
51
52 @Override
53public String toString() {
54return "Page [currentPage=" + currentPage + ", pageSize=" + pageSize + ", pageCount=" + pageCount
55 + ", totalCount=" + totalCount + ", list=" + list + ", url=" + url + "]";
56 }
57
58public Page(Integer currentPage, Integer pageSize, Integer pageCount, Integer totalCount, List<?> list, 59 String url) {
60this.currentPage = currentPage;
61this.pageSize = pageSize;
62this.pageCount = pageCount;
64this.list = list;
65this.url = url;
66 }
67
68public Page() {
69 }
70
71public Page(Integer currentPage, Integer pageSize) {
72this.currentPage = currentPage;
73this.pageSize = pageSize;
74 }
75 }
1// servlet层(分页相关代码)
2//-----直接做分页展⽰-----
3 String current = Parameter("currentPage"); // 获取前端传⼊的当前页
4 Page page = Page(current); // 在业务层给page对象赋值
5 request.setAttribute("page", page);
1//service层相关代码
2public Page getPage(String current) {
3 Integer currentPage = 1; // 默认为第⼀页
4 Integer pageSize = 5; // 每页显⽰5条记录
5if(current != null){
6 currentPage = Integer.parseInt(current);
7 }
8 Page page = new Page(currentPage, pageSize); //当前页和页⼤⼩的赋值
9
10 Integer totalCount = TotalCount();
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 = GoodsInfoListPage(startIndex, pageSize);
20 page.setList(list); //设置数据
21
22 page.setUrl("GoodsInfoServlet?action=goodsInfoList"); //设置url
23
24return page;
25 }
1//Dao相关分页代码
2public int getTotalCount() { // 获取总记录数
3 String sql="select count(1) from t_goods_info";
TotalCount(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的配置与应⽤
Pagehelper 下载地址:
jsqlparser 下载地址:
使⽤步骤:
<!-- 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、导⼊相关包 jar 和 jar。
2、在MyBatis全局配置⽂件中配置分页插件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<!-- 注意 <plugins> 在xml⽂件中的位置,必须要符合 /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="dels.ServiceStation"/>
<typeAlias alias="InspectorInfo" type="dels.InspectorInfo"/>
<typeAlias alias="StationInspector" type="dels.StationInspector"/>
</typeAliases>
<!-- 配置分页 -->
<plugins>
<!-- 配置分页插件 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
<!-- batis.util为PageHelper类所在包名 -->
<!-- <plugin interceptor="batis.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://st.suixinhuan;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/l"/>jquery是什么有什么作用
<mapper resource="com/mybatis/l"/>
<mapper resource="com/mybatis/l"/>
</mappers>
</configuration>
3、使⽤PageHelper提供的⽅法进⾏分页
batis;
2
3import java.util.List;
4
5import org.apache.ibatis.session.SqlSession;
6
7import com.github.pagehelper.Page;
8import com.github.pagehelper.PageHelper;
9import com.github.pagehelper.PageInfo;
batis.dao.InspectorInfoMapper;
batis.dao.MyBatisUtil;
batis.dao.ServiceStationMapper;
batis.dao.StationInspectorMapper;
dels.ServiceStation;
dels.InspectorInfo;
dels.StationInspector;
17//batis.util.Page;
18
19/*
20 * 测试类
21*/
22public class HelloMyBatisProgram {
23public static void main(String[] args) {
24 SqlSession session = Session();
25//由框架⽣成ServiceStationMapper接⼝实现对象
26 ServiceStationMapper ssDaoMaper = Mapper(ServiceStationMapper.class);
27 InspectorInfoMapper iiDaoMaper = Mapper(InspectorInfoMapper.class);
28 StationInspectorMapper siDaoMaper = Mapper(StationInspectorMapper.class);
29//System.out.Class().getName());
30 System.out.println("===========分页获取所有服务站列表============");
31
32//分页设置放在查询之前
33 Page<Object> page = PageHelper.startPage(1, 5, "StationName");
34 List<ServiceStation> listPage = ssDaoMaper.findAllPage("110100");
35for(ServiceStation item:listPage) {
36 System.out.StationName()+" "+CityCode()+" "+CityName());
37 }
38 System.out.println("当前页码:"+PageNum());
39 System.out.println("每页的记录数:"+PageSize());
40 System.out.println("总记录数:"+Total());
41 System.out.println("总页码:"+Pages());
42 }
43 }
4、可以使⽤更强⼤的PageInfo封装返回结果
batis;
2
3import java.util.List;
4
5import org.apache.ibatis.session.SqlSession;
6
7import com.github.pagehelper.Page;
8import com.github.pagehelper.PageHelper;
9import com.github.pagehelper.PageInfo;
batis.dao.InspectorInfoMapper;
batis.dao.MyBatisUtil;
batis.dao.ServiceStationMapper;
batis.dao.StationInspectorMapper;
dels.ServiceStation;
dels.InspectorInfo;
dels.StationInspector;
17//batis.util.Page;
18
19/*
20 * 测试类
21*/
22public class HelloMyBatisProgram {
23public static void main(String[] args) {
24 SqlSession session = Session();
25//由框架⽣成ServiceStationMapper接⼝实现对象
26 ServiceStationMapper ssDaoMaper = Mapper(ServiceStationMapper.class);
27 InspectorInfoMapper iiDaoMaper = Mapper(InspectorInfoMapper.class);
28 StationInspectorMapper siDaoMaper = Mapper(StationInspectorMapper.class);
29//System.out.Class().getName());
30 System.out.println("===========分页获取所有服务站列表============");
31
32//分页设置放在查询之前
33 page = PageHelper.startPage(1, 3, "StationName desc");
34 List<ServiceStation> list = ssDaoMaper.findAll();
35for(ServiceStation item:list) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论