SpringBoot相关系列:多数据源配置

目录:

第一种:mybatis 多数据源配置

第二种:mybatis-plus多数据源配置

第一种:mybatis

阿咚的多数据实现是使用配置类,以及yml文件的方式,ORM使用mybatis 话不多说直接上代码。

数据源1配置类

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
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 tk.mybatis.spring.annotation.MapperScan;
数据源1

@Configuration
 @MapperScan(basePackages = "com.**.mapper.aDong1",sqlSessionFactoryRef = "aDong1SqlSessionFactory")
 public class aDong1DataSourceConfig {

   @Primary //默认数据源
   @Bean("DaibaiDataSource")
   @ConfigurationProperties(prefix = "spring.datasource.dynamic.aDong1") //yml配置文件
   public DataSource getaDong1DataSource(){
     return DataSourceBuilder.*create*().build();
   }

   @Primary
   @Bean("aDong1SqlSessionFactory")
   public SqlSessionFactory aDong1SqlSessionFactory(@Qualifier("DaibaiDataSource") DataSource dataSource) throws Exception {
     SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
     bean.setDataSource(dataSource);
     // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
     bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/mapper/aDong1/**/*.xml"));
     return bean.getObject();
   }


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

数据源2

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
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 tk.mybatis.spring.annotation.MapperScan; 
//数据源2

@Configuration
 @MapperScan(basePackages = "com.*.mapper.sso",sqlSessionFactoryRef = "ssoSqlSessionFactory")
 public class aDong2SourceConfig {


   @Bean("aDong2Source")
   @ConfigurationProperties(prefix = "spring.datasource.dynamic.sso") //yml配置文件
   public DataSource getaDong1DataSource(){
     return DataSourceBuilder.*create*().build();
   }


   @Bean("aDong2SqlSessionFactory")
   public SqlSessionFactory aDong2SqlSessionFactory(@Qualifier("aDong2Source") DataSource dataSource) throws Exception {
     SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
     bean.setDataSource(dataSource);
     // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
     bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/mapper/aDong2/**/*.xml"));
     return bean.getObject();
   }
   @Bean("aDong2SqlSessionTemplate")
   public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("aDong2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
     return new SqlSessionTemplate(sqlSessionFactory);
   }
 }

yml文件配置: 根据自己情况填上 信息

datasource:
  dynamic:
   primary: dabai *#**设置默认数据源
\*   aDong1:
    jdbc-url: 
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: 
    password: 
 
   aDong2:
    jdbc-url: 
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: 
    password: 

使用的时候跟单数据源一样,只要直接使用对应表的mapper就直接可以。

第二种 mybatis-plus 多数据源配置


mybatis-plus是mybatis的增强版,只增加,不影响。也就是说使用mybatis-plus兼容原来所有的mybatis代码和配置。然后又做了很多增加和简化使用,具体看官网教程https://mybatis.plus/

相对于mybatis的多数据源配置就是改了下 SqlSessionFactory
核心代码就是修改mybatis为mybatis-plus,如下

数据源1:

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
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.autoconfigure.jdbc.DataSourceProperties;
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 javax.sql.DataSource;

/**
 * 数据源 aDong1 库aDong-back
 */
@Configuration
@MapperScan(basePackages = "com.aDong.**.mapper",sqlSessionFactoryRef = "aDong1SqlSessionFactory")
public class aDong1DataSourceConfig {


    @Primary //默认数据源
    @Bean("aDong1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.aDong1") //yml配置文件
    public DataSource getaDong1DataSource(){
         return  DataSourceBuilder.create().build();
    }

    @Primary
    @Bean("aDong1SqlSessionFactory")
    public SqlSessionFactory aDong1SqlSessionFactory(@Qualifier("aDong1DataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        bean.setConfiguration(configuration);
        // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath*:com/aDong/ai/common/mapper/**/*.xml"));
        bean.setPlugins(new Interceptor[]{
                new PaginationInterceptor()
        });
        bean.setGlobalConfig(new GlobalConfig().setBanner(false));

        return bean.getObject();
    }


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

数据源2

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.MybatisXMLLanguageDriver;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
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.autoconfigure.jdbc.DataSourceProperties;
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 javax.sql.DataSource;
/**
 * 数据源 aDong2  库riskeys
 */
@Configuration
@MapperScan(basePackages = "com.riskeys.**.aDong2mapper",sqlSessionFactoryRef = "aDong2SqlSessionFactory")
public class aDong2DataSourceConfig {

    @Bean("aDong2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.aDong2") //yml配置文件
    public DataSource getDabaiDataSource(){
         return  DataSourceBuilder.create().build();
    }

    @Bean("aDong2SqlSessionFactory")
    public SqlSessionFactory aDong2SqlSessionFactory(@Qualifier("aDong2DataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
        configuration.setJdbcTypeForNull(JdbcType.NULL);
        bean.setConfiguration(configuration);
        // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath*:com/riskeys/ai/common/aDong2mapper/*.xml"));
        bean.setPlugins(new Interceptor[]{
                new PaginationInterceptor()
        });
        bean.setGlobalConfig(new GlobalConfig().setBanner(false));
        return bean.getObject();
    }

    @Bean("aDong2SqlSessionTemplate")
    public SqlSessionTemplate aDong2SqlSessionTemplate(@Qualifier("aDong2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值