• springboot mybatis多数据源配置


    1、配置文件

    spring:
      datasource:
        db1:
          driver-class-name: com.mysql.cj.jdbc.Driver
          type: com.zaxxer.hikari.HikariDataSource
          url: jdbc:mysql://127.0.0.1:3306/hello_spring_boot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
          username: root
          password: 123456
        db2:
          driver-class-name: com.mysql.cj.jdbc.Driver
          type: com.zaxxer.hikari.HikariDataSource
          url: jdbc:mysql://127.0.0.1:3306/hello_spring_boot_2?useUnicode=true&characterEncoding=UTF-8&useSSL=false
          username: root
          password: 123456
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3、DataSource配置

    @Configuration
    public class DataSourceConfig {
    
        @Primary
        @Bean("db1")
        @ConfigurationProperties(prefix = "spring.datasource.db1")
        public DataSourceProperties db1() {
            return new DataSourceProperties();
        }
    
        @Primary
        @Bean
        public DataSource dataSource1(@Qualifier("db1") DataSourceProperties dataSourceProperties) {
            return dataSourceProperties.initializeDataSourceBuilder().build();
        }
    
        @Bean("db2")
        @ConfigurationProperties(prefix = "spring.datasource.db2")
        public DataSourceProperties db2() {
            return new DataSourceProperties();
        }
    
        @Bean
        public DataSource dataSource2(@Qualifier("db2") DataSourceProperties dataSourceProperties) {
            return dataSourceProperties.initializeDataSourceBuilder().build();
        }
    }
    
    • 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

    4、根据数据源配置扫描的mapper

    @Configuration
    @MapperScan(
        basePackages = "com.tcoding.demo.mybatis.mbg.mapper1",
        annotationClass = Db1.class, sqlSessionTemplateRef =
        "sqlSessionTemplate1")
    public class MybatisConfigDb1 {
    
        @Primary
        @Bean
        public SqlSessionFactory db1SqlSessionFactory1(@Qualifier("dataSource1") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
            sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
            sqlSessionFactoryBean.setTypeHandlers(new EncryptTypeHandler());
            return sqlSessionFactoryBean.getObject();
        }
    
        @Primary
        @Bean
        public DataSourceTransactionManager transactionManager1(@Qualifier("dataSource1") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Primary
        @Bean("sqlSessionTemplate1")
        public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("db1SqlSessionFactory1") SqlSessionFactory sqlSessionFactory) {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    
    
    • 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
    @Configuration
    @MapperScan(
        basePackages = "com.tcoding.demo.mybatis.mbg.mapper2",
        annotationClass = Db2.class,
        sqlSessionTemplateRef = "sqlSessionTemplate2")
    public class MybatisConfigDb2 {
    
        @Bean
        public SqlSessionFactory db1SqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
            // XML 配置时开启
            sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/db2/*.xml"));
            sqlSessionFactoryBean.setTypeHandlers(new EncryptTypeHandler());
            org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
            // 开启驼峰命名
            configuration.setMapUnderscoreToCamelCase(true);
            sqlSessionFactoryBean.setConfiguration(configuration);
    
            return sqlSessionFactoryBean.getObject();
        }
    
        @Bean
        public DataSourceTransactionManager dataSourceTransactionManager2(@Qualifier("dataSource2") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean("sqlSessionTemplate2")
        public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("db1SqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    
    
    • 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

    4.1、踩坑 MapperScan 的 basePackages和annotationClass属性

    写demo的时候basePackages和annotationClass两个同时使用了,死活不好使,项目起不来
    原因如下 annotationClass 需要需要如下的定义
    需要@Target({ElementType.TYPE})
    @Retention(value = RetentionPolicy.RUNTIME)不然不生效

    @Target({ElementType.TYPE})
    @Retention(value = RetentionPolicy.RUNTIME)
    @Documented
    public @interface Db1 {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    坑的是basePackages和annotationClass取的是交集,所以annotationClass能不用就不用吧

    4.2、XML和注解配置Mapper需要注意的点

    xml配置是需要在
    SqlSessionFactory是指定 mapper.xml的路径

    sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
    
    • 1

    注解的方式为了方便可以开启驼峰命名

    5、源码连接

    https://github.com/googalAmbition/hello-spring-boot/tree/main/03-mybatis

  • 相关阅读:
    ubuntu24.04LVM扩容问题
    25 DRF详细学习篇章二|Parsers|Renderers|Serializers
    基于springboot的教材预定系统平台
    按列而非按行读取文本
    科技云报道:数智化升级,如何跨越数字世界与实体产业的鸿沟?
    【微信小程序 | 实战开发】常用的基础内容组件介绍和使用(2)
    详细指南:基于差分进化的马尔可夫链蒙特卡罗加速技术在MATLAB中的应用
    opentelemetry、grafana、Prometheus、jaeger、victoria-metrics 介绍、关系与使用
    【21天算法挑战赛】排序算法——直接插入排序
    端侧GPU基于opencl实现reduce算子
  • 原文地址:https://blog.csdn.net/qq_23934475/article/details/126568596