描述
項目中用到boot 整合 mybatis-plus , 個人在使用分頁條件查詢的時候一直查不出 total, pages, 終于找到原因了.
環境
1
2
|
< springboot.version >2.1.5.RELEASE</ springboot.version > < mybatisplus.version >3.1.1</ mybatisplus.version > |
配置
1.自定義MybatisPlusConfig 配置分頁插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.eyelake.smart.park.portal.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author: LiangJingXing * @Date: 2019/8/21 19:38 * @Decription: MybatisPlus 配置分頁 性能分析 */ @Configuration @MapperScan ( "com.eyelake.smart.park.portal.mapper.park" ) public class MybatisPlusConfig { /** * 分頁插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor().setDialectType( "mysql" ); } } |
2.自定義的DataSourceConfig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class DataSourceConfig { @Autowired private PaginationInterceptor paginationInterceptor; ... @Primary @Bean (name = "helmetSqlSessionFactory" ) public SqlSessionFactory helmetSqlSessionFactory( @Qualifier ( "helmetDataSource" ) DataSource helmetDataSource) throws Exception { MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean(); sqlSessionFactory.setDataSource(helmetDataSource); ... // 關鍵代碼 設置 MyBatis-Plus 分頁插件 Interceptor[] plugins = {paginationInterceptor}; sqlSessionFactory.setPlugins(plugins); ... return sqlSessionFactory.getObject(); } } |
3.執行分頁查詢
1
2
3
4
|
Page<UserInfoDto> page = new Page<>(currentPage, pageSize); QueryWrapper<UserInfoDto> userInfoDtoQueryWrapper = new QueryWrapper<>(); userInfoDtoQueryWrapper.groupBy( "tui.id " ); IPage<UserInfoDto> userInfoDtoIPage = baseMapper.selectAllUserInfoDtoByPage(page, userInfoDtoQueryWrapper); |
4.查看數據
mybatis plus分頁不出來pages和total的解決記錄
按著官方的分頁例子寫完以后,發現pages和total都為0,仔細觀察了好多遍還是沒解決。
最好找到一段配置添加后,正常了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package kulink.cvscloud.core.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.baomidou.mybatisplus.plugins.PaginationInterceptor; @Configuration public class MybatisPlusConfig { /** * mybatis-plus分頁插件 */ @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor page = new PaginationInterceptor(); page.setDialectType( "mysql" ); return page; } } |
到此這篇關于MyBatis-Plus分頁插件不生效的解決方法的文章就介紹到這了,更多相關MyBatis-Plus分頁不生效內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/qq_36241003/article/details/100056609