springboot2+mybatis多种方式实现多数据配置方法

 更新时间:2020年03月30日 14:21:34   作者:落孤秋叶  
这篇文章主要介绍了springboot2+mybatis多种方式实现多数据配置方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

业务系统复杂程度增加,为了解决数据库I/O瓶颈,很自然会进行拆库拆表分服务来应对。这就会出现一个系统中可能会访问多处数据库,需要配置多个数据源。

第一种场景:项目服务从其它多处数据库取基础数据进行业务处理,因此各库之间不会出现重表等情况。

第二种场景:为了减轻写入压力进行读写分库,读走从库,写为主库。此种表名等信息皆为一致。

第三种场景:以上两种皆有。对于某些业务需要大数据量的汇总统计,希望不影响正常业务必须走从库(表信息一致),某些配置信息不存在读写压力,出现不分库(表信息不一致)

 项目源代码:

https://github.com/zzsong/springboot-multiple-datasource.git

有三个目录:

one:
    直接使用多@Bean配置,@MapperScan来路径区分读何库

two:
    使用注解的方式来标识走何dataSource,AOP拦截注入动态数据源   

third:
    使用spring的Bean命名策略进行区分数据来源

项目技术选型: springBoot2.2.5 + mybatis + druid + mysql

先看主要的pom包

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/> 
  </parent>

        <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.19</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.21</version>
    </dependency>

application.yml

spring:
 datasource:
  druid:
   core:
    url: jdbc:mysql:///kc_core?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
   schedule:
    url: jdbc:mysql:///kc_schedule?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

mysql新版本必须带有serverTimezone,不然会报连接异常。

第一种:通过@MapperScans 扫描匹配相关的数据源

@Configuration
@MapperScans({
    @MapperScan(basePackages = "com.zss.one.mapper.core", sqlSessionTemplateRef = "coreSqlSessionTemplate",sqlSessionFactoryRef = "coreSqlSessionFactory"),
    @MapperScan(basePackages = "com.zss.one.mapper.schedule", sqlSessionTemplateRef = "scheduleSqlSessionTemplate",sqlSessionFactoryRef = "scheduleSqlSessionFactory")
})
public class MybatisOneConfig {

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.druid.core")
  public DataSource coreDataSource(){
    return DruidDataSourceBuilder.create().build();
  }

  @Bean
  public SqlSessionFactory coreSqlSessionFactory(@Qualifier("coreDataSource") DataSource coreDataSource) throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(coreDataSource);
    sessionFactory.getObject().getConfiguration().setJdbcTypeForNull(null);
    sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return sessionFactory.getObject();
  }

  @Bean
  public SqlSessionTemplate coreSqlSessionTemplate(@Qualifier("coreSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }

  //======schedule========
  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.druid.schedule")
  public DataSource scheduleDataSource(){
    return DruidDataSourceBuilder.create().build();
  }

  @Bean
  public SqlSessionFactory scheduleSqlSessionFactory(@Qualifier("scheduleDataSource") DataSource coreDataSource) throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(coreDataSource);
    sessionFactory.getObject().getConfiguration().setJdbcTypeForNull(null);
    sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return sessionFactory.getObject();
  }

  @Bean
  public SqlSessionTemplate scheduleSqlSessionTemplate(@Qualifier("scheduleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}

第二种是动态数据源模式,通过AOP切入注解引导使用何数据源。用自定义注解@interface来标识方法走对应的数据源。

注意事项:类中的方法再调用带数据源的方法,不能被AOP切入

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TargetDataSource {
  String value();
}

extends spring的动态DataSource路由来匹配

public class DynamicDataSource extends AbstractRoutingDataSource {

  @Override
  protected Object determineCurrentLookupKey() {
    return DataSourceContextRouting.getDataSourceName();
  }
}
@Configuration
//@EnableConfigurationProperties(MybatisProperties.class)//不要使用此公共配置,Configuration会破坏相关dataSource的配置
@MapperScan("com.zss.two.mapper")
public class MybatisConfig {

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.druid.core")
  public DataSource coreDataSource() {
    return DruidDataSourceBuilder.create().build();
  }

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.druid.schedule")
  public DataSource scheduleDataSource() {
    return DruidDataSourceBuilder.create().build();
  }

  @Autowired
  @Qualifier("coreDataSource")
  private DataSource coreDataSource;

  @Autowired
  @Qualifier("scheduleDataSource")
  private DataSource scheduleDataSource;

  @Bean
  public DynamicDataSource dataSource() {
    Map<Object, Object> targetDataSources = new HashMap<>();
    targetDataSources.put(DataSourceConstants.CORE_DATA_SOURCE, coreDataSource);
    targetDataSources.put(DataSourceConstants.SCHEDULE_DATA_SOURCE, scheduleDataSource);

    DynamicDataSource dataSource = new DynamicDataSource();

    //设置数据源映射
    dataSource.setTargetDataSources(targetDataSources);
////    设置默认数据源,当无法映射到数据源时会使用默认数据源
    dataSource.setDefaultTargetDataSource(coreDataSource);
    dataSource.afterPropertiesSet();
    return dataSource;
  }
  /**
   * 根据数据源创建SqlSessionFactory
   */
  @Bean
  public SqlSessionFactory sqlSessionFactory(DynamicDataSource dataSource) throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.getObject().getConfiguration().setJdbcTypeForNull(null);
    sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return sessionFactory.getObject();
  }

  @Bean
  public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }

第三种,自定义Bean命名策略,按beanName进行自动匹配使用数据源

@Component
public class CoreBeanNameGenerator implements BeanNameGenerator {
  @Override
  public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    return "core"+ ClassUtils.getShortName(definition.getBeanClassName());
  }
}

@Component
public class ScheduleBeanNameGenerator implements BeanNameGenerator {
  @Override
  public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    return "schedule"+ ClassUtils.getShortName(definition.getBeanClassName());
  }
}

