springboot手动配置多数据源

文章目录

yml

spring:
  datasource: # 数据库链接
    otc:
      jdbc-url: jdbc:mysql://x.x.x.x:3306/db1_v1?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
      username: root
      password: ENC(0BB79lsvpyt/Vj4fEVfITHOMGu7YuLf0)  #数据库名、用户名和密码改为自己的
      driver-class-name: com.mysql.cj.jdbc.Driver
      mapper-locations: classpath*:mapper/otcmapper/*.xml
    db2:
      jdbc-url: jdbc:sqlserver://x.x.x.x:1433;DatabaseName=db2_v1
      username: root
      password: 123456  #数据库名、用户名和密码改为自己的
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      mapper-locations: classpath*:mapper/plmmapper/*.xml

配置类

启动类添加注解参数,排除自动配置数据源
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
配置第一个数据源
其余的数据源按照以下规则再写配置类即可

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
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.beans.factory.annotation.Value;
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 org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com.batch.otc.otc.mapper"}, sqlSessionFactoryRef = "otcSqlSessionFactory")
public class OtcDataSourceConfig {
    @Value("${spring.datasource.otc.mapper-locations}")
    private String otcMapperLocation;

    @Primary // 表示这个数据源是默认数据源, 这个注解必须要加,因为不加的话spring将分不清楚那个为主数据源(默认数据源)
    @Bean("otcDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.otc") //读取application.yml中的配置参数映射成为一个对象
    public DataSource primaryDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("otcSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("otcDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        MybatisConfiguration mybatisConfiguration=new MybatisConfiguration();
//        mybatisConfiguration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);//日志打印
        mybatisConfiguration.setMapUnderscoreToCamelCase(true);// 驼峰
        mybatisConfiguration.setObjectWrapperFactory(new MybatisMapWrapperFactory());// map结果也转驼峰
        sqlSessionFactory.setConfiguration(mybatisConfiguration);
        sqlSessionFactory.setDataSource(dataSource); //数据源
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactory.setMapperLocations(resolver.getResources(otcMapperLocation));
        sqlSessionFactory.setTypeAliasesPackage("com.batch.otc.otc.entity");
        sqlSessionFactory.setGlobalConfig(globalConfig());
        sqlSessionFactory.setPlugins(new Interceptor[]{mybatisPlusInterceptor()});
        return sqlSessionFactory.getObject();
    }
    @Bean("otcSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("otcSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setLogicDeleteValue("Y");
        dbConfig.setLogicNotDeleteValue("N");
        globalConfig.setDbConfig(dbConfig);
        return globalConfig;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可——叹——落叶飘零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值