SpringBoot配置多数据源

引言 如何在实际项目中区分多数据源

当在项目使用多数据源时,会产生事务管理,数据源区分等问题,下面以test、test2作为两个数据源进行讲解。
  1. 分包结构
    cn.impassive.test:访问test数据库;
    cn.impassive.test2:访问test2数据库;
  2. 注解方式
    在方法上添加注解进行判断
    @DataSourcetest001(自定义注解)
    public void test(){}

下面的例子是以分包结构为例。

1、修改application.properties配置文件

spring.datasource.test.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.test.jdbc-url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.test.username=root
spring.datasource.test.password=123456

spring.datasource.test1.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.test1.username=root
spring.datasource.test1.password=123456

2、配置数据源

创建一个包,取名为datasource,用于存放数据源的配置代码。
DataSource1Config:

package cn.impassive.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
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.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration  //注册到springboot容器中
@MapperScan(basePackages = {"cn.impassive.test"},sqlSessionFactoryRef = "testSqlSessionFactory") //在cn.impassive.test包下的数据库访问,使用此数据源
public class Datasource1Config {

    @Bean(name = "testDatasource")
    @ConfigurationProperties(prefix = "spring.datasource.test")  //配置前缀,对应application.properties文件中的前缀。
    public DataSource testDatasource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "testSqlSessionFactory")
    @Primary   //设置为默认的数据源,如果不添加该注解,在启动项目时会提示找不到默认的注解
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDatasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

        bean.setDataSource(dataSource);

        return bean.getObject();
    }

    @Bean(name = "testTransactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager(@Qualifier("testDatasource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "testSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

DataSource2Config

package cn.impassive.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
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.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration  //注册到springboot容器中
@MapperScan(basePackages = {"cn.impassive.test1"},sqlSessionFactoryRef = "test1SqlSessionFactory") //在cn.impassive.test1的数据库访问使用此数据源
public class Datasource2Config {

    @Bean(name = "test1Datasource")
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    public DataSource test11Datasource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test1SqlSessionFactory")
    public SqlSessionFactory test11SqlSessionFactory(@Qualifier("test1Datasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

        bean.setDataSource(dataSource);

        return bean.getObject();
    }

    @Bean(name = "test1TransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("test1Datasource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test1SqlSessionTemplate")
    public SqlSessionTemplate test1SqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

至此,数据源配置完成。

3、访问数据库

创建test,test1包,在包下面创建Dao,进行数据库访问。然后分别是用不同包下的Dao访问数据库即可。

你可能感兴趣的:(Mybatis,SpringBoot,多数据源)