springboot整合多数据源配置(包含事务处理)

springboot整合多数据源配置(包含事务处理),第1张

springboot整合多数据源配置(包含事务处理) 一、第一种  springboot+druid+mybatisplus+jdk8使用注解整合

1.1 application.yml 配置文件如下

server:
  port: 8080
spring:
  datasource:
    dynamic:
      primary: database1 # 配置默认数据库
      datasource:
        database1: # 数据源1配置
          url: jdbc:mysql://127.0.0.1:3306/database1?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
        database2: # 数据源2配置
          url: jdbc:mysql://127.0.0.1:3306/database2?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
      durid:
        initial-size: 1
        max-active: 20
        min-idle: 1
        max-wait: 60000
  autoconfigure:
    # 去除druid配置
    exclude:  com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure 

1.2 配置启动类

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

1.3 给使用非默认数据源添加注解@DS

@DS("database2")
public interface DemoMapper extends baseMapper {
    @Select("SELECT * FROM test")
    @DS("database2")
    List selectAll();
}
//-------------------
@Service
@DS("database2")
public class DemoServiceImpl extends ServiceImpl implements IDemoService {}
//-------------------
二、第二种 springboot+mybatis使用分包方式整合
spring-boot-starter-web
mybatis-spring-boot-starter
mysql-connector-java
lombok

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 多数据源的配置类

SpringBoot2.0以前需要在方法上加Primary注解,来判断默认的数据库连接

@Configuration
@MapperScan(basePackages = "com.edu.multipledatasource.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DataSourceConfig1 {

    @Primary // 表示这个数据源是默认数据源, 这个注解必须要加,因为不加的话spring将分不清楚那个为主数据源(默认数据源)
    @Bean("db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db1") //读取application.yml中的配置参数映射成为一个对象
    public DataSource getDb1DataSource(){
        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*:mybatis/db1
    @Bean(name = "demo1SqlSessionFactory")
    public SqlSessionFactory demo1SqlSessionFactory(@Qualifier("demo1DataSource") DataSource dataSource) throws Exception {
		//逻辑删除 如果值为1,表示已经删除,如果为0,表示未删除
        GlobalConfiguration globalConfig = new GlobalConfiguration();
        globalConfig.setLogicDeletevalue("1");
        globalConfig.setLogicNotDeletevalue("0");
        globalConfig.setSqlInjector(new LogicSqlInjector());
        //分页插件
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.addInterceptor(new PaginationInterceptor());

        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfiguration(configuration);
        sqlSessionFactoryBean.setGlobalConfig(globalConfig);
        return sqlSessionFactoryBean.getObject();
    }

如果任使用mybatis的SqlSessionFactoryBean,当调用baseMapper的方法时,会报Invalid bound statement (not found)异常。

本文GIT源码: https://github.com/cuiwenbo1992/multiple-data-source

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5638286.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存