mybatis-plus多数据源配置整合druid

1.导入jar包


    com.baomidou
    mybatis-plus-boot-starter
    3.5.2



    com.baomidou
    dynamic-datasource-spring-boot-starter
    3.5.2



    com.alibaba
    druid-spring-boot-starter
    1.2.11


    mysql
    mysql-connector-java
    runtime

2.配置yml

# 数据源配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    druid:
      webStatFilter:
        enabled: true
      stat-view-servlet:
        enabled: true                   # 启用StatViewServlet
        url-pattern: /druid/*           # 访问内置监控页面的路径,内置监控页面的首页是/druid/index.html
        # 控制台管理用户名和密码
        login-username: root
        login-password: 123456
      filter:
        stat:
          enabled: true
          # 慢SQL记录
          log-slow-sql: true
          slow-sql-millis: 1000
          merge-sql: true
        wall:
          config:
            multi-statement-allow: true
    dynamic:
      primary: db1
      strict: false
      druid:
        filters: stat,wall
        # 配置初始化大小、最小、最大
        initial-size: 5
        min-idle: 5
        max-active: 20
        # 配置获取连接等待超时的时间(单位:毫秒)
        max-wait: 60000
        # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        time-between-eviction-runs-millis: 2000
        # 配置一个连接在池中最小生存的时间,单位是毫秒
        min-evictable-idle-time-millis: 600000
        # 用来测试连接是否可用的SQL语句,默认值每种数据库都不相同,这是mysql
        validation-query: select 1
        # 应用向连接池申请连接,并且testOnBorrow为false时,连接池将会判断连接是否处于空闲状态,如果是,则验证这条连接是否可用
        test-whileIdle: true
        # 如果为true,默认是false,应用向连接池申请连接时,连接池会判断这条连接是否是可用的
        test-on-borrow: true
        # 如果为true(默认false),当应用使用完连接,连接池回收连接的时候会判断该连接是否还可用
        test-on-return: true
        # 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle
        pool-prepared-statements: true
        # 要启用PSCache,必须配置大于0,当大于0时, poolPreparedStatements自动触发修改为true,
        # 在Druid中,不会存在Oracle下PSCache占用内存过多的问题,
        # 可以把这个数值配置大一些,比如说100
        max-pool-prepared-statement-per-connection-size: 20
        # 连接池中的minIdle数量以内的连接,空闲时间超过minEvictableIdleTimeMillis,则会执行keepAlive操作
        keep-alive: true
      datasource:
        db1:
          url: jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
          username: username
          password: password
          driver-class-name: com.mysql.cj.jdbc.Driver
        db2:
          url: jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
          username: username
          password: password
          driver-class-name: com.mysql.cj.jdbc.Driver

3.从本地数据库加在数据源

port com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;

import java.sql.ResultSet;import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;

public class MyDataSource extends AbstractJdbcDataSourceProvider {

    private static final Log log = LogFactory.getLog(MyDataSource.class);

    public MyDataSource(@Value("spring.datasource.driver-class-name") String driverClassName,
                        @Value("spring.datasource.url") String url,
                        @Value("spring.datasource.username") String username,
                        @Value("spring.datasource.password") String password) {
        super(driverClassName, url, username, password);
    }

    @Override
    protected Map executeStmt(Statement statement) throws SQLException {
        ResultSet resultSet = statement.executeQuery("SELECT * FROM data_source_config WHERE status = 1");
        Map dataSources = new LinkedHashMap<>();
        while (resultSet.next()) {
            String dataSourceName = resultSet.getString("dataSourceName");
            String driverClassName = resultSet.getString("driverClassName");
            String url = resultSet.getString("url");
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            DataSourceProperty dataSourceProperty = new DataSourceProperty();
            dataSourceProperty.setDriverClassName(driverClassName);
            dataSourceProperty.setUrl(url);
            dataSourceProperty.setUsername(username);
            dataSourceProperty.setPassword(password);
            dataSources.put(dataSourceName, dataSourceProperty);
        }
        return dataSources;
    }
}

你可能感兴趣的:(mybatis,java,spring,boot)