前言
项目有多个数据源,根据配置文件配置的连接数来自动生成多数据源配置,并且使用 aop切换数据源,使用的是 AbstractRoutingDataSource 重写 determineCurrentLookupKey 方法。
在切换数据源之前 @Transactional 先执行,此时会去获取数据源,如果不使用用前置通知(@Before),那么此时数据源还没有切换就会获取默认的数据源 ,这种情况会导致数据源切换失败。
源码:
一、其它方式
1.1 引入Maven依赖
org.springframework.boot
spring-boot-starter-parent
2.1.10.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-configuration-processor
tk.mybatis
mapper-spring-boot-starter
2.1.5
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-aop
io.springfox
springfox-swagger-ui
2.9.2
io.springfox
springfox-swagger2
2.9.2
org.springframework.boot
spring-boot-starter-security
io.jsonwebtoken
jjwt
0.9.1
com.alibaba
fastjson
1.2.70
org.projectlombok
lombok
true
commons-codec
commons-codec
1.14
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.6
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
mysql
mysql-connector-java
5.1.41
tk.mybatis
mapper
4.1.5
1.2 配置数据源
# 默认数据源
spring.datasource.url=jdbc:mysql://localhost:3306/user_center?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.hikari.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.username=root
spring.datasource.hikari.password=yibo
# 配置第一个数据源
spring.datasource.hikari.db1.jdbc-url=jdbc:mysql://localhost:3306/user_center?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.hikari.db1.username=root
spring.datasource.hikari.db1.password=yibo
spring.datasource.hikari.db1.driver-class-name=com.mysql.cj.jdbc.Driver
# 配置第二个数据源
spring.datasource.hikari.db2.jdbc-url=jdbc:mysql://localhost:3306/trade?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.hikari.db2.username=root
spring.datasource.hikari.db2.password=yibo
spring.datasource.hikari.db2.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis.type-aliases-package: com.yibo.center.domain.entity
#mybatis.mapper-locations: classpath:mapper/*.xml
mapper.identity: MYSQL
mapper.not-empty: false
#是否激活 swagger true or false
swagger.enable=true
1.3 构建数据源
-
默认数据源SpringBoot会自动构建
-
构建非默认数据源
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
/**
* @author: huangyibo
* @Date: 2020/6/19 2:26
* @Description:
*/
@Configuration
@MapperScan(basePackages = "com.yibo.multisource.mapper.user", sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class DataSource1Config {
/**
* 生成数据源. @Primary 注解声明为默认数据源
*/
@Bean(name = "db1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.hikari.db1")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 创建 SqlSessionFactory
*/
@Bean(name = "db1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/user/*.xml"));
return bean.getObject();
}
/**
* 配置事务管理
*/
@Bean(name = "db1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "db1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
/**
* @author: huangyibo
* @Date: 2020/6/19 2:27
* @Description:
*/
@Configuration
@MapperScan(basePackages = "com.yibo.multisource.mapper.trade", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DataSource2Config {
@Bean(name = "db2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.hikari.db2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/trade/*.xml"));
return bean.getObject();
}
@Bean(name = "db2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "db2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
非primary的事务管理器 需要在@Transactional中加上事务管理器beanName,不然事务不会起作用,默认使用的是primary的事务管理器
1.4 具体使用
import com.yibo.multisource.domain.entity.trade.TradeGoods;
import com.yibo.multisource.domain.entity.vo.TradeGoodsAO;
import com.yibo.multisource.mapper.trade.TradeGoodsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
/**
* @author: huangyibo
* @Date: 2020/6/11 0:23
* @Description:
*/
@Service
public class TradeGoodsService {
@Autowired
private TradeGoodsMapper tradeGoodsMapper;
@Transactional
public List findAll(){
return tradeGoodsMapper.selectAll();
}
@Transactional("db2TransactionManager")
public String addTradeGoods(TradeGoodsAO tradeGoodsAO){
TradeGoods tradeGoods = new TradeGoods();
BeanUtils.copyProperties(tradeGoodsAO,tradeGoods);
tradeGoods.setAddTime(new Date());
tradeGoodsMapper.insert(tradeGoods);
return "SUCCESS";
}
}
import com.yibo.multisource.domain.entity.user.User;
import com.yibo.multisource.domain.entity.vo.UserAO;
import com.yibo.multisource.mapper.user.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
/**
* @author: huangyibo
* @Date: 2020/6/10 23:46
* @Description:
*/
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List findAll(){
return userMapper.selectAll();
}
@Transactional
public User findById(Integer id){
User user = new User();
user.setId(id);
return userMapper.selectOne(user);
}
@Transactional("db1TransactionManager")
public String addUser(UserAO userAO){
User user = new User();
BeanUtils.copyProperties(userAO,user);
user.setCreateTime(new Date());
user.setUpdateTime(new Date());
userMapper.insert(user);
return "SUCCESS";
}
}
参考:
https://blog.csdn.net/sinat_36454672/article/details/117529762
https://blog.csdn.net/study_zmm/article/details/106790031
http://www.linhao007.com/2017/11/23/01/