springBoot多数据源案例
2017-07-18 本文已影响90人
jeffDeng
多数据源在日常企业级系统开发中还常见,我也是最近接触比较多,写个案例给大家参考下:
框架:springBoot + mybaits
nosql: redis +mangodb
1.项目分多个模块,降低耦合度(图一):

2.数据源有多个,建立不同数据源配置(图二、图三)

3.具体类的配置(直接贴代码)
@SuppressWarnings("all")
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = OpsDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "opsSqlSessionFactory")
public class OpsDataSourceConfig {
// 精确到主数据库目录,以便跟其他数据源隔离
static final String PACKAGE = "com.iboxpay.dao.ops";
static final String MAPPER_LOCATION = "classpath:mappers/ops/*/*.xml";
@NotNull
@Value("${ops.datasource.url}")
private String url;
@NotNull
@Value("${ops.datasource.username}")
private String username;
@NotNull
@Value("${ops.datasource.password}")
private String password;
@NotNull
@Value("${ops.datasource.driverClassName}")
private String driverClass;
@NotNull
@Value("${spring.datasource.druid.filters}")
private String filters;
@Bean
@Primary
DataSource opsDataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setUrl(url);
dataSource.setFilters(filters);
dataSource.setDefaultAutoCommit(false);
return dataSource;
}
@Bean(name = "opsTransactionManager")
@Primary
public DataSourceTransactionManager opsTransactionManager() throws SQLException {
return new DataSourceTransactionManager(opsDataSource());
}
@Bean(name = "opsSqlSessionFactory")
@Primary
public SqlSessionFactory opsSqlSessionFactory(
@Qualifier("opsDataSource") DataSource opsDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(opsDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(OpsDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.validation.constraints.NotNull;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan(basePackages = WechatDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "wechatSqlSessionFactory")
public class WechatDataSourceConfig {
static final String PACKAGE = "com.iboxpay.dao.wechat";
static final String MAPPER_LOCATION = "classpath:mappers/wechat/*/*.xml";
@Value("${wechat.datasource.url}")
private String url;
@Value("${wechat.datasource.username}")
private String username;
@Value("${wechat.datasource.password}")
private String password;
@Value("${wechat.datasource.driverClassName}")
private String driverClass;
@NotNull
@Value("${spring.datasource.druid.filters}")
private String filters;
@Bean(name = "wechatDataSource")
public DataSource wechatDataSource() throws SQLException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setFilters(filters);
dataSource.setDefaultAutoCommit(false);
return dataSource;
}
@Bean(name = "wechatDataSourceTransactionManager")
public DataSourceTransactionManager wechatDataSourceTransactionManager(
@Qualifier("wechatDataSource") DataSource wechatDataSource) {
return new DataSourceTransactionManager(wechatDataSource);
}
@Bean(name = "wechatSqlSessionFactory")
public SqlSessionFactory wechatSqlSessionFactory(
@Qualifier("wechatDataSource") DataSource wechatDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(wechatDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
使用@MapperScan 注解:
basePackages = WechatDataSourceConfig.PACKAGE 指定扫描的mapper接口所在的包
sqlSessionFactoryRef = "wechatSqlSessionFactory" 指定固定的数据源到sqlSessionFactoryRef 中
static final String MAPPER_LOCATION = "classpath:mappers/ops/*/*.xml";
static final String MAPPER_LOCATION = "classpath:mappers/wechat/*/*.xml";
根据不同的MAPPER_LOCATION路径,读取的数据配置文件也不同,扫描的PACKAGE 也不同,这样就做到多数据源的操作。