数据库双数据源配置
数据库双数据源配置
配置文件添加2个数据库的配置信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #第一数据源 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
|
配置双数据源的配置类
第一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| 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;
@Configuration @MapperScan(basePackages = "cn.mapper.mysql", sqlSessionFactoryRef = "mysqlSqlSessionFactory") public class MySqlDataSourceConfig {
@Bean(name = "mysqlDataSource") @ConfigurationProperties(prefix = "spring.datasource.secondary") public HikariDataSource getMysqlDateSource() { return new HikariDataSource(); }
@Bean(name = "mysqlSqlSessionFactory") public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource datasource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); 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); } }
|
第二个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| 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;
@Configuration @MapperScan(basePackages = "mapper.sqlserver", sqlSessionFactoryRef = "sqlserverSqlSessionFactory") public class SqlserverDataSourceConfig {
@Bean(name = "sqlserverDataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource.primary") public HikariDataSource getSqlserverDateSource() { return new HikariDataSource(); }
@Bean(name = "sqlserverSqlSessionFactory") @Primary public SqlSessionFactory sqlserverSqlSessionFactory(@Qualifier("sqlserverDataSource") DataSource datasource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); 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