Springboot 多数据源整合的三种方式

方式一: 使用Spring提供的AbstractRoutingDataSource

配置文件

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    datasource1:
      url: jdbc:mysql://127.0.0.1:3306/dcytest?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
      username: root
      password: root
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      driver-class-name: com.mysql.cj.jdbc.Driver
    datasource2:
      url: jdbc:mysql://127.0.0.1:3306/drools?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
      username: root
      password: root
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      driver-class-name: com.mysql.cj.jdbc.Driver

主要依赖

       <!-- 阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.1</version>
        </dependency>

核心代码

  • 定义数据源注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DS {
	// 默认数据源
    String value() default "ds1";
}
  • 使用AbstractRoutingDataSource返回当前数据源
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * @description:
 * @author: dcy
 * @create: 2024-01-20 20:27
 */

public class DynamicDataSource extends AbstractRoutingDataSource {


    // 当前使用的数据源标识
    public static ThreadLocal<String> name=new ThreadLocal<>();

    // 返回当前数据源标识
    @Override
    protected Object determineCurrentLookupKey() {
        return name.get();
    }
}
  • 数据源配置
import cn.ac.zing.dyds.ds1.ds.DynamicDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: dcy
 * @create: 2024-01-20 20:22
 */
@Configuration
public class DataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.datasource1")
    public DataSource dataSource1() {
        // 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSource
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.datasource2")
    public DataSource dataSource2() {
        // 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSource
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    public DataSource myRoutingDataSource(@Qualifier("dataSource1") DataSource dataSource1,
                                          @Qualifier("dataSource2") DataSource dataSource2) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("ds1", dataSource1);
        targetDataSources.put("ds2", dataSource2);

        DynamicDataSource myRoutingDataSource = new DynamicDataSource();
        myRoutingDataSource.setDefaultTargetDataSource(dataSource1);
        myRoutingDataSource.setTargetDataSources(targetDataSources);
        return myRoutingDataSource;
    }
}
  • MyBatis xml配置
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @description:
 * @author: dcy
 * @create: 2024-01-20 21:28
 */
@EnableTransactionManagement
@Configuration
public class MyBatisConfig {

    @Resource(name = "myRoutingDataSource")
    private DataSource myRoutingDataSource;


    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(myRoutingDataSource);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager() {
        return new DataSourceTransactionManager(myRoutingDataSource);
    }
}
  • 使用AOP设置需要使用的数据源
import cn.ac.zing.dyds.ds1.annotation.DS;
import cn.ac.zing.dyds.ds1.ds.DynamicDataSource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: dcy
 * @create: 2024-01-20 20:35
 */
@Component
@Aspect
public class DynamicDataSourceAspect {

    // 前置
    @Before("within(cn.ac.zing.dyds.ds1.service.impl.*) && @annotation(ds)")
    public void before(JoinPoint point, DS ds){
        String name = ds.value();
        DynamicDataSource.name.set(name);

        System.out.println(name);
    }
}

使用

  • mapper
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface DSMapper {


    List<Map<String, Object>> selectStudentList();

    List<Map<String, Object>> selectRuleInfoList();

}
  • xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ac.zing.dyds.ds1.mapper.DSMapper">

<select id="selectStudentList" resultType="java.util.Map">
    select * from student
</select>

</mapper>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ac.zing.dyds.ds1.mapper.DSMapper">

<select id="selectRuleInfoList" resultType="java.util.Map">
    select * from rule_info
</select>

</mapper>
  • service
import java.util.List;
import java.util.Map;

public interface DSService {

    List<Map<String, Object>> selectStudentList();

    List<Map<String, Object>> selectRuleInfoList();
}


import cn.ac.zing.dyds.ds1.annotation.DS;
import cn.ac.zing.dyds.ds1.mapper.DSMapper;
import cn.ac.zing.dyds.ds1.service.DSService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: dcy
 * @create: 2024-01-20 20:33
 */
@Service
public class DSServiceImpl implements DSService {

    @Resource
    DSMapper dsMapper;

    @DS("ds1")
    @Override
    public List<Map<String, Object>> selectStudentList() {
        return dsMapper.selectStudentList();
    }

