@[TOC]
SpringBoot配置多数据源实战
需求来源:
当相关业务场景想实现同时操作2个甚至多个不同数据库表的时候,就需要配置多个数据源。
简单粗暴3步使用步骤:
1.修改包名结构
2.application.properties配置2套数据源,注意加前缀
3.修改DataSourceOmniConfig里面的注解@MapperScan和@Bean(name = "transactionManager")即可,其他不用改可直接使用
思路讲解:
springboot默认支持一个数据库,所以可简写(具体参照自己项目即可),但是当想实现配置多数据源的时候,application.properties配置文件需要修改前缀,用于自定义工厂进行扫描产生Bean
目录结构:
使用注意点:
1.DataSourceOmniConfig和DataSourcePlatformConfig中,其中一个要使用注解@Primary,代表哪个是主要的数据源 |
---|
2.使用只需修改其中的注解属性@MapperScan(basePackages=xxxx) |
3.主要数据源中事务那里要改成这样@Bean(name = "transactionManager"),不改会报错显示缺少事务支持 |
4.配置文件中url前要加jdbc,例如spring.datasource01.jdbc-url |
DataSourceOmniConfig
package com.geespace.omni.datasource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
/**
* 表示这个类为一个配置类,配置Mybatis的接口类放的地方
* @author: liudz
* @date: 2020/8/13
*/
@Configuration
@MapperScan(basePackages = "com.geespace.omni.dao.database1", sqlSessionFactoryRef = "db01SqlSessionFactory")
public class DataSourceOmniConfig {
/**
* 将这个对象放入Spring容器中,读取application.properties中的配置参数映射成为一个对象,prefix表示参数的前缀
* @author: liudz
* @date: 2020/8/13
* @return 执行结果
*/
@Bean(name = "db01DataSource")
@ConfigurationProperties(prefix = "spring.datasource01")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 将这个对象放入Spring容器中
* @param dataSource dataSource
* @author: liudz
* @date: 2020/8/13
* @return 执行结果
* @throws Exception Exception
*/
@Bean(name = "db01SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("db01DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource((dataSource));
return bean.getObject();
}
/**
* 将这个对象放入Spring容器中
* @param dataSource dataSource
* @author: liudz
* @date: 2020/8/13
* @return 执行结果
* @throws Exception Exception
*/
@Bean(name = "transactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("db01DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* 将这个对象放入Spring容器中
* @param sqlSessionFactory sqlSessionFactory
* @author: liudz
* @date: 2020/8/13
* @return 执行结果
* @throws Exception Exception
*/
@Bean(name = "db01SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("db01SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
DataSourcePlatformConfig
package com.geespace.omni.datasource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
/**
* 表示这个类为一个配置类, 配置Mybatis的接口类放的地方
* @author: liudz
* @date: 2020/8/13
*/
@Configuration
@MapperScan(basePackages = "com.geespace.omni.dao.database2", sqlSessionFactoryRef = "db02SqlSessionFactory")
public class DataSourcePlatformConfig {
/**
*
* @author liudz
* @date 2020/8/13
* @return 执行结果
**/
@Bean(name = "db02DataSource")
@ConfigurationProperties(prefix = "spring.datasource02")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* @param dataSource dataSource
* @author liudz
* @date 2020/8/13
* @return 执行结果
* @throws Exception Exception
**/
@Bean(name = "db02SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("db02DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource((dataSource));
return bean.getObject();
}
/**
* @param dataSource dataSource
* @author liudz
* @date 2020/8/13
* @return 执行结果
**/
@Bean(name = "db02TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("db02DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* @param sqlSessionFactory sqlSessionFactory
* @author liudz
* @date 2020/8/13
* @return 执行结果
* @throws Exception Exception
**/
@Bean(name = "db02SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("db02SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
重要信息