博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot+mysql 多数据源配置
阅读量:2442 次
发布时间:2019-05-10

本文共 6880 字,大约阅读时间需要 22 分钟。

Springboot 多数据源配置

1、配置文件
#主数据源配置master.spring.datasource.driver = com.mysql.jdbc.Drivermaster.spring.datasource.url = ...master.spring.datasource.username = ...master.spring.datasource.password = ...master.spring.datasource.initialPoolSize = 1master.spring.datasource.minIdle = 10master.spring.datasource.maxActive = 100#配置另一个数据源second.spring.datasource.driver = com.mysql.jdbc.Driversecond.spring.datasource.url = ...second.spring.datasource.username = ...second.spring.datasource.password = ...second.spring.datasource.initialPoolSize = 1second.spring.datasource.minIdle = 10second.spring.datasource.maxActive = 100
2、创建生成数据源的类
@Configuration@Profile(value = {
"default", "prod"})public class DataSourceConfig {
@Value("${master.spring.datasource.url}") private String url; @Value("${master.spring.datasource.username}") private String user; @Value("${master.spring.datasource.password}") private String password; @Value("${master.spring.datasource.driver}") private String driverClass; @Value("${master.spring.datasource.initialPoolSize}") private int initialPoolSize; @Value("${master.spring.datasource.minIdle}") private int minIdle; @Value("${master.spring.datasource.maxActive}") private int maxActive; @Value("${second.spring.datasource.url}") private String url2; @Value("${second.spring.datasource.username}") private String user2; @Value("${second.spring.datasource.password}") private String password2; @Value("${second.spring.datasource.driver}") private String driverClass2; @Value("${second.spring.datasource.initialPoolSize}") private int initialPoolSize2; @Value("${second.spring.datasource.minIdle}") private int minIdle2; @Value("${second.spring.datasource.maxActive}") private int maxActive2; @Primary @Bean(name = "localDataSource", destroyMethod = "close", autowire = Autowire.BY_NAME) public DruidDataSource localDataSource() {
return getDruidDataSource(url, user, password, initialPoolSize, minIdle, maxActive); } @Bean(name = "local2DataSource", destroyMethod = "close", autowire = Autowire.BY_NAME) public DruidDataSource local2DataSource() {
return getDruidDataSource(url2, user2, password2, initialPoolSize2, minIdle2, maxActive2); } //数据源生成函数 private DruidDataSource getDruidDataSource(String jdbcUrl, String jdbcUser, String jdbcPassword, int initialPoolSize, int minIdle, int maxActive) {
DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName("com.mysql.jdbc.Driver"); druidDataSource.setUrl(jdbcUrl); druidDataSource.setUsername(jdbcUser); druidDataSource.setPassword(jdbcPassword); druidDataSource.setInitialSize(initialPoolSize); druidDataSource.setMinIdle(minIdle); druidDataSource.setMaxActive(maxActive); return druidDataSource; }}
3、配置数据源1
@Configuration//注意basePackages中的路径一定要扫描到数据源1repository具体的文件夹下,xxx是自己的路径@MapperScan(basePackages = {
"com.xxx.xxx.xxx.xxx"}, sqlSessionTemplateRef = "localSqlSessionTemplate")@EnableApolloConfig("mysql")public class LocalDaoConfiguration {
@Primary @Bean(name = "localSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //xxx数据源1 xml是自己的路径 Resource[] resources = resolver.getResources("classpath:mapping/xxx/*.xml"); Resource[] resArray = Arrays.copyOf(resources, resources.length); sqlSessionFactoryBean.setMapperLocations(resArray); return sqlSessionFactoryBean.getObject(); } @Primary @Bean(name = "localTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("localDataSource") DataSource dataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } @Primary @Bean(name = "localTransactionTemplate") public TransactionTemplate transactionTemplate( @Qualifier("localTransactionManager") DataSourceTransactionManager transactionManager) {
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); return transactionTemplate; } @Primary @Bean(name = "localSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate( @Qualifier("localSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory); }}
4、配置数据源2
@Configuration//注意basePackages中的路径一定要扫描到数据源2repository具体的文件夹下,xxx是自己的路径@MapperScan(basePackages = {
"com.xxx.xxx.xxx.xxx"}, sqlSessionTemplateRef = "local2SqlSessionTemplate")@EnableApolloConfig("mysql")public class LocalDaoConfiguration {
@Primary @Bean(name = "local2SqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("local2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //xxx是数据源2 自己的路径 Resource[] resources = resolver.getResources("classpath:mapping/xxx/*.xml"); Resource[] resArray = Arrays.copyOf(resources, resources.length); sqlSessionFactoryBean.setMapperLocations(resArray); return sqlSessionFactoryBean.getObject(); } @Primary @Bean(name = "local2TransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("local2DataSource") DataSource dataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } @Primary @Bean(name = "local2TransactionTemplate") public TransactionTemplate transactionTemplate( @Qualifier("local2TransactionManager") DataSourceTransactionManager transactionManager) {
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); return transactionTemplate; } @Primary @Bean(name = "local2SqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate( @Qualifier("local2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory); }}
5、就可以愉快的运行了

转载地址:http://cwjqb.baihongyu.com/

你可能感兴趣的文章
XML加ASP实现网页“本地化” (转)
查看>>
Java中的异步网络编程 (转)
查看>>
用于核心模式驱动程序的网络体系结构(1) (转)
查看>>
More Effective C++ 条款20 (转)
查看>>
一个程序员的爱恋 (转)
查看>>
足球战术->边锋之Decorator篇 (转)
查看>>
编写优质无错代码(1) (转)
查看>>
MySQL 4.1.0 中文参考手册 --- 6.3 用于 SELECT 和 WHERE 子句的函数 (1) (转)
查看>>
vs.net beta 2中利用DataGrid分页详解 (转)
查看>>
Process-Display-Process (PDP) pattern (转)
查看>>
基于构件复用的软件方法与COM支持 (转)
查看>>
DELPHI中使用API函数详解 (转)
查看>>
Single Entry Point to EJB Layer (转)
查看>>
InsideJVM(3)--Method area(方法区) (转)
查看>>
中文版Windows XP 的新增功能(转)
查看>>
Web Application 開 發 利 器 - WebSnap(三) (转)
查看>>
跟我学 安装Windows Vista Bata2实录(转)
查看>>
Windows Vista IIS 7.0开启方法(转)
查看>>
Windows Vista六大版本详细介绍(转)
查看>>
Windows XP 中注册表内容的导入和导出(转)
查看>>