mybatis-plus+druid配置多套数据源

这里我使用的是mysql和postgresql进行配置,详细讲讲会遇到的问题

1、首先引入需要用到的依赖

<!--        Mybatis-plus依赖-->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.2</version>
		</dependency>
<!--  postgresql依赖-->
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.21</version>
		</dependency>
		<!--  数据库连接池  -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.2.9</version>
		</dependency>

2、然后就可以开始配置了,先在yml配置两套数据源

spring:
  datasource:
    postgresql:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: org.postgresql.Driver
      jdbc-url: jdbc:postgresql://localhost:5433/xxxx?useUnicode=true&characterEncoding=utf8&useSSL=true
      username: 
      password: 
    mysql:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://localhost:3306/xxxx?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true
      username: 
      password:

这里注意url一定要写成jdbc-url,否则等会报错找不到url

3、编写MybatisPlusConfig文件,等下要用,防止配置多数据源后mybatis-plus配置失效

@Configuration
public class MybatisPlusConfig {
    @Scope("prototype") 
    @Bean(name = "globalConfig")
    //读取yml中mp的global-config配置
    @ConfigurationProperties(prefix = "mybatis-plus.global-config") 
    public GlobalConfig globalConfig(){
        GlobalConfig globalConfig = new GlobalConfig();
        return globalConfig;
    }
    @Scope("prototype")
    @Bean(name = "configuration")
    //读取yml中mp的configuration配置
    @ConfigurationProperties(prefix = "mybatis-plus.configuration")
    public MybatisConfiguration configuration(){
        MybatisConfiguration configuration = new MybatisConfiguration();
        return configuration;
    }
}

4、编写两个数据源配置类
mysql数据源配置

@Configuration
//填写自己的mapper接口路径提供扫描
@MapperScan(basePackages = "com.xxx.xxx.mapper", sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
public class MysqlDataSource {
    @Autowired
    private MybatisConfiguration configuration;
    @Autowired
    private GlobalConfig globalConfig;

    @Bean("mysqlDataSource") //省略则是默认方法名为注册的bean名称
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.mysql")//设置配置
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("mysqlSqlSessionFactory")
    @Primary
    public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
        //注意使用mybatis-plus需要用MybatisSqlSessionFactoryBean而不是SqlSessionFactoryBean
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        //设置数据源
        bean.setDataSource(dataSource);
        //设置mp全局配置
        bean.setGlobalConfig(globalConfig);
        //设置mp通用配置
        bean.setConfiguration(configuration);
        //设置对应的xml文件位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql/*.xml"));
        return bean.getObject();
    }

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

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

postgresql数据源配置

@Configuration
//填写自己dmapper接口路径提供扫描
@MapperScan(basePackages = "com.xxx.xxx.mapper", sqlSessionTemplateRef = "postgresqlSqlSessionTemplate")
public class PostgresqlDataSource {
    @Autowired
    private MybatisConfiguration configuration;
    @Autowired
    private GlobalConfig globalConfig;

    @Bean("postgresqlDataSource") //省略则是默认方法名为注册的bean名称
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.postgresql")//设置配置
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("postgresqlSqlSessionFactory")
    @Primary
    public SqlSessionFactory postgresqlSqlSessionFactory(@Qualifier("postgresqlDataSource") DataSource dataSource) throws Exception {
        //注意使用mybatis-plus需要用MybatisSqlSessionFactoryBean而不是SqlSessionFactoryBean
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        //设置数据源
        bean.setDataSource(dataSource);
        //设置mp全局配置
        bean.setGlobalConfig(globalConfig);
        //设置mp通用配置
        bean.setConfiguration(configuration);
        //设置对应的xml文件位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/postgresql/*.xml"));
        return bean.getObject();
    }

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

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

至此多数据源配置完成,接下来就能正常使用mybatis-plus,正常访问两个数据库了

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值