springboot+mybatis多数据源

1、配置文件

spring.datasource.primary.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.primary.jdbc-url=jdbc:mysql:///project_datahub?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
spring.datasource.primary.username=root
spring.datasource.primary.password=root
 
spring.datasource.secondary.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.jdbc-url=jdbc:mysql:///project_datahub_second?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root

2、DataSourceConfig 配置

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
 
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @Description
 * @Author lxk
 * @version V1.0.0
 */
@Configuration
public class DataSourceConfig {
 
    @Primary
    @Bean(name = "firstDataSource")
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource firstDataSource() {
        System.out.println("primary db built");
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name = "secondDataSource")
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public DataSource secondDataSource() {
        System.out.println("secondary db built");
        return DataSourceBuilder.create().build();
    }
  
   
}

3、各个数据源单独的配置

firstDataSource

@Configuration
@MapperScan(
        basePackages = "com.it.iais.mapper.first",//mapper.java包路径
        sqlSessionFactoryRef = "firstSqlSessionFactory")
public class PrimaryConfig {

    @Autowired
    @Qualifier("firstDataSource")
    private DataSource firstDataSource;

    private final static String mapper_location="classpath:mapper/first/*/*.xml";
  
    @Bean
    @Primary
    public SqlSessionFactory firstSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(firstDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper_location));
        bean.getObject().getConfiguration().setCallSettersOnNulls(true);
        //开启驼峰转化,主要是为了解决复杂的sql对应的字段对应不上的问题
        bean.getObject().getConfiguration.setMapUnderscoreToCamelCase(true);
        return bean.getObject();
    }

    @Bean
    @Primary
    public SqlSessionTemplate firstSqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(firstSqlSessionFactory());
    }

}

secondDataSource

@Configuration
@MapperScan(
        basePackages = "com.it.iais.mapper.second",//mapper.java包路径
        sqlSessionFactoryRef = "secondSqlSessionFactory")
public class PrimaryConfig {

    @Autowired
    @Qualifier("secondDataSource")
    private DataSource secondDataSource;

    private final static String mapper_location="classpath:mapper/second/*/*.xml";
  
    @Bean
    public SqlSessionFactory secondSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(secondDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper_location));
        bean.getObject().getConfiguration().setCallSettersOnNulls(true);
        //开启驼峰转化,主要是为了解决复杂的sql对应的字段对应不上的问题
        bean.getObject().getConfiguration.setMapUnderscoreToCamelCase(true);
        return bean.getObject();
    }

    @Bean
    public SqlSessionTemplate secondSqlSessionTemplate() throws Exception {
        return new SqlSessionTemplate(secondSqlSessionFactory());
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值