本文采用不同包路径的mapper访问不同的数据源
application-properties配置文件中配置两个数据源
#数据库连接配置
app.datasource.primary.type=com.zaxxer.hikari.HikariDataSource
app.datasource.primary.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
#app.datasource.primary.jdbc-url=jdbc:sqlserver://127.0.0.1:1433;SelectMethod=cursor;Databasename=attendance
app.datasource.primary.jdbc-url=jdbc:sqlserver://170.0.0.1:1433;SelectMethod=cursor;Databasename=attendance
app.datasource.primary.username=root
app.datasource.primary.password=ENC(tbpIb42Y1Q9fxoB7CcLyCmMnpBn8)
app.datasource.secondar.type=com.zaxxer.hikari.HikariDataSource
app.datasource.secondar.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
app.datasource.secondar.jdbc-url=jdbc:sqlserver://170.0.0.1:1433;SelectMethod=cursor;Databasename=zkeco
app.datasource.secondar.username=root
app.datasource.secondar.password=ENC(tbpIb42Y1Q9fxoBSCcpyCRmMnpBn8)
#HikariCP配置
spring.datasource.hikari.maximumPoolSize=30
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=300000
spring.datasource.hikari.minimumIdle=10
spring.datasource.hikari.leakDetectionThreshold=10000
spring.datasource.hikari.maxLifetime=1800000
其中数据源密码通过jasypt进行加密(也可以使用明文)
创建两个数据源配置类
import javax.sql.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.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
/**
* <p>Description: 数据源attendance配置<p>
*
* @author oldw
* @date 2020年2月18日
*/
@Configuration
// basePackages配置mapper存放的路径
@MapperScan(basePackages = "com.attendance.mapper.primary", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class DataSourcePrimaryConfig {
/**
* 生成数据源. @Primary 注解声明为默认数据源
*/
@Bean(name = "primaryDataSource")
//prefix为properties中数据源的前缀
@ConfigurationProperties(prefix = "app.datasource.primary")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 创建 SqlSessionFactory
*/
@Bean(name = "primarySqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// mapper.xml路径
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
return bean.getObject();
}
/**
* 配置事务管理
*/
@Bean(name = "primaryTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "primarySqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
在创建第二个数据源配置