java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot多数据源集成

SpringBoot超详细讲解多数据源集成

作者:肥仔哥哥1930

今天分享下SpringBoot多数据源集成,我怕麻烦,这里我觉得我的集成也应该是最简单的,清晰明了

一、多数据源使用场景与弊端

1.场景

2.弊端

二、使用步骤

1.引入库

<!-- 多数据源支持 -->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
      <version>3.3.6</version>
    </dependency>

2.多数据源配置文件

#数据源配置
spring:
  application:
    name: olap
  datasource:
    dynamic:
      primary: target
    target:
      jdbc-url: jdbc:mysql://192.168.1.101:15051/db1?useUnicode=true&characterEncoding=utf8&useSSL=true&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
      username: root
      password: 123
      driver-class-name: com.mysql.jdbc.Driver
    source1:
      jdbc-url: jdbc:mysql://192.168.102:15052/db2?useUnicode=true&characterEncoding=utf8&useSSL=true&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
      username: admin
      password: 456
      driver-class-name: com.mysql.jdbc.Driver

3.多数据源配置类

数据源1

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
@MapperScan(basePackages = "com.easylinkin.dc.mapper.source1", sqlSessionFactoryRef = "source1SqlSessionFactory")
public class Source1DataSourceConfig {
  // mapper.xml所在地址
  private static final String MAPPER_LOCATION = "classpath*:mapper/business/source1/*.xml";
  /**
   * 数据源
   */
  @Bean(name = "source1DataSource")
  @ConfigurationProperties(prefix = "spring.datasource.source1")
  public DataSource dataSource() {
    DataSource build = DataSourceBuilder.create().build();
    return build;
  }
  /**
   * 事务管理器
   */
  @Bean(name = "source1TransactionManager")
  public PlatformTransactionManager dataSourceTransactionManager(
      @Qualifier("source1DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }
  /**
   * session工厂
   */
  @Bean(name = "source1SqlSessionFactory")
  public SqlSessionFactory sqlSessionFactory(@Qualifier("source1DataSource") DataSource dataSource)
      throws Exception {
  /*  final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(
        Source1DataSourceConfig.MAPPER_LOCATION));*/
    //不使用下面这个无法使用mybatis-plus提供的BaseMapper中的方法,需要增加mybatis-plus 的拓展依赖
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    //不在这里注入分页插件,会失效
    //bean.setPlugins(new PaginationInterceptor[]{new PaginationInterceptor()});
    return bean.getObject();
  }
}

数据源2

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
@MapperScan(basePackages = "com.easylinkin.dc.mapper.target", sqlSessionFactoryRef = "targetSqlSessionFactory")
public class TargetDataSourceConfig {
  // mapper.xml所在地址
  private static final String MAPPER_LOCATION = "classpath*:mapper/business/target/*.xml";
  /**
   * 主数据源,Primary注解必须增加,它表示该数据源为默认数据源 项目中还可能存在其他的数据源,如获取时不指定名称,则默认获取这个数据源,如果不添加,则启动时候回报错
   */
  @Primary
  @Bean(name = "targetDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.target")
  public DataSource dataSource() {
    DataSource build = DataSourceBuilder.create().build();
    return build;
  }
  /**
   * 事务管理器,Primary注解作用同上
   */
  @Bean(name = "masterTransactionManager")
  @Primary
  public PlatformTransactionManager dataSourceTransactionManager(
      @Qualifier("targetDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }
  /**
   * session工厂,Primary注解作用同上
   */
  @Bean(name = "targetSqlSessionFactory")
  @Primary
  public SqlSessionFactory sqlSessionFactory(@Qualifier("targetDataSource") DataSource dataSource)
      throws Exception {
    /*final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(
        TargetDataSourceConfig.MAPPER_LOCATION));
    return sessionFactoryBean.getObject();*/
    //不使用下面这个无法使用mybatis-plus提供的BaseMapper中的方法,需要增加mybatis-plus 的拓展依赖
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    //不在这里注入分页插件,会失效
    //bean.setPlugins(new PaginationInterceptor[]{new PaginationInterceptor()});
    return bean.getObject();
  }
}

4.使用

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.dc.common.exceptions.BusinessException;
import com.easylinkin.dc.common.utils.io.OutputStreamUtil;
import com.easylinkin.dc.mapper.source1.FkRegisterVisitorMapper;
import com.easylinkin.dc.business.visitor.entity.FkRegisterVisitor;
import com.easylinkin.dc.business.visitor.entity.vo.FkRegisterVisitorVo;
import com.easylinkin.dc.business.visitor.service.FkRegisterVisitorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
 * FkRegisterVisitor表服务实现类
 *
 * @author CodeGenerator
 * @date 2022/05/16
 */
@DS("source1")
@Slf4j
@Service("fkRegisterVisitorService")
public class FkRegisterVisitorServiceImpl extends ServiceImpl
    <FkRegisterVisitorMapper, FkRegisterVisitor> implements FkRegisterVisitorService {
    }

注意是在service的实现类上用@DS(“数据源名称”)注解,数据源名称与配置文件里的datasource下的一致。

注意dao不能再用@DS注解,否在报错

总结

我也是一个怕麻烦的人,所以能有简单的办法,我绝对不用复杂的,多数据源集成就是这么简单,也没啥好总结的,就提醒下:

跨数据源的业务接口要注意事务

@Transactional不生效

其实也有动态切换数据源的方法,我觉得没必要,规规矩矩用service,别把自己整糊了。

好就简单写道这里,up!

到此这篇关于SpringBoot超详细讲解多数据源集成的文章就介绍到这了,更多相关SpringBoot多数据源集成内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文