美文网首页
spring boot + mybatis plus 实现多数据

spring boot + mybatis plus 实现多数据

作者: _大叔_ | 来源:发表于2020-07-21 09:51 被阅读0次

    官方给出了基本操作

    传送门,但在spring boot整合中还是会出现问题。如下:

    Description:
    
    Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
    

    只需要在 配置文件中加如下:

    spring:
      autoconfigure:
        exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
    

    用的是 Druid 的数据源,所以排除 Druid 使用 spring boot 自带的。

    实际应用

    mysql pom

            <!-- 数据持久相关配置 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus}</version>
            </dependency>
    
            <!-- 驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${db-drive}</version>
            </dependency>
    
            <!-- 多数据源 -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
                <version>3.1.1</version>
            </dependency>
    

    配置文件

    spring:
      main:
        allow-bean-definition-overriding: true
      autoconfigure:
        exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
      datasource:
        # 配置监控
        druid:
          stat-view-servlet:
            # 在1.1.9版本不需要,以上必须要
            enabled: true
            url-pattern: "/druid/*"
            # IP白名单(没有配置或者为空,则允许所有访问)
            allow: 127.0.0.1
            # IP黑名单 (存在共同时,deny优先于allow)
            deny: 192.168.1.73
            # 禁用HTML页面上的“Reset All”功能
            reset-enable: true
            # 登录名
            login-username: admin
            # 登录密码
            login-password: admin@2020
          web-stat-filter:
            enabled: true
            url-pattern: "/*"
            exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
        dynamic:
          druid:
            # 配置监控统计拦截的filters,去掉后,监控界面的sql无法统计
            # filter 提供的所有log 是为了 输出JDBC执行的日志
            filters: stat,wall
            # 初始化连接大小
            initial-size: 30
            # 最小空闲连接数
            min-idle: 20
            # 最大连接数
            maxActive: 200
            # 获取连接时最大等待时间,单位毫秒,使用
            maxWait: 10000
            # maxWait 并发效率会有所下降,maxWait 会造成公平锁,useUnfairLock 使用非公平锁
            useUnfairLock: true
            # 检测连接是否可用的测试语句 如果为 null,testOnBorrow testOnReturn testWhileIdle 都不起作用
            validation-query: 'select 1'
            # testWhileIdle(空闲时检测):如果为true(默认true),当应用向连接池申请连接,并且testOnBorrow为false时,连接池将会判断连接是否处于空闲状态,如果是,则验证这条连接是否可用。
            # testWhileIdle什么时候会起作用? 获取连接时 且 testOnBorrow=false testWhileIdle=true
            testWhileIdle: true
            # testOnBorrow(获取连接时检测) 检测池里连接的可用性 false 不检测 true 检测.但消耗性能。
            # 假如连接池中的连接被数据库关闭了,应用通过连接池getConnection时,可能获取到这些不可用的连接,且这些连接如果不被其他线程回收的话,它们不会被连接池被废除,也不会重新被创建,
            # 占用了连接池的名额,项目本身作为服务端,数据库链接被关闭,客户端调用服务端就会出现大量的timeout,客户端设置了超时时间,然而主动断开,服务端必然出现close_wait。
            testOnBorrow: false
            # 连接保持空闲而不被驱逐的最小时间,如果说连接的真正空闲时间 等于该值,则关闭物理连接
            # minEvictableIdleTimeMillis: 30000
            testOnReturn: false
          primary: wiedp
          datasource:
            # 主库
            wiedp:
              url: jdbc:mysql://10.xx.xx.100:3306/wiedp?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&&serverTimezone=Asia/Shanghai
              driver-class-name: com.mysql.cj.jdbc.Driver
              type: com.alibaba.druid.pool.DruidDataSource
              username: xx
    #          password: xx@2020
              password: mysql@dev.2020
            # 采集库
            iedp:
              url: jdbc:mysql://10.xx.xx.101:3306/iedp?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&&serverTimezone=Asia/Shanghai
              driver-class-name: com.mysql.cj.jdbc.Driver
              type: com.alibaba.druid.pool.DruidDataSource
              username: xxxx
              password: xxxx@xxxx
    

    官方完整案例

    注意:如果是低版本的 druid-spring-boot-starter 可能部分参数修改无效,建议使用高版本。

    相关文章

      网友评论

          本文标题:spring boot + mybatis plus 实现多数据

          本文链接:https://www.haomeiwen.com/subject/pneofktx.html