myabtisPlus双数据源使用分页插件 2020-12-06

  • 版本:

版本不同有可能报错!!!!

SpringBoot:  2.1.8.RELEASE
mybatis-plus-boot-starter:  3.3.2


  • 首先配置MybatisPlus配置类:


import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * MybatisConfig配置类
 *
 * @author 陈定雄
 * @date 2019/10/26 0:31
 */
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
@MapperScan("mapper包路径1(使用.分级)")
@MapperScan("mapper路径2")
public class MybatisPlusConfig {

    //设置分页的bean=====================================================================================================
    @Bean
    public PaginationInterceptor paginationInterceptor() {

        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }


    @Configuration
    @MapperScan(basePackages = "mapper包路径2(使用.分级)", sqlSessionTemplateRef = "sqlSessionTemplate")
    public class Db1 {
        @Bean
        @Primary
        public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
            //mybatis应该使用  SqlSessionFactoryBean
            //SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

            //mybatis-plus应该使用  MybatisSqlSessionFactoryBean  否则无法使用baseMapper
            MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
            sqlSessionFactory.setDataSource(dataSource);
            sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath:mapper包路径(使用 / 分级)/*.xml"));
            //添加分页功能================================================================================================
            sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor()});
            return sqlSessionFactory.getObject();
        }

        @Bean
        @Primary
        public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }

        @Bean
        @Primary
        public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    }

    @Configuration
    @MapperScan(basePackages = "mapper包路径1(使用.分级)", sqlSessionTemplateRef = "sqlSessionTemplate2")
    public class Db2 {

        @Bean
        public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
            //mybatis应该使用  SqlSessionFactoryBean
            //SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

            //mybatis-plus应该使用  MybatisSqlSessionFactoryBean  否则无法使用baseMapper
            MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
            sqlSessionFactory.setDataSource(dataSource);
            sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath:mapper包路径(使用 / 分级)/*.xml"));
            //添加分页功能================================================================================================
            sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor()});
            return sqlSessionFactory.getObject();
        }
        @Bean
        public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }

        @Bean
        public DataSourceTransactionManager dataSourceTransactionManager2(@Qualifier("dataSource2") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    }
}


  • Controller

controller接收前端穿来的页数,条数,装入Page<属性类的名称>对象,将分页的Page对象与需要查询的条件的对象穿入service


@ApiOperation(value = "")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "分页的页数"),
                        @ApiImplicitParam(name = "limit", value = "需要查询的记录条数")})
    @RequestMapping(value = "/路径/{current}/{limit}", method = RequestMethod.POST)
    public List<属性类> 方法名(@PathVariable Long current, @PathVariable Long limit, @RequestBody QueryHeader queryHeader) {

        Page<属性类> Page = new Page<>(current, limit);//参数一是当前页,参数二是每页数据的条数

        service层.queryConditionPage(Page,queryHeader);

        return Page.getRecords();
    }


  • service

service再调用mapper,mapper会返回一个IPage<属性对象>对象

    @Override
    public IPage<属性类> queryConditionPage(Page<属性类> Page, QueryHeader queryHeader) {
        if(queryHeader==null){
            mapper.selectPage(shuxing,queryHeader);
        }
        IPage<属性类> Pages = mapper.selectPage(Page, queryHeader);

        return Pages;
    }

  • mapper

mapper再执行sql,数据会自动封装到在controller创建出来的Page对象中,所以在controller直接调用Page.getRecords()方法就可以获取到已经分页好的数据了

  • mapper接口

    IPage<属性类> selectPage(Page<属性类> page, @Param("queryHeader")QueryHeader queryHeader);

  • sql




你可能感兴趣的:(myabtisPlus双数据源使用分页插件 2020-12-06)