轉自:https://www.jianshu.com/p/5acf5ac7a440
mybatis + pagehelper 數據庫分頁
Mybatis的一個插件,PageHelper,非常方便mybatis分頁查詢。
優點: 不需要自己在每個 mapper文件中寫 limit x,x 這樣的代碼,引入插件調用pagehelper即可自動注入分頁。
1、引入jar
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>最新版本</version> </dependency> //推薦使用下面這種方式 <!--pagehelper--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency>
2、配置插件攔截規則
- 方法一、在 MyBatis 配置 xml 中配置攔截器插件
在mybatis-config.xml中配置
<!-- plugins在配置文件中的位置必須符合要求,否則會報錯,順序如下: properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins> <!-- com.github.pagehelper為PageHelper類所在包名 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 使用下面的方式配置參數,后面會有所有的參數介紹 --> <property name="param1" value="value1"/> </plugin> </plugins>
- 方法二、在 Spring 配置文件中配置攔截器插件
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注意其他配置 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置參數,一行配置一個 --> <value> params=value1 </value> </property> </bean> </array> </property> </bean>
如圖:

image.png
- 方法三、在 SpringBoot配置文件application.properties,添加如下幾行配置信息,推薦使用
#分頁插件 pagehelper.helper-dialect=mysql pagehelper.params=count=countSql pagehelper.reasonable=true pagehelper.support-methods-arguments=true
3、調用
//第一種、RowBounds方式的調用 List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(0, 10)); //第二種、Mapper接口方式的調用,推薦這種使用方式。 PageHelper.startPage(1, 10); List<Country> list = countryMapper.selectIf(1); //第三種、Mapper接口方式的調用,推薦這種使用方式。 PageHelper.offsetPage(1, 10); List<Country> list = countryMapper.selectIf(1); //第四種、參數方法調用 //存在以下 Mapper 接口方法,你不需要在 xml 處理后兩個參數 public interface CountryMapper { List<Country> selectByPageNumSize( @Param("user") User user, @Param("pageNum") int pageNum, @Param("pageSize") int pageSize); } //配置supportMethodsArguments=true //在代碼中直接調用: List<Country> list = countryMapper.selectByPageNumSize(user, 1, 10); //第五種、參數對象 //如果 pageNum 和 pageSize 存在於 User 對象中,只要參數有值,也會被分頁 //有如下 User 對象 public class User { //其他fields //下面兩個參數名和 params 配置的名字一致 private Integer pageNum; private Integer pageSize; } //存在以下 Mapper 接口方法,你不需要在 xml 處理后兩個參數 public interface CountryMapper { List<Country> selectByPageNumSize(User user); } //當 user 中的 pageNum!= null && pageSize!= null 時,會自動分頁 List<Country> list = countryMapper.selectByPageNumSize(user); //第六種、ISelect 接口方式 //jdk6,7用法,創建接口 Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(new ISelect() { @Override public void doSelect() { countryMapper.selectGroupBy(); } }); //jdk8 lambda用法 Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(()-> countryMapper.selectGroupBy()); //也可以直接返回PageInfo,注意doSelectPageInfo方法和doSelectPage pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(new ISelect(