• SpringBoot+Mybatis+Dynamic实现多数据源配置


      😊 @ 作者: 一恍过去
      🎊 @ 社区: Java技术栈交流
      🎉 @ 主题: SpringBoot+Mybatis+Dynamic实现多数据源配置
      ⏱️ @ 创作时间: 2022年06月17日

      前言

      在引入Dynamic包后,通过使用@DS实现多数据源的使用,该注解可以用于方法上和类上;

      1、创建数据库(表)

      分别在mysql中创建两个库:systemlog,并且创建对应的表(根据项目实际情况的定),如下:

      在这里插入图片描述

      2、POM

      <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>2.2.2</version>
      </dependency>
      <dependency>
          <groupId>com.baomidou</groupId>
          <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
          <version>3.5.1</version>
      </dependency>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10

      3、配置yaml

      spring:
        application:
          name: quartz
        datasource:
          dynamic:
            #设置默认的数据源或者数据源组
            primary: system 
            strict: false
            datasource:
              system:
                driverClassName: com.mysql.cj.jdbc.Driver
                url: jdbc:mysql://127.0.0.1:3307/system?useUnicode=true&useSSL=false&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
                username: root
                password: lhzlx
              log:
                driverClassName: com.mysql.cj.jdbc.Driver
                url: jdbc:mysql://127.0.0.1:3307/log?useUnicode=true&useSSL=false&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
                username: root
                password: lhzlx
      
          # hikari连接池
          type: com.zaxxer.hikari.HikariDataSource
          hikari:
            #最大连接数,小于等于0会被重置为默认值10;大于零小于1会被重置为minimum-idle的值
            maximum-pool-size: 10
            #最小空闲连接,默认值 10,小于0或大于maximum-pool-size,都会重置为maximum-pool-size
            minimum-idle: 2
            #连接超时时间:毫秒,小于250毫秒,否则被重置为默认值30秒
            connection-timeout: 60000
            #空闲连接超时时间,默认值600000ms(10分钟),大于等于max-lifetime且max-lifetime>0,会被重置为0;
            #不等于0且小于10秒,会被重置为10秒。
            #只有空闲连接数大于最大连接数且空闲时间超过该值,才会被释放(自动释放过期链接)
            idle-timeout: 600000
            #连接最大存活时间.不等于0且小于30秒,会被重置为默认值30分钟.设置应该比mysql设置的超时时间短
            max-lifetime: 640000
            #连接测试查询
            connection-test-query: SELECT 1
      
      
      #mapper 别名扫描
      mybatis:
        mapper-locations: classpath*:mappers/*.xml
        type-aliases-package: com.lhz.demo.model.entity
        #数据库类型
        configuration.database-id: mysql
        #自动驼峰转换
        configuration.map-underscore-to-camel-case: true
      
      • 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

      4、Mapper接口

      com.lhz.demo.mapper目录下创建LogMapperSysMapper,将@DS注解用在Mapper接口上表示为整个Mapper接口指定数据源;
      LogMapper:

      @Mapper
      public interface LogMapper {
          List<TbLog> selectAll();
      }
      
      • 1
      • 2
      • 3
      • 4

      SysMapper:

      @Mapper
      @DS("log")
      public interface SysMapper {
          List<TbSystem> selectAll();
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

      5、Mappeing(XML)

      resources/mappers目录下创建LogMapper.xmlSystemMapper.xml文件

      LogMapper.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      <mapper namespace="com.lhz.demo.mapper.LogMapper">
      
          <select id="selectAll" resultType="com.lhz.demo.model.entity.TbLog">
              select * from tb_log
          </select>
      </mapper>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8

      SystemMapper.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      <mapper namespace="com.lhz.demo.mapper.SysMapper">
      
      
          <select id="selectAll" resultType="com.lhz.demo.model.entity.TbSystem">
              select * from tb_system
          </select>
      </mapper>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

      6、Service

      @DS注解用在调用Mapp接口的方法上,表示当前请求会访问指定数据源;

      @Service
      public class TestService {
      
          @Resource
          private SysMapper sysMapper;
      
          @Resource
          private LogMapper logMapper;
      
          /**
           * 不指定数据源,模式使用system数据源
           * @return
           */
          public Object sys() {
              return sysMapper.selectAll();
          }
      
      
          /**
           * 指定数据源查询
           * @return
           */
          @DS("log")
          public Object log() {
              return logMapper.selectAll();
          }
      }
      
      • 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

      7、Controller

      @RestController
      @RequestMapping("/test")
      @Slf4j
      public class TestController {
      
          @Resource
          private TestService testService;
      
          /**
           * 查询system库
           * @return
           */
          @GetMapping("/sys")
          public Object sys() {
              return testService.sys();
          }
      
      
          /**
           * 查询log库
           * @return
           */
          @GetMapping("/log")
          public Object log() {
              return testService.log();
          }
      }
      
      • 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

      8、测试

      启动项目分别访问`http://localhost:9090/test/sys`与`http://localhost:9090/test/log`均有数据输出,则表示配置成功;
      
      • 1

      在这里插入图片描述

      在这里插入图片描述

    • 相关阅读:
      精读《素书》精彩语录及感悟篇(二)
      已加密的PDF怎么解密?只要学会这两招即可轻松解密
      微信小程序开发入门需要学什么?
      Nginx学习笔记06——Nginx反向代理
      C语言strcat函数再学习
      软考高级系统架构设计师系列之:企业集成平台技术的应用和架构设计
      ps2023最新版免费滤镜插件Exposure安装下载教程
      链游:未来游戏发展的新风向
      申请修改 Web of Science 检索页面的错误信息
      数据结构之顺序表
    • 原文地址:https://blog.csdn.net/zhuocailing3390/article/details/125242377