spring boot多数据源配置示例

1. application.properties

#\u4E3B\u5E93\u914D\u7F6E
spring.datasource.primary.url=jdbc:mysql://mysql.test.xxxxx.com:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.primary.type=com.zaxxer.hikari.HikariDataSource


spring.datasource.secondary.url=jdbc:mysql://mysql.log.test.xxxx.com:3306/test_log?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.secondary.type=com.zaxxer.hikari.HikariDataSource

2. 配置类

2.1 datasource配置

import javax.sql.DataSource;

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.context.annotation.Primary;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
public class DataSourceConfig {
    @Bean
    @Qualifier("primaryDataSourceProperties")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSourceProperties primaryDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Autowired
    @Qualifier("primaryDataSourceProperties")
    private DataSourceProperties primaryDataSourceProperties;

    @Bean
    @Qualifier("primaryDataSource")
    @Primary
    public DataSource primaryDataSource() {
        return primaryDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }

    //

    @Bean
    @Qualifier("secondaryDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSourceProperties secondaryDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Autowired
    @Qualifier("secondaryDataSourceProperties")
    private DataSourceProperties secondaryDataSourceProperties;

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    public DataSource secondaryDataSource() {
        return secondaryDataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }


}

2.2 主库配置

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/*
 * 主库的配置
 */
@Configuration
@MapperScan(basePackages = "com.xxxx.xxxx.mapper",sqlSessionFactoryRef = "primarySqlSessionFactory")
public class DatabaseConfig {
    @Autowired
    @Qualifier("primaryDataSource") 
    private DataSource primaryDataSource;
    
    @Bean("primarySqlSessionFactory")
    @Qualifier("primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(primaryDataSource);
        return sessionFactoryBean.getObject();
    }
} // class DatabaseConfig

2.3 日志库配置

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
 * 主库的配置
 */
@Configuration
@MapperScan(basePackages = "com.xxxx.xxxx.logmapper",sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class LogDatabaseConfig{
    
    @Autowired
    @Qualifier("secondaryDataSource") 
    private DataSource secondaryDataSource;
    
    @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(secondaryDataSource);
        return sessionFactoryBean.getObject();
    }
} // class LogDatabaseConfig

3. 使用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值