MyBatis-Plus 分页插件失效分析

https://blog.csdn.net/qq_36241003/article/details/100056609

描述

项目中用到boot 整合 mybatis-plus , 个人在使用分页条件查询的时候一直查不出 total, pages, 终于找到原因了.

环境

<springboot.version>2.1.5.RELEASE</springboot.version>
<mybatisplus.version>3.1.1</mybatisplus.version>

配置

  1. 自定义MybatisPlusConfig 配置分页插件
    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
    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. 执行分页查询
    Page<UserInfoDto> page = new Page<>(currentPage, pageSize);
            QueryWrapper<UserInfoDto> userInfoDtoQueryWrapper = new QueryWrapper<>();
            userInfoDtoQueryWrapper.groupBy("tui.id ");
            IPage<UserInfoDto> userInfoDtoIPage = baseMapper.selectAllUserInfoDtoByPage(page, userInfoDtoQueryWrapper);

     

  4. 查看数据

         

 

 

 如果使用mybatisplus-starter,当MybatisPlusAutoConfiguration生效时会添加分页interceptors(前提DataSourceAutoConfiguration生效)

 

posted @ 2020-02-21 00:36  zbjice  阅读(3284)  评论(0编辑  收藏