配置多个数据源示例如下:
DxMybatisConfig.java
@Configuration
@MapperScan("com.justplay1994.github.dao.dx")
public class DxMybatisConfig {
@Bean(name = "dxDataSource") //作为一个bean对象并命名
@ConfigurationProperties(prefix = "spring.dx") //配置文件中,该数据源的前缀
@Primary //用于标记主数据源,除了主数据源外,其余注入文件都不添加该注解
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dxSqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("dxDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/justplay1994/github/dao/dx/*.xml"));//对应mapper.xml的具体位置
return bean.getObject();
}
@Bean(name = "dxTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("dxDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "dxSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("dxSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
FtMybatisConfig.java
@Configuration
@MapperScan(basePackages = "com.justplay1994.github.dao.ft", sqlSessionFactoryRef = "ftSqlSessionFactory")
public class FtMybatisConfig {
@Bean(name = "ftDataSource") //作为一个bean对象并命名
@ConfigurationProperties(prefix = "spring.ft") //配置文件中,该数据源的前缀
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "ftSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("ftDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/justplay1994/github/dao/ft/*.xml"));//对应mapper.xml的具体位置
return bean.getObject();
}
@Bean(name = "ftTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("ftDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "ftSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ftSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
配置:
spring:
ft:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: oracle.jdbc.OracleDriver
jdbc-url: jdbc:oracle:thin:@10.217.17.71:1521/orcl
username: DAXING
password: 123456
hikari:
minimum-idle: 5
maximum-pool-size: 24
pool-name: ${spring.application.name}-CP
idle-timeout: 6000 #移除空闲连接的时间
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
leakDetectionThreshold: 2000
validation-interval: 5000
validation-query-timeout: 5000
max-lifetime: 1800000
test-while-idle: true
dx:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://10.217.17.79:3306/moniwa?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: Grand_p0ss
hikari:
minimum-idle: 5
maximum-pool-size: 24
pool-name: ${spring.application.name}-CP
idle-timeout: 6000 #移除空闲连接的时间
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
leakDetectionThreshold: 2000
validation-interval: 5000
validation-query-timeout: 5000
max-lifetime: 1800000
test-while-idle: true
maven依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--使用Hikari连接池,需要禁用springboot内置的tomcat的连接池,同时要引入Hikari连接池的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>com.github.noraui</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
关键点:
- Application启动类增加注解:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class }) - 其中一个数据源配置增加@Primay,不加的话会出现sqlsession found 2,spring不知道选哪个。
- 另一个数据源在@MapperScan上增加:
@MapperScan(basePackages = “com.justplay1994.github.dao.ft”, sqlSessionFactoryRef = “ftSqlSessionFactory”) - 数据源扫描不同的包。