本篇文章是基于Springboot2.0、mybatis与阿里的Druid整合,简单配置多数据源处理。
public class PathDefineConstant {
/**
* 基础包路径(application启动类所在路径)
*/
public final static String BASE_PACKAGE_PATH = "com.example.demo";
/**
* resources下 mapper包根路径
*/
public final static String MAPPER_PACKAGE_PATH = "mapper";
/**
* 主数据源基础路径
*/
public final static String MASTER_BASE_PACKAGE_PATH = BASE_PACKAGE_PATH + ".master";
/**
* 主数据源mapper配置路径
*/
public final static String MASTER_MAPPER_PACKAGE_PATH = MAPPER_PACKAGE_PATH + ".master";
/**
* user据源基础路径
*/
public final static String USER_BASE_PACKAGE_PATH = BASE_PACKAGE_PATH + ".user";
/**
* user数据源基础路径
*/
public final static String USER_MAPPER_PACKAGE_PATH = MAPPER_PACKAGE_PATH + ".user";
}
spring:
datasource:
master:
driverClassName: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/sys?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
user:
driver-class-name: oracle.jdbc.driver.OracleDriver
jdbc-url: jdbc:oracle:thin:@localhost:1521:hrdb
username: hr
password: 123456
###################以下为druid增加的配置###########################
type: com.alibaba.druid.pool.DruidDataSource
@Configuration
public class DatasourceConfig {
/**
* 主数据源
* @return
*/
@Primary
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "userDataSource")
@ConfigurationProperties(prefix = "spring.datasource.user")
public DataSource userDataSource() {
return DataSourceBuilder.create().build();
}
}
@Slf4j
public class MybatisConfig {
@Configuration
@MapperScan(
basePackages = {PathDefineConstant.MASTER_BASE_PACKAGE_PATH},
sqlSessionTemplateRef = "masterSqlSessionTemplate")
public static class MasterDatasourceConfig{
@Resource
DataSource masterDataSource;
@Bean(name="masterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory() throws Exception {
log.info("masterSqlSessionFactory create success!");
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(masterDataSource);
String locationPattern = "classpath:"+PathDefineConstant.MASTER_MAPPER_PACKAGE_PATH+"/*.xml";
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(locationPattern));
return factoryBean.getObject();
}
@Bean(name = "masterTransactionManger")
public DataSourceTransactionManager masterTransactionManger() {
return new DataSourceTransactionManager(masterDataSource);
}
@Bean(name = "masterSqlSessionTemplate")
public SqlSessionTemplate masterSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(masterSqlSessionFactory());
}
}
@Configuration
@MapperScan(basePackages = {PathDefineConstant.USER_BASE_PACKAGE_PATH},
sqlSessionTemplateRef ="userSqlSessionTemplate")
public static class UserDatasourceConfig{
@Resource
DataSource userDataSource;
@Bean(name = "userSqlSessionFactory")
public SqlSessionFactory userSqlSessionFactory() throws Exception {
log.info("userSqlSessionFactory create success!");
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(userDataSource);
String locationPattern = "classpath:"+PathDefineConstant.USER_MAPPER_PACKAGE_PATH+"/*.xml";
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(locationPattern));
return factoryBean.getObject();
}
@Bean(name = "userTransactionManger")
public DataSourceTransactionManager userTransactionManger() {
return new DataSourceTransactionManager(userDataSource);
}
@Bean(name = "userSqlSessionTemplate")
public SqlSessionTemplate userSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(userSqlSessionFactory());
}
}
}