spring boot之分頁外掛PageHepler
阿新 • • 發佈:2018-11-08
一.使用原生的PageHepler
1.新增依賴
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.3</version>
</dependency>
2.spring boot啟動類中新增配置
@Bean
PageHelper pageHelper(){
//分頁外掛
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("reasonable", "true");
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("returnPageInfo", "check");
properties.setProperty("params", "count=countSql" );
pageHelper.setProperties(properties);
//新增外掛
new SqlSessionFactoryBean().setPlugins(new Interceptor[]{pageHelper});
return pageHelper;
}
二.使用spring boot提供的starter
1.新增依賴
<!-- 分頁外掛 -->
<dependency>
<groupId>com.github.pagehelper</groupId >
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.9</version>
</dependency>
2.application.properties新增配置
#pagehelper外掛
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
三.使用
// pageNum頁碼 pageSize每一頁顯示條數
PageHelper.startPage(pageNum, pageSize);
List<User> users = userMapper.findAllUsers();
PageInfo<User> pageInfo = new PageInfo<>(users);
System.out.println("appsPageInfo is " + pageInfo);