码迷,mamicode.com
首页 > 数据库 > 详细

数据库双数据源配置

时间:2020-07-24 10:04:17      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:name   一个   UNC   zone   rtt   perl   content   public   ack   

数据库双数据源配置

配置文件添加2个数据库的配置信息

#第一数据源
spring.datasource.primary.jdbc-url=jdbc:sqlserver://127.0.0.1\\FANGRONG:1433;database=dataserver
spring.datasource.primary.username=aaa
spring.datasource.primary.password=123456
spring.datasource.primary.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.primary.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.primary.minimum-idle=1
spring.datasource.primary.maximum-pool-size=15
spring.datasource.primary.auto-commit=true
spring.datasource.primary.idle-timeout=60000
spring.datasource.primary.pool-name=fang_sqlserver
spring.datasource.primary.max-lifetime=1800000
spring.datasource.primary.connection-timeout=30000
spring.datasource.primary.connection-test-query=SELECT 1

#第二数据源
spring.datasource.secondary.jdbc-url=jdbc:mysql://127.0.0.1:3306/reptie?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&autoReconnect=true
spring.datasource.secondary.username=bbb
spring.datasource.secondary.password=123456
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.secondary.minimum-idle=1
spring.datasource.secondary.maximum-pool-size=15
spring.datasource.secondary.auto-commit=true
spring.datasource.secondary.idle-timeout=60000
spring.datasource.secondary.pool-name=fang_mysql
spring.datasource.secondary.max-lifetime=1800000
spring.datasource.secondary.connection-timeout=30000
spring.datasource.secondary.connection-test-query=SELECT 1

配置双数据源的配置类

第一个

import com.zaxxer.hikari.HikariDataSource;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author PangG
 * @MapperScan 扫描mapper所在路径
 */
@Configuration
@MapperScan(basePackages = "cn.mapper.mysql", sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MySqlDataSourceConfig {

    /**
     * @Bean 注册Bean对象
     * @ConfigurationProperties 读取properties中的配置参数映射成为一个对象
     */
    @Bean(name = "mysqlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public HikariDataSource getMysqlDateSource() {
        return new HikariDataSource();
    }

    /**
     * @param datasource 数据源
     * @return SqlSessionFactory
     */
    @Bean(name = "mysqlSqlSessionFactory")
    public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        //mybatis扫描xml所在位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/mysql/*.xml"));
        return bean.getObject();
    }

    @Bean("mysqlSessionTemplate")
    public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

第二个

import com.zaxxer.hikari.HikariDataSource;
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.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;

/**
 * @author PangG
 * @MapperScan 扫描mapper所在路径
 */
@Configuration
@MapperScan(basePackages = "mapper.sqlserver", sqlSessionFactoryRef = "sqlserverSqlSessionFactory")
public class SqlserverDataSourceConfig {

    /**
     * @Bean 注册Bean对象
     * @Primary 表示默认数据源
     * @ConfigurationProperties 读取properties中的配置参数映射成为一个对象
     */
    @Bean(name = "sqlserverDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public HikariDataSource getSqlserverDateSource() {
        return new HikariDataSource();
    }

    /**
     * @param datasource 数据源
     * @return SqlSessionFactory
     * @Primary 默认SqlSessionFactory
     */
    @Bean(name = "sqlserverSqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlserverSqlSessionFactory(@Qualifier("sqlserverDataSource") DataSource datasource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        //mybatis扫描xml所在位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/sqlserver/*.xml"));
        return bean.getObject();
    }

    @Bean("sqlserverSessionTemplate")
    @Primary
    public SqlSessionTemplate sqlserverSqlSessionTemplate(@Qualifier("sqlserverSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

注意配置类配置了扫描路径一定要关闭配置文件中的的扫描

#mybatis.mapper-locations=classpath:mapper//.xml

 来源:锌闻网

数据库双数据源配置

标签:name   一个   UNC   zone   rtt   perl   content   public   ack   

原文地址:https://www.cnblogs.com/1994jinnan/p/13369457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!