使用mybatis MapperScannerConfigurer自动扫描,将Mapper接口生成注入到spring

  @Bean
  public MapperScannerConfigurer coreMapperScannerConfig(CoreBeanNameGenerator coreBeanNameGenerator){
    MapperScannerConfigurer configurer = new MapperScannerConfigurer();
    configurer.setNameGenerator(coreBeanNameGenerator);
    configurer.setBasePackage("com.zss.third.mapper.core,com.zss.third.mapper.order");
    configurer.setSqlSessionFactoryBeanName("coreSqlSessionFactory");
    configurer.setSqlSessionTemplateBeanName("coreSqlSessionTemplate");
    return configurer;
  }

  @Bean
  public MapperScannerConfigurer scheduleMapperScannerConfig(ScheduleBeanNameGenerator scheduleBeanNameGenerator){
    MapperScannerConfigurer configurer = new MapperScannerConfigurer();
    configurer.setNameGenerator(scheduleBeanNameGenerator);
    configurer.setBasePackage("com.zss.third.mapper.schedule,com.zss.third.mapper.order");
    configurer.setSqlSessionFactoryBeanName("scheduleSqlSessionFactory");
    configurer.setSqlSessionTemplateBeanName("scheduleSqlSessionTemplate");
    return configurer;
  }

到此,三种多数据源匹配主要点介绍完,详细直接下载github项目。 在resources/db含有相关测试表及数据脚本。

到此这篇关于springboot2+mybatis多种方式实现多数据配置方法的文章就介绍到这了,更多相关springboot2+mybatis 多数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java加载properties文件的六种方法总结

    java加载properties文件的六种方法总结

    这篇文章主要介绍了java加载properties文件的六种方法总结的相关资料,需要的朋友可以参考下
    2017-05-05
  • 解决JDK21中用不了TimeUtild问题

    解决JDK21中用不了TimeUtild问题

    在使用TimeUtil时,可能因为IDE版本不兼容导致问题,升级IDEA到2023.2以上版本可解决此问题,详细步骤可以通过评论区索取安装包或直接从官网下载,分享个人经验,希望对大家有帮助
    2024-10-10
  • SpringBoot集成vue的开发解决方案

    SpringBoot集成vue的开发解决方案

    这篇文章主要介绍了SpringBoot集成vue的开发解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Spring实现动态切换多数据源的解决方案

    Spring实现动态切换多数据源的解决方案

    这篇文章主要给大家介绍了Spring实现动态切换多数据源的解决方案,文中给出了详细的介绍和示例代码,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友可以参考学习,下面来一起看看吧。
    2017-01-01
  • Java自动生成编号的方法步骤

    Java自动生成编号的方法步骤

    在新增数据时,往往需要自动生成编号,本文主要介绍了Java自动生成编号的方法步骤,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Maven Repository仓库的具体使用

    Maven Repository仓库的具体使用

    本文主要介绍了Maven Repository仓库的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • SpringBoot最新定时任务的7种实现方案

    SpringBoot最新定时任务的7种实现方案

    在现代应用中,定时任务是一个非常常见的需求,本文将通过7种方式讲解如何在SpringBoot中实现定时任务,包括使用@Scheduled注解、ScheduledExecutorService、Quartz、SpringTaskScheduler、Redis、XXL-JOB和Elastic-Job等,各有优缺点,选择时应根据实际需求进行考虑
    2024-12-12
  • Java ThreadLocal原理解析以及应用场景分析案例详解

    Java ThreadLocal原理解析以及应用场景分析案例详解

    这篇文章主要介绍了Java ThreadLocal原理解析以及应用场景分析案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • 使用Springboot封装一个自适配的数据单位转换工具类

    使用Springboot封装一个自适配的数据单位转换工具类

    我们在接收前台传输的数据时,往往SpringBoot使用内置的数据类型转换器把我们提交的数据自动封装成对象等类型,下面这篇文章主要给大家介绍了关于使用Springboot封装一个自适配的数据单位转换工具类的相关资料,需要的朋友可以参考下
    2023-03-03
  • Java队列篇之实现数组模拟队列及可复用环形队列详解

    Java队列篇之实现数组模拟队列及可复用环形队列详解

    像栈一样,队列(queue)也是一种线性表,它的特性是先进先出,插入在一端,删除在另一端。就像排队一样,刚来的人入队(push)要排在队尾(rear),每次出队(pop)的都是队首(front)的人
    2021-10-10

最新评论