    @DS("ds2")
    @Override
    public List<Map<String, Object>> selectRuleInfoList() {
        return dsMapper.selectRuleInfoList();
    }
}

方式二: 使用MyBatis注册多个SqlSessionFactory

配置文件

server:
  port: 8081
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    datasource1:
      url: jdbc:mysql://127.0.0.1:3306/dcytest?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
      username: root
      password: root
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      driver-class-name: com.mysql.cj.jdbc.Driver
    datasource2:
      url: jdbc:mysql://127.0.0.1:3306/drools?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
      username: root
      password: root
      initial-size: 1
      min-idle: 1
      max-active: 20
      test-on-borrow: true
      driver-class-name: com.mysql.cj.jdbc.Driver

主要依赖

       <!-- 阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.1</version>
        </dependency>

数据源配置

  • 数据源1
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

/**
 * @description:
 * @author: dcy
 * @create: 2024-01-20 21:52
 */
@Configuration
// 继承mybatis:
// 1. 指定扫描的mapper接口包(主库)
// 2. 指定使用sqlSessionFactory是哪个(主库)
@MapperScan(basePackages = "cn.ac.zing.dyds.ds2.mapper.ds1",
        sqlSessionFactoryRef="ds1SqlSessionFactory")
public class DS1Config {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.datasource1")
    public DataSource dataSource1() {
        // 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSource
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public SqlSessionFactory ds1SqlSessionFactory()
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 指定主库
        sessionFactory.setDataSource(dataSource1());
        // 指定主库对应的mapper.xml文件
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/ds1/*.xml"));
        return sessionFactory.getObject();
    }

    @Bean
    @Primary
    public DataSourceTransactionManager ds1TransactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource1());
        return dataSourceTransactionManager;
    }


    @Bean
    public TransactionTemplate ds1TransactionTemplate(){
        return new TransactionTemplate(ds1TransactionManager());
    }
}
  • 数据源2
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
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.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

/**
 * @description:
 * @author: dcy
 * @create: 2024-01-20 21:52
 */
@Configuration
// 继承mybatis:
// 1. 指定扫描的mapper接口包(主库)
// 2. 指定使用sqlSessionFactory是哪个(主库)
@MapperScan(basePackages = "cn.ac.zing.dyds.ds2.mapper.ds2",
        sqlSessionFactoryRef="ds2SqlSessionFactory")
public class DS2Config {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.datasource2")
    public DataSource dataSource2() {
        // 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSource
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public SqlSessionFactory ds2SqlSessionFactory()
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 指定主库
        sessionFactory.setDataSource(dataSource2());
        // 指定主库对应的mapper.xml文件
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/ds2/*.xml"));
        return sessionFactory.getObject();
    }

    @Bean
    @Primary
    public DataSourceTransactionManager ds2TransactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource2());
        return dataSourceTransactionManager;
    }


    @Bean
    public TransactionTemplate ds2TransactionTemplate(){
        return new TransactionTemplate(ds2TransactionManager());
    }
}

使用

  • DS1Mapper
import java.util.List;
import java.util.Map;

public interface DS1Mapper {

    List<Map<String, Object>> selectStudentList();
}

  • DS2Mapper
import java.util.List;
import java.util.Map;

public interface DS2Mapper {

    List<Map<String, Object>> selectRuleInfoList();
}
  • service
import java.util.List;
import java.util.Map;

public interface DSService {

    List<Map<String, Object>> selectStudentList();

    List<Map<String, Object>> selectRuleInfoList();
}

import cn.ac.zing.dyds.ds2.mapper.ds1.DS1Mapper;
import cn.ac.zing.dyds.ds2.mapper.ds2.DS2Mapper;
import cn.ac.zing.dyds.ds2.service.DSService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: dcy
 * @create: 2024-01-20 20:33
 */
@Service
public class DSServiceImpl implements DSService {

    @Resource
    DS1Mapper ds1Mapper;

    @Resource
    DS2Mapper ds2Mapper;


    @Override
    public List<Map<String, Object>> selectStudentList() {
        return ds1Mapper.selectStudentList();
    }

    @Override
    public List<Map<String, Object>> selectRuleInfoList() {
        return ds2Mapper.selectRuleInfoList();
    }
}

  • xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ac.zing.dyds.ds2.mapper.ds1.DS1Mapper">

<select id="selectStudentList" resultType="java.util.Map">
    select * from student
</select>

</mapper>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ac.zing.dyds.ds2.mapper.ds2.DS2Mapper">

<select id="selectRuleInfoList" resultType="java.util.Map">
    select * from rule_info
</select>

</mapper>

方式三: 使用dynamic-datasource框架

配置文件

server:
  port: 8081
spring:
  datasource:
    dynamic:
      #设置默认的数据源或者数据源组,默认值即为datasource1
      primary: ds1
      #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      strict: false
      datasource:
        ds1:
          url: jdbc:mysql://127.0.0.1:3306/dcytest?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
          username: root
          password: root
          initial-size: 1
          min-idle: 1
          max-active: 20
          test-on-borrow: true
          driver-class-name: com.mysql.cj.jdbc.Driver
        ds2:
          url: jdbc:mysql://127.0.0.1:3306/drools?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
          username: root
          password: root
          initial-size: 1
          min-idle: 1
          max-active: 20
          test-on-borrow: true
          driver-class-name: com.mysql.cj.jdbc.Driver

#配置mapper xml文件的路径
mybatis:
  mapper-locations: mapper/**/*.xml

核心依赖

    <dependencies>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.5.0</version>
        </dependency>
    </dependencies>

使用

  • Mapper
import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

@Mapper
public interface DSMapper {


    @DS("ds1")
    List<Map<String, Object>> selectStudentList();

    @DS("ds2")
    List<Map<String, Object>> selectRuleInfoList();

}
  • service
import java.util.List;
import java.util.Map;

public interface DSService {

    List<Map<String, Object>> selectStudentList();

    List<Map<String, Object>> selectRuleInfoList();
}

import cn.ac.zing.dyds.ds3.mapper.DSMapper;
import cn.ac.zing.dyds.ds3.service.DSService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: dcy
 * @create: 2024-01-20 20:33
 */
@Service
public class DSServiceImpl implements DSService {

    @Resource
    DSMapper dsMapper;

    @Override
    public List<Map<String, Object>> selectStudentList() {
        return dsMapper.selectStudentList();
    }

    @Override
    public List<Map<String, Object>> selectRuleInfoList() {
        return dsMapper.selectRuleInfoList();
    }
}
  • xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ac.zing.dyds.ds3.mapper.DSMapper">

<select id="selectStudentList" resultType="java.util.Map">
    select * from student
</select>

</mapper>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.ac.zing.dyds.ds3.mapper.DSMapper">

<select id="selectRuleInfoList" resultType="java.util.Map">
    select * from rule_info
</select>

</mapper>
springboot可以通过使用dynamic-datasource-spring-boot-starter来实现多数据源整合。该启动器基于springboot,可以快速集成多个数据源。一般的思路是根据自定义的配置数据源信息初始化数据源,并使用druid连接池和mybatis进行相关配置。引用 具体步骤可以包括以下几个方面: 1. 引入相关依赖:在项目的pom.xml文件中添加dynamic-datasource-spring-boot-starter、druid-spring-boot-starter和mybatis-spring-boot-starter等依赖。引用 2. 配置数据源信息:在application.yml或application.properties文件中配置多个数据源的信息,包括数据库的连接地址、用户名、密码等。可以使用@ConfigurationProperties注解来绑定配置文件中的数据源信息到对应的实体类中。 3. 初始化数据源:使用Configuration类来初始化数据源。可以自定义一个DynamicDataSourceConfig类,并在其中使用@Bean注解来配置数据源。在该类中可以通过读取配置文件的方式来获取数据源信息,并将其初始化为对应的数据源。 4. 配置mybatis:在application.yml或application.properties文件中配置mybatis的相关信息,如mapper的扫描路径等。 通过以上步骤,就可以实现springboot多数据源整合。当然,以上只是一个基本的思路,具体实现还需要根据项目的实际需求进行调整和扩展。引用<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot实现多数据源的两种方式](https://blog.csdn.net/m0_67401761/article/details/126114612)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot整合多数据源的两种方式](https://blog.csdn.net/hongs468/article/details/128469985)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值