SpringBoot 2.1.10.RELEASE 结合MybatisPlus 3.2.0 版本整合Druid多数据源

  • Demo 结构及源码

    • Demo结构图
      SpringBoot 2.1.10.RELEASE 结合MybatisPlus 3.2.0 版本整合Druid多数据源_第1张图片
    • 源码链接: https://download.csdn.net/download/weixin_44187730/12026906
  • pom.xml添加依赖

<dependencies>
 
        <dependency>
            <groupId>com.spring4allgroupId>
            <artifactId>swagger-spring-boot-starterartifactId>
            <version>1.9.1.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.42version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>

        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.2.0version>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-generatorartifactId>
            <version>3.2.0version>
        dependency>
        <dependency>
            <groupId>org.apache.velocitygroupId>
            <artifactId>velocity-engine-coreartifactId>
            <version>2.1version>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>dynamic-datasource-spring-boot-starterartifactId>
            <version>2.5.7version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
dependencies>
  • MybatisPlus整合

    • MP整合: https://blog.csdn.net/weixin_44187730/article/details/101620137
  • Druid多数据源配置application-druid.yml

#Druid多数据源配置信息
spring:
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  datasource:
    #启动执行SQL脚本 ALWAYS:每次初始化  NEVER:不初始化  EMBEDDED:仅初始化一次
    initialization-mode: EMBEDDED
    schema:
      - classpath:sql/master_db.sql
      - classpath:sql/slave_db.sql
    type: com.alibaba.druid.pool.DruidDataSource
    dynamic:
      #指定默认链接的 数据源
      primary: master
      datasource:
        #指定多个数据源链接信息 名称可自己指定
        master:
          url: jdbc:mysql://127.0.0.1:3306/master_db?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
          username: root
          password: admin
          driver-class-name: com.mysql.jdbc.Driver
        slave:
          url: jdbc:mysql://127.0.0.1:3306/slave_db?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
          username: root
          password: admin
          driver-class-name: com.mysql.jdbc.Driver
    druid:
      #初始化大小
      initial-size: 5
      min-idle: 5
      max-active: 20
      #等待超时时间
      max-wait: 60000
      #心跳检测
      time-between-eviction-runs-millis: 60000
      #配置连接在池的最小时间
      min-evictable-idle-time-millis: 300000
      validation-query: SELCET 1
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      #打开PSCache
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      #配置监控统计连接的filters,去掉后监控sql无法统计
      filters: stat,wall,slf4j
      connection-properties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
      #配置DruidStatFilter
      web-stat-filter:
        enabled: true
        url-pattern: "/*"
        exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
      #配置DruidStatViewServlet
      stat-view-servlet:
        url-pattern: "/druid/*"
        #IP白名单
        allow: 127.0.0.1,192.168.44.*
        #IP黑名单
        deny: 192.168.15.85
        reset-enable: false
        # druid监控台登录用户密码
        login-username: admin
        login-password: 123456
  • 启用相关配置application.yml
#端口配置
server:
  port: 9088
  servlet:
    context-path: /
spring:
  application:
    name: boot-druid-mp-dynamic-db
  profiles:
    active: mp,druid

  • 编写测试方法
    • 在需要切换数据源的地方加上@DS(“数据源名称”)
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @DS("master")
    @Override
    public List<Users> getAllUserFromMaster() {

        return this.baseMapper.selectList(null);
    }

    @DS("slave")
    @Override
    public List<Users> getAllUserFromSlave() {

        return this.baseMapper.selectList(null);
    }
}

  • 编写测试接口
@RestController
@RequestMapping("/us")
public class UsersController {

    @Autowired
    UsersService usersService;


    @GetMapping("/getAllUserFromMaster")
    public Object getAllUserFromMaster() {

        return usersService.getAllUserFromMaster();
    }

    @GetMapping("/getAllUserFromSlave")
    public Object getAllUserFromSlave() {

        return usersService.getAllUserFromSlave();
    }
}
  • 测试数据源切换
    SpringBoot 2.1.10.RELEASE 结合MybatisPlus 3.2.0 版本整合Druid多数据源_第2张图片
    SpringBoot 2.1.10.RELEASE 结合MybatisPlus 3.2.0 版本整合Druid多数据源_第3张图片

你可能感兴趣的:(SpringBoot)