spring: datasource: type: com.alibaba.druid.pool.DruidDataSource datasource1: url: jdbc:mysql://127.0.0.1:3306/datasource1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false username: root password: root initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true driver-class-name: com.mysql.cj.jdbc.Driver datasource2: url: jdbc:mysql://127.0.0.1:3306/datasource2?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false username: root password: root initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true driver-class-name: com.mysql.cj.jdbc.Driver
@Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.datasource1") public DataSource dataSource1() { // 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSource return DruidDataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = "spring.datasource.datasource2") public DataSource dataSource2() { // 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSource return DruidDataSourceBuilder.create().build(); } /* @Bean public Interceptor dynamicDataSourcePlugin(){ return new DynamicDataSourcePlugin(); } */ @Bean public DataSourceTransactionManager transactionManager1(DynamicDataSource dataSource){ DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } @Bean public DataSourceTransactionManager transactionManager2(DynamicDataSource dataSource){ DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } }
** 在类里面注入分别注入两个数据源**
@Component @Primary // 将该Bean设置为主要注入Bean public class DynamicDataSource extends AbstractRoutingDataSource { // 当前使用的数据源标识 public static ThreadLocalname=new ThreadLocal<>(); // 写 @Autowired DataSource dataSource1; // 读 @Autowired DataSource dataSource2; // 返回当前数据源标识 @Override protected Object determineCurrentLookupKey() { return name.get(); } } // ** 在这个类初始化完成之后,进行数据源的注入** // 当前使用的数据源标识 public static ThreadLocal name=new ThreadLocal<>(); @Override public void afterPropertiesSet() { // 为targetDataSources初始化所有数据源 Map
@Component @Aspect public class DynamicDataSourceAspect implements Ordered { // 前置 @Before("within(com.tuling.dynamic.datasource.service.impl.*) && @annotation(wr)") public void before(JoinPoint point, WR wr){ String name = wr.value(); DynamicDataSource.name.set(name); System.out.println(name); } @Override public int getOrder() { return 0; } // 环绕通知 }
@Service public class FriendImplService implements FriendService { @Autowired FriendMapper friendMapper; @Override @WR("R") // 库2 public Listlist() { // DynamicDataSource.name.set("R"); return friendMapper.list(); } @Override @WR("W") // 库1 public void save(Friend friend) { // DynamicDataSource.name.set("W"); friendMapper.save(friend); } }
上面采用注解的方式就是,其实是利用切面进行数据源的设置,和注释的注释方式类似
@Configuration // 继承mybatis: // 1. 指定扫描的mapper接口包(主库) // 2. 指定使用sqlSessionFactory是哪个(主库) @MapperScan(basePackages = "com.datasource.dynamic.mybatis.mapper.w", sqlSessionFactoryRef="wSqlSessionFactory") public class WMyBatisConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.datasource1") public DataSource dataSource1() { // 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSource return DruidDataSourceBuilder.create().build(); } @Bean @Primary public SqlSessionFactory wSqlSessionFactory() throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); // 指定主库 sessionFactory.setDataSource(dataSource1()); // 指定主库对应的mapper.xml文件 /*sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:mapper/order/*.xml"));*/ return sessionFactory.getObject(); } @Bean @Primary public DataSourceTransactionManager wTransactionManager(){ DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource1()); return dataSourceTransactionManager; } @Bean public TransactionTemplate wTransactionTemplate(){ return new TransactionTemplate(wTransactionManager()); } }
@Configuration // 继承mybatis: // 1. 指定扫描的mapper接口包(主库) // 2. 指定使用sqlSessionFactory是哪个(主库) @MapperScan(basePackages = "com.tuling.datasource.dynamic.mybatis.mapper.r", sqlSessionFactoryRef="rSqlSessionFactory") public class RMyBatisConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.datasource2") public DataSource dataSource2() { // 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSource return DruidDataSourceBuilder.create().build(); } @Bean @Primary public SqlSessionFactory rSqlSessionFactory() throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); // 指定主库 sessionFactory.setDataSource(dataSource2()); // 指定主库对应的mapper.xml文件 /*sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:mapper/r/*.xml"));*/ return sessionFactory.getObject(); } @Bean public DataSourceTransactionManager rTransactionManager(){ DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource2()); return dataSourceTransactionManager; } @Bean public TransactionTemplate rTransactionTemplate(){ return new TransactionTemplate(rTransactionManager()); } }
@Autowired private RFriendMapper rFriendMapper; @Autowired private WFriendMapper wFriendMapper; // 读-- 读库 @Override public Listlist() { return rFriendMapper.list(); } // 保存-- 写库 @Override public void saveW(Friend friend) { friend.setName("loulan"); wFriendMapper.save(friend); } // 保存-- 读库 @Override public void saveR(Friend friend) { friend.setName("loulan"); rFriendMapper.save(friend); }
com.baomidou dynamic-datasource-spring-boot-starter3.5.0
spring: datasource: dynamic: #设置默认的数据源或者数据源组,默认值即为master primary: master #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源 strict: false datasource: master: url: jdbc:mysql://127.0.0.1:3306/datasource1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false username: root password: root initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true driver-class-name: com.mysql.cj.jdbc.Driver slave_1: url: jdbc:mysql://127.0.0.1:3306/datasource2?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false username: root password: root initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true driver-class-name: com.mysql.cj.jdbc.Driver
@Service public class FriendImplService implements FriendService { @Autowired FriendMapper friendMapper; @Override @DS("slave_1") // 从库, 如果按照下划线命名方式配置多个 , 可以指定前缀即可(组名) public Listlist() { return friendMapper.list(); } @Override @DS("master") public void save(Friend friend) { friendMapper.save(friend); } @DS("master") @DSTransactional public void saveAll(){ // 执行多数据源的操作 } }