1.环境及导入的jar
mybatis版本3.3.1(建议使用3.3以上的版本)
pagehelper-4.2.1插件及依赖jar(附:jar下载地址)
maven配置:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
2.创建mybatis配置文件:mybatis-config.xml:
<?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">
<!-- 配置管理器 -->
<configuration>
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 4.0.0以后版本可以不设置该参数 -->
<property name="dialect" value="oracle"/>
<!--将RowBounds第一个参数offset当成pageNum页码使用 -->
<property name="offsetAsPageNum" value="true"/>
<!-- 使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
<!-- 如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
<property name="pageSizeZero" value="true"/>
<!-- 如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页(false返回空);-->
<property name="reasonable" value="true"/>
<!-- 支持通过Mapper接口参数来传递分页参数 -->
<property name="supportMethodsArguments" value="true"/>
<!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
<property name="returnPageInfo" value="check"/>
</plugin>
</plugins>
</configuration>
3.在Spring配置中配置sqlSessionFactory的时候引入配置文件
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations"
value="classpath:com/data/dao/*.xml"></property>
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
4.在server 层中使用分页功能
Page<?> page = PageHelper.startPage(1,10,true);
System.out.println(page.toString()+"获得所有page信息");
List<Processs> list=processMapper.selectAllProcess();
5.page对象信息(pagehelper源码)
/**
* 页码,从1开始
*/
private int pageNum;
/**
* 页面大小
*/
private int pageSize;
/**
* 起始行
*/
private int startRow;
/**
* 末行
*/
private int endRow;
/**
* 总数
*/
private long total;
/**
* 总页数
*/
private int pages;
/**
* 包含count查询
*/
private boolean count = true;
/**
* count信号,3种情况,null的时候执行默认BoundSql,true的时候执行count,false执行分页
*/
private Boolean countSignal;
/**
* 排序
*/
private String orderBy;
/**
* 只增加排序
*/
private boolean orderByOnly;
/**
* 分页合理化
*/
private Boolean reasonable;
/**
* 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
*/
private Boolean pageSizeZero;
6.BasePageHelper(pagehelper源码)
/** * 获取任意查询方法的count总数 * * @param select * @return */ public static long count(ISelect select) { Page<?> page = startPage(1, -1, true); select.doSelect(); return page.getTotal(); } /** * 开始分页 * * @param pageNum 页码 * @param pageSize 每页显示数量 */ public static <E> Page<E> startPage(int pageNum, int pageSize) { return startPage(pageNum, pageSize, true); } /** * 开始分页 * * @param pageNum 页码 * @param pageSize 每页显示数量 * @param count 是否进行count查询 */ public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count) { return startPage(pageNum, pageSize, count, null); } /** * 开始分页 * * @param pageNum 页码 * @param pageSize 每页显示数量 * @param orderBy 排序 */ public static <E> Page<E> startPage(int pageNum, int pageSize, String orderBy) { Page<E> page = startPage(pageNum, pageSize); page.setOrderBy(orderBy); return page; } /** * 开始分页 * * @param offset 页码 * @param limit 每页显示数量 */ public static <E> Page<E> offsetPage(int offset, int limit) { return offsetPage(offset, limit, true); } /** * 开始分页 * * @param offset 页码 * @param limit 每页显示数量 * @param count 是否进行count查询 */ public static <E> Page<E> offsetPage(int offset, int limit, boolean count) { Page<E> page = new Page<E>(new int[]{offset, limit}, count); //当已经执行过orderBy的时候 Page<E> oldPage = SqlUtil.getLocalPage(); if (oldPage != null && oldPage.isOrderByOnly()) { page.setOrderBy(oldPage.getOrderBy()); } SqlUtil.setLocalPage(page); return page; } /** * 开始分页 * * @param offset 页码 * @param limit 每页显示数量 * @param orderBy 排序 */ public static <E> Page<E> offsetPage(int offset, int limit, String orderBy) { Page<E> page = offsetPage(offset, limit); page.setOrderBy(orderBy); return page; } /** * 开始分页 * * @param pageNum 页码 * @param pageSize 每页显示数量 * @param count 是否进行count查询 * @param reasonable 分页合理化,null时用默认配置 */ public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count, Boolean reasonable) { return startPage(pageNum, pageSize, count, reasonable, null); } /** * 开始分页 * * @param pageNum 页码 * @param pageSize 每页显示数量 * @param count 是否进行count查询 * @param reasonable 分页合理化,null时用默认配置 * @param pageSizeZero true且pageSize=0时返回全部结果,false时分页,null时用默认配置 */ public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) { Page<E> page = new Page<E>(pageNum, pageSize, count); page.setReasonable(reasonable); page.setPageSizeZero(pageSizeZero); //当已经执行过orderBy的时候 Page<E> oldPage = SqlUtil.getLocalPage(); if (oldPage != null && oldPage.isOrderByOnly()) { page.setOrderBy(oldPage.getOrderBy()); } SqlUtil.setLocalPage(page); return page; } /** * 开始分页 * * @param params */ public static <E> Page<E> startPage(Object params) { Page<E> page = SqlUtil.getPageFromObject(params); //当已经执行过orderBy的时候 Page<E> oldPage = SqlUtil.getLocalPage(); if (oldPage != null && oldPage.isOrderByOnly()) { page.setOrderBy(oldPage.getOrderBy()); } SqlUtil.setLocalPage(page); return page; } /** * 排序 * * @param orderBy */ public static void orderBy(String orderBy) { Page<?> page = SqlUtil.getLocalPage(); if (page != null) { page.setOrderBy(orderBy); } else { page = new Page(); page.setOrderBy(orderBy); page.setOrderByOnly(true); SqlUtil.setLocalPage(page); } } /** * 手动清空分页数据 */ public static void clearPage(){ SqlUtil.clearLocalPage(); }
最后
以上就是坚强宝马最近收集整理的关于mybatis 分页插件pagehelper集成及使用的全部内容,更多相关mybatis内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复