SpringBoot Mybatis 分页插件PageHelper

时间:2022-11-08 19:54:40

添加maven配置:

    <!-- 分布插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.</version>
</dependency>

添加Bean:

//配置mybatis的分页插件pageHelper
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
//设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用
properties.setProperty("offsetAsPageNum","true");
//设置为true时,使用RowBounds分页会进行count查询
properties.setProperty("rowBoundsWithCount","true");
//设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果
//properties.setProperty("pageSizeZero","true");
//禁用合理化,启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
properties.setProperty("reasonable","false");
//配置mysql数据库的方言
properties.setProperty("dialect","mysql"); //配置mysql数据库的方言
pageHelper.setProperties(properties);
return pageHelper;
}

SqlSessionFactory:

    @Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource,PageHelper pageHelper) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
Resource[] mappers = (new PathMatchingResourcePatternResolver())
.getResources("classpath*:mybatis_mapper/*Mapper.xml");
sessionFactory.setMapperLocations(mappers);
sessionFactory.setPlugins(new Interceptor[]{pageHelper});
return sessionFactory.getObject();
}

应用:

    public PageInfo<DataDict> selectListByParentKey(String parentKey){
PageHelper.startPage(,);
List<DataDict> list=mapper.selectListByParentKey(parentKey);
PageInfo<DataDict> pageInfo = new PageInfo<>(list);
return pageInfo;
}

封装返回的PageInfo

public class PageResult {

    private ModelMapper modelMapper=SpringUtils.getBean(ModelMapper.class);

    private PageInfo pageInfo;

    private Class aClass;

    public PageResult(PageInfo _pageInfo ){
this.pageInfo=_pageInfo;
} /**
*
* @param _pageInfo pageHelper的实例
* @param _aClass list输出类型,如果aClass不为空,就将list转换为aClass类型,反之则不进行转换
*/
public PageResult(PageInfo _pageInfo,Class _aClass){
this.pageInfo=_pageInfo;
this.aClass=_aClass;
} private Integer pageIndex; private Integer pageSize; private Long totalCount; private Integer totalPages; private List list; public Integer getPageIndex() {
return pageInfo.getPageNum();
} public Integer getPageSize() {
return pageInfo.getPageSize();
} public Long getTotalCount() {
return pageInfo.getTotal();
} public Integer getTotalPages(){
return pageInfo.getPages();
} public List getList() {
List list= pageInfo.getList();
if(null!=aClass) {
List list2 = new ArrayList();
list.forEach(x -> {
list2.add(modelMapper.map(x, aClass));
});
return list2;
}else{
return list;
}
}
}

调用:

        PageHelper.startPage(request.getPageIndex(),request.getPageSize());
List<DataDict> list=dataDictBizFacade.selectListByCondition(request.getDictKey(),request.getDictDesc(),request.getParentKey());
PageResult result=new PageResult(new PageInfo<>(list),DataDictResponse.class);
response.setData(result);

===============================================

4.1.6版本

会出现没调用pagehelper的sql里面也进行 分页

升级5.1.10版本

pom.xml

    <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.</version>
</dependency>

配置类修改为:

@Configuration
@ConfigurationProperties("jdbc.datasource")
@MapperScan(basePackages = "com.shitou.huishi.domain.dataaccess", annotationClass = MyBatisMapper.class)
public class DataSourceConfig {
// 动态配置从esb config读取
private String url = "jdbc:mysql://120.26.130.187:3306/huishi-server?useUnicode=true&characterEncoding=utf-8&useSSL=false";
private String username = "root";
private String password = "jsy2016memeda";
private String driverClassName = "com.mysql.jdbc.Driver"; public void setUrl(String url) {
this.url = url;
} public void setUsername(String username) {
this.username = username;
} public void setPassword(String password) {
this.password = password;
} public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
} //配置mybatis的分页插件pageHelper
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
//设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用
properties.setProperty("offsetAsPageNum","true");
//设置为true时,使用RowBounds分页会进行count查询
properties.setProperty("rowBoundsWithCount","true");
//设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果
//properties.setProperty("pageSizeZero","true");
//禁用合理化,启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页
properties.setProperty("reasonable","false");
//配置mysql数据库的方言
// properties.setProperty("dialect","mysql"); //配置mysql数据库的方言
pageHelper.setProperties(properties);
return pageHelper;
} @Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource, PageHelper pageHelper) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
Resource[] mappers = (new PathMatchingResourcePatternResolver())
.getResources("classpath*:mybatis_mapper/*Mapper.xml");
sessionFactory.setMapperLocations(mappers);
sessionFactory.setPlugins(new Interceptor[]{new PageInterceptor()});
// sessionFactory
// .setPlugins(new Interceptor[] { new MysqlPageReqParamInterceptor(), new MysqlPageResultInterceptor() });
return sessionFactory.getObject();
} @Bean
public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource") DataSource dataSource)
throws Exception {
final DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
} /*@Bean
public TransactionTemplate transactionTemplate(
@Qualifier("dataSourceTransactionManager") DataSourceTransactionManager dataSourceTransactionManager)
throws Exception {
final TransactionTemplate jdbcTemplate = new TransactionTemplate();
jdbcTemplate.setTransactionManager(dataSourceTransactionManager);
return jdbcTemplate;
}*/
}

注意其中的拦截类换成了PageInterceptor

2.取消了配置识别mysql数据库的属性dialect,版本5以后可以自动识别


https://www.ciphermagic.cn/mybatis-page-2.html#Spring%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6

https://www.cnblogs.com/1315925303zxz/p/7364552.html

http://www.imooc.com/article/15451

https://blog.csdn.net/liangwenmail/article/details/77864267

https://github.com/pagehelper/Mybatis-PageHelper/issues/166