Java框架—SpringBoot+MyBatis实现多数据源配置

SpringBoot 实现多数据源配置

目录

  • 一、多数据源使用场景
  • 二、多数据源的实现
    • 1.springBoot+MyBatis分包方式整合实现
      • 1.1 application.yml配置
      • 1.2连接数据源配置类
    • 2.springboot+druid+mybatisplus使用注解整合
      • 2.1application.yml配置
      • 2.2使用@DS注解来区分不同数据源
      • 2.3 @Transaction 和 @DS 同时使用的问题
    • 3.继承AbstractRoutingDataSource实现
      • 3.1application.yml配置
      • 3.2 配置类和继承类
      • 3.3 使用
      • 3.4 自定义注解方式
      • 3.5使用@Transaction问题

一、多数据源使用场景

在实际开发中,经常可能遇到在一个应用中可能需要访问多个数据库的情况:

  • ①业务复杂(数据量大)
    数据分布在不同的数据库中,数据库拆了, 应用没拆。 一个公司多个子项目多个平台,各用各的数据库,涉及数据共享。

  • ②读写分离
    为了解决数据库的读性能瓶颈(读比写性能更高, 写锁会影响读阻塞,从而影响读的性能)。
    很多数据库拥主从架构。也就是,一台主数据库服务器,是对外提供增删改业务的生产服务器;另一台或者多台从数据库服务器,主要进行读的操作。ꞏ

读写分离可以通过中间件(ShardingSphere、mycat、mysql-proxy 、TDDL …)进行处理,但是有一些公司,没有专门的中间件团队搭建读写分离基础设施,因此需要业务开发人员自行实现读写分离。

二、多数据源的实现

1.springBoot+MyBatis分包方式整合实现

1.1 application.yml配置

server:
  port: 8080 # 启动端口
spring:
  datasource: 
    db1: # 数据源1
      jdbc-url: jdbc:mysql://localhost:3306/db1?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
    db2: # 数据源2
      jdbc-url: jdbc:mysql://localhost:3306/db2?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver

1.2连接数据源配置类

@Configuration
@MapperScan(basePackages = "com.example.dao.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DataSourceConfig1 {
   

    @Primary // 表示默认数据源, 这个注解必须要加,不然spring将分不清楚那个为主数据源
    @Bean("db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db1") //读取yml文件中的配置的数据源参数
    public DataSource createDb1DataSource(){
   
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean("db1SqlSessionFactory")
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
   
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
        return bean.getObject();
    }

	// 配置事务
	@Bean(name = "db1TransactionManager")
    public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
   
		DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
		dataSourceTransactionManager.setRollbackOnCommitFailure(true);
		dataSourceTransactionManager.setGlobalRollbackOnParticipationFailure(true);
    	return dataSourceTransactionManager;
    }

    @Primary
    @Bean("db1SqlSessionTemplate")
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
   
        return new SqlSessionTemplate(sqlSessionFactory);
    }

你可能感兴趣的:(Java,常用框架集合,java,spring,boot,mybatis)