MyBatisPlus分页查询
MybatisPlus 本身具有配置多数据源 和分页查询 的功能
@Slf4j @Configuration public class MybatisConfiguration { /** * 分页插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
若自定义多数据源 则需要在 MybatisSqlSessionFactoryBean 添加拦截器
package com.dcsyun.base.ability.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; @Configuration @MapperScan(basePackages = {"com.zhengcj.demo.dao.devicecontrol"}, sqlSessionTemplateRef = "template1") public class DataSource1Config { @Autowired private MybatisPlusInterceptor plusInterceptor; @Bean(name = "db1") @ConfigurationProperties(prefix = "spring.datasource.db1") public DruidDataSource druidDataSource1() { return DruidDataSourceBuilder.create().build();//按默认配置就行了。 } @Bean(name = "factory1") public SqlSessionFactory sqlSessionFactory(@Qualifier("db1") DruidDataSource dataSource) throws Exception { MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); factoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/db1/**Mapper.xml")); factoryBean.setPlugins(plusInterceptor); return factoryBean.getObject(); } @Bean(name = "template1") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("factory1") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }