SpringBoot 通过配置类的方式配置 MybatisPlus

有时候因为某些原因,配置文件的配置无法满足我们的要求(如2个mapper要查询不同的数据库)可以通过配置类的形势配置2个mapper查询不同的数据库:

示例:有3个mapper接口要查询不同的数据库

1 配置文件配置3个mapper的数据库配置

spring:
  datasource:
    db1:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/db1?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
      username: root
      password: root
    db2:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/db2?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
      username: root
      password: root
    db3:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/db3?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
      username: root
      password: root

2 新增加3个配置类配置

@Configuration
@MapperScan(basePackages = "org.gjw.mapper.db1",sqlSessionFactoryRef = "db1SqlSessionFactory")
public class MybatisPlusConfigDb1 {

    /**
     * 读取 spring.datasource.db1的数据库配置
     **/
    @Bean("db1Datasource")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource db1Datasource(){
        return new HikariDataSource();
    }

    @Bean("db1TransactionManager")
    public TransactionManager transactionManager(@Qualifier("db1Datasource") DataSource db1Datasource){
        //设置 transactional 事务
        return new DataSourceTransactionManager(db1Datasource);
    }

    @Bean("db1SqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("db1Datasource") DataSource db1Datasource) throws Exception {
        //注意这里是mybatis-plus内的 SqlSessionFactoryBean
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        //设置datasource
        sqlSessionFactoryBean.setDataSource(db1Datasource);
        //获取mapper资源 
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/**/*.xml"); 
        //设置mapper的位置,注意3个mepper对应的xml得分别放在3个不同的包
        sqlSessionFactoryBean.setMapperLocations(resources);

        //其他配置 ps:sqlSessionFactoryBean内可以配置yml的一切配置
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        //是否下划线转驼峰
        mybatisConfiguration.setMapUnderscoreToCamelCase(false);
        //设置日志
        mybatisConfiguration.setLogImpl(StdOutImpl.class);
        //配置别名包
        sqlSessionFactoryBean.setTypeAliasesPackage("org.gjw.bean.db1"); 

        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration);

        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setBanner(false);
        sqlSessionFactoryBean.setGlobalConfig(globalConfig);

        return  sqlSessionFactoryBean.getObject();
    }


    @Bean("db1SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory ){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

其余2个参考则配置

posted @ 2021-04-24 16:45  gudian  阅读(99)  评论(0编辑  收藏  举报