yml文件配置
# 密码 用户名 url 填写相应的
second:
datasource:
url:
username:
password:
driverClassName: com.mysql.cj.jdbc.Driver
third:
datasource:
driverClassName: oracle.jdbc.driver.OracleDriver
url:
username:
password:
SecondDatasourceConfig.java
@Configuration
//扫描 Mapper 接口并容器管理
@MapperScan(basePackages = SecondDatasourceConfig.PACKAGE, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDatasourceConfig {
// 新建的目录,以便跟其他数据源隔离 这里放的是mapper.java文件
static final String PACKAGE = "com.tv189.hb.mms.hbfile";
//默认情况下src/main/java包下的.xml .properties文件不会被编译 这里放的mapper.xml文件
static final String MAPPER_LOCATION = "classpath*:secondDatasourceMapper/*.xml";
@Value("${second.datasource.url}")
private String url;
@Value("${second.datasource.username}")
private String user;
@Value("${second.datasource.password}")
private String password;
@Value("${second.datasource.driverClassName}")
private String driverClass;
@Bean(name = "secondDataSource")
public DataSource secondDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setJdbcUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
dataSource.setMinimumIdle(1);
dataSource.setMaximumPoolSize(5);
return dataSource;
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager secondTransactionManager() {
return new DataSourceTransactionManager(secondDataSource());
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource secondDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(secondDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(SecondDatasourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
ThirdDatasourceConfig.java
@Configuration
//扫描 Mapper 接口并容器管理
@MapperScan(basePackages = ThirdDatasourceConfig.PACKAGE, sqlSessionFactoryRef = "thirdSqlSessionFactory")
public class ThirdDatasourceConfig {
// 新建的目录,以便跟其他数据源隔离 把不同数据源的文件分开放置避免交叉
static final String PACKAGE = "*.*.*.mms.cpsfile.dao";
//默认情况下src/main/java包下的.xml .properties文件不会被编译
static final String MAPPER_LOCATION = "classpath*:thirdDatasourceMapper/*.xml";
@Value("${third.datasource.url}")
private String url;
@Value("${third.datasource.username}")
private String user;
@Value("${third.datasource.password}")
private String password;
@Value("${third.datasource.driverClassName}")
private String driverClass;
@Bean(name = "thirdDataSource")
public DataSource thirdDataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setJdbcUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
dataSource.setMinimumIdle(1);
dataSource.setMaximumPoolSize(5);
return dataSource;
}
@Bean(name = "thirdTransactionManager")
public DataSourceTransactionManager thirdTransactionManager() {
return new DataSourceTransactionManager(thirdDataSource());
}
@Bean(name = "thirdSqlSessionFactory")
public SqlSessionFactory thirdSqlSessionFactory(@Qualifier("thirdDataSource") DataSource thirdDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(thirdDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(ThirdDatasourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
原文:https://www.cnblogs.com/yichenbusan/p/14768261.html