• SpringBoot和MybatisPlus的多数据源配置及两者的区别


    applicaiton.yml

    spring:
      datasource:
        hikari:
          connection-timeout: 300000
          minimum-idle: 2
          maximum-pool-size: 100
          idle-timeout: 120000
          leak-detection-threshold: 300000
        slot1:
          driver-class-name: com.mysql.cj.jdbc.Driver
          jdbc-url: jdbc:mysql://127.0.0.1:3306/cspg_aomp_db_ms_new?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
          username: root
          password: ...
        slot2:
          driver-class-name: com.mysql.cj.jdbc.Driver
          jdbc-url: jdbc:mysql://188.188.188.188:31827/storage-user?useSSL=false&autoReconnect=true&characterEncoding=utf8&allowPublicKeyRetrieval=true
          username: root
          password: ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    SpringBoot多数据源配置

    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.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(
            basePackages = "com.cspg.nsx.mapper1",
            sqlSessionFactoryRef = "masterSqlSessionFactory")
    public class DataSourceSecondaryConfig {
    
        @Bean(name = "dslot1")
        @Qualifier("dslot1")
        @Primary
        @ConfigurationProperties(prefix="spring.datasource.slot1")
        public DataSource primaryDataSource() {
            return DataSourceBuilder.create().build();
        }
    
    
        @Bean(name = "masterSqlSessionFactory")
        @Primary
        public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dslot1") DataSource masterDataSource)
                throws Exception {
            final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
            sessionFactory.setDataSource(masterDataSource);
            /*
    			获取xml文件资源对象
    			当Mapper接口所对应的.xml文件与Mapper接口文件分离,存储在 resources
    			文件夹下的时候,需要手动指定.xml文件所在的路径
    		*/
            Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper1/*.xml");
            sessionFactory.setMapperLocations(resources);
    
            return sessionFactory.getObject();
        }
    
        @Bean(name = "MasterDataSourceManager")
        @Primary
        public DataSourceTransactionManager MasterDataSourceManager() {
            return new DataSourceTransactionManager(primaryDataSource());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    第二个数据源配置,去掉@Primary,改MapperScan和ClassPath路径,改bean的命名,此处不写啦

    MybatisPlus多数据源配置

    @Configuration
    @MapperScan(
            basePackages = "com.cspg.nsx.mapper1",
            sqlSessionFactoryRef = "masterSqlSessionFactory")
    public class SecondDataSourceConfig {
    
        @Bean(name = "dslot1")
        @Qualifier("dslot1")
        @Primary
        @ConfigurationProperties(prefix="spring.datasource.slot1")
        public DataSource primaryDataSource() {
            return DataSourceBuilder.create().build();
        }
    
    
    
        @Bean(name = "masterSqlSessionFactory")
        @Primary
        public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dslot1") DataSource masterDataSource)
                throws Exception {
            MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
            bean.setDataSource(masterDataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper1/*.xml"));
            return bean.getObject();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    还有一些SqlSessionTemplate 和DataSourceTransactionManager 没有配上,看你情况定,可以参考下面的链接配,这里主要讲区别!

    区别

    MybatisPlus是用MybatisSqlSessionFactoryBean 来创建SqlSessionFactory ,而SpringBoot的则使用SqlSessionFactoryBean来创建SqlSessionFactory 😮

    参考

    SpringBoot多数据源配置

    MybatisPlus多数据源配置

  • 相关阅读:
    设计模式-行为型-解释器模式
    重点区域人员徘徊识别监测系统
    一文搞懂基于透视变换的车道线拟合
    DJYOS技术特点:关键服务微秒级恢复
    springboot实现全局事务管理
    渗透测试-CNA_Bshell程序
    Cocos Creator 3.x 热更新
    基于XML配置的AOP
    LCD驱动
    【SpringCloud学习笔记】服务提供者,服务消费者
  • 原文地址:https://blog.csdn.net/weixin_42835230/article/details/133963052