MyBatis-Plus分页插件不生效的解决方法

2025-05-29 0 90

描述

项目中用到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分页插件不生效的解决方法

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分页不生效内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/qq_36241003/article/details/100056609

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 MyBatis-Plus分页插件不生效的解决方法 https://www.kuaiidc.com/116533.html

相关文章

发表评论
暂无评论