数据库双数据源配置
配置文件添加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