SpringBoot integrates Druid to configure multiple data sources

Table of contents

1. Initialize the project

1.1. Initialization project

1.2. Add dependencies

1.3. Configure the yml file

1.4. Add the @MapperScan annotation to the Spring Boot startup class to scan the Mapper folder

1.5. Configure the use of data sources

1.5.1. Annotation method

1.5.2. AOP-based manual implementation of multiple data source native methods

2. Results display


Mybatis-Plus: Introduction | MyBatis-Plus (baomidou.com)

1. Initialize the project

Before officially starting, initialize a springboot project

1.1. Initialization project

Create an empty Spring Boot project

You can use  Spring Initializer (opens new window) to quickly initialize a Spring Boot project

1.2. Add dependencies

Maven Central Warehouse

Add Druid and JDBC driver dependencies in pom.xml

        <!-- 阿里数据库druid连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.6</version>
        </dependency>       
        <!-- 多数据源配置dynamic-datasource-spring-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
       <!-- oracle驱动 -->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
       <!--mysql驱动:注意和mysql版本对应,实际应用中根据需要自行引入相关数据源驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.4</version>
            <scope>runtime</scope>
        </dependency>
        <!--devtools热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
        <!--Hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.18</version>
        </dependency>
        <!-- 整合mybatis plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>

1.3. Configure the yml file

1) application.yml: configure data source and Druid monitoring

# 配置多数据源  使用:如果需要调用不用的数据库源,只需要再impl里面使用注释@DS("数据源名称")即可
#  Tomcat  服务配置server:server:server:
server:
  port: 8085
spring:
  devtools:
    restart:
      enabled: true
      additional-paths: src/main/java
      exclude: resources/*
  profiles:
    active: pro
  datasource:
    druid:
      # 下面为连接池的补充设置,应用到上面所有数据源中
      # 初始化大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 20
      # 配置获取连接等待超时的时间
      max-wait: 60000
      # 每60秒运行一次空闲连接回收器
      time-between-eviction-runs-millis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 300000
      # 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.
      test-while-idle: true
      # 建议配置为false。获取连接时执行validationQuery检测连接是否有效,这个配置会降低性能。
      test-on-borrow: false
      # 建议配置为false。获取连接时执行validationQuery检测连接是否有效,这个配置会降低性能。
      test-on-return: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      pool-prepared-statements: true
      # 验证连接是否可用,使用的SQL语句
      validation-query: SELECT 1
      #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall用于防火墙
      max-pool-prepared-statement-per-connection-size: 20
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      # 合并多个DruidDataSource的监控数据
      use-global-data-source-stat: true
      # 监控过滤器  可视化页面地址:  http://localhost:8085/druid/login.html
      webStatFilter:
        #  是否开启监控
        enabled: true
      # 提供监控信息展示的html页面;提供监控信息的JSON API
      statViewServlet:
        enabled: true
        # 设置白名单,不填则允许所有访问
        allow:
        #  监控路径
        url-pattern: /druid/*
        # 控制台管理用户名和密码
        login-username: admin
        login-password: admin
      # 慢sql记录
      filter:
        stat:
          enabled: true # 开启DruidDataSource状态监控
          log-slow-sql: true # 开启慢SQL记录功能,启用后如果遇到执行很慢的 SQL,便会输出到日志中,
          slow-sql-millis: 5000  # 默认3000毫秒,这里超过5s,就是慢,记录到日志
          merge-sql: true
        # 防御SQL注入
        wall:
          config:
            multi-statement-allow: true

  # 自动设置json返回格式
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss


# mybatis-plus相关配置
mybatis-plus:
  configuration:
    #开启sql日志
    #    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
    map-underscore-to-camel-case: true
    # 解决oracle更新数据为null时无法转换报错,mysql不会出现此情况
    jdbc-type-for-null: 'null'
  #实体类所在包
  type-aliases-package: com.batchUtil.model
  # xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
  mapper-locations: classpath:mapper/*.xml

# 设置日志级别
logging:
  level:
    root: INFO # INFO级别以及以上级别的日志输出到控制台上
    #com.example.demo.dao: debug,可以控制单个包下日志级别
    # 设置logback.xml位置,如果logback不在resources或者不是默认文件名
    # config: classpath:log/logback.xml

2) application-pro.yml: configure multiple data sources

#mysql和阿里druid配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    dynamic:
      primary: jc-mysqldb #设置默认的数据源或者数据源组
      strict: false
      datasource:
        zw-mysqldb:
        # driver-class需要注意mysql驱动的版本(com.mysql.cj.jdbc.Driver 或 com.mysql.jdbc.Driver)
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://192.168.6.13:3306/500140?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai?relaxAutoCommit=true&zeroDateTimeBehavior=convertToNull
          username: root
          password: 123456
        jc-mysqldb:
          driver-class-name: com.mysql.jdbc.Driver
          #捷成数据中心
          url: jdbc:mysql://192.168.5.15:3306/dc?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
          username: duizhang_dyt
          password: abc#123#321#cba

1.4. Add annotations to the Spring Boot startup class  @MapperScan and scan the Mapper folder

@MapperScan(value = "com.police.violation.mapper")

1.5. Configure the use of data sources

There are two ways to use data sources, one is annotation and the other is aspect

1.5.1. Annotation method

This method is more suitable for a single project, the application scenario is not complicated, and there are few mapper files

@DS annotation description:

1. Annotations are on methods, classes, interfaces, and enumerations. At the same time, there is a principle of proximity, and annotations on methods take precedence over annotations on classes;

2. Do not use the @DS annotation, the default primary data source;

package com.batchUtil.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.batchUtil.model.MacConnect;

import java.util.List;

/**
 * @author Administrator
 */
@DS("zw-mysqldb")
public interface MacConnectMapper {

    /**
     * 根据编码获取车站数据库连接信息
     *
     * @return
     */
    MacConnect selectByMacId(String macId);


    List<MacConnect> selectStationMac();


}

1.5.2. AOP-based manual implementation of multiple data source native methods

This method is more suitable for large projects, unified and standardized management, which is not only a show of skill, but also in line with the actual situation

1) maven dependency, the dependency is the same as before, on this basis, the sliced ​​bread is added

        <!-- aop 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

2) Configuration file: configure as before

Note: Spring Boot 2.X version no longer supports configuration inheritance. If there are multiple data sources, all configurations of each data source need to be configured separately, otherwise the configuration will not take effect;

3) Multiple data source name classes

Main data source configuration class:

/**   
 * 类名称:d   
 * 类描述:   
 * 创建人:Administrator
 * 创建时间:2020年4月1日 下午4:45:12   
 * 修改人:Administrator
 * 修改时间:2020年4月1日 下午4:45:12   
 * 修改备注:   
 * @version       
 */

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 
import javax.sql.DataSource;
 
@Configuration
//配置mapper路径
@MapperScan(basePackages = "com.ykx.transinfo.mapper", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSourceConfig1 {
 
    // 将这个对象放入Spring容器中
    @Bean(name = "test1DataSource")
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.one")
    public DataSource getDateSource1()
    {
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name = "test1SqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource datasource)
            throws Exception
    {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/one/*.xml"));
        return bean.getObject();
    }
 
    @Bean("test1SqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate test1SqlSessionTemplate(
            @Qualifier("test1SqlSessionFactory") SqlSessionFactory sessionFactory)
    {
        return new SqlSessionTemplate(sessionFactory);
    }
}

Secondary data source configuration class:


/**   
 * 类名称:s   
 * 类描述:   
 * 创建人:Administrator
 * 创建时间:2020年4月1日 下午4:45:38   
 * 修改人:Administrator
 * 修改时间:2020年4月1日 下午4:45:38   
 * 修改备注:   
 * @version       
 */

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
 
@Configuration
@MapperScan(basePackages = "com.ykx.transinfo.mysqlmapper", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSourceConfig2 {
    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.two")
    public DataSource getDateSource2()
    {
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory test2SqlSessionFactory(@Qualifier("test2DataSource") DataSource datasource)
            throws Exception
    {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/two/*.xml"));
        return bean.getObject();
    }
 
    @Bean("test2SqlSessionTemplate")
    public SqlSessionTemplate test2SqlSessionTemplate(
            @Qualifier("test2SqlSessionFactory") SqlSessionFactory sessionFactory)
    {
        return new SqlSessionTemplate(sessionFactory);
    }
}

4).Project structure

Note: The focus is on the correspondence between the mapper structure and the path in the configuration class

5). Startup class - the startup class needs to unload the automatic configuration of the data source 

package com.ykx.transinfo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
// @MapperScan 和dao层@Mapper 二选一
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableScheduling
@MapperScan("com.ykx.transinfo.mapper,com.ykx.transinfo.mysqlmapper")
public class DataServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(DataServerApplication.class, args);
	}

}

Subsequent mapper and service writing methods are no different from usual

The data source may encounter transaction problems: https://blog.csdn.net/u011974797/article/details/130154340

2. Results display

The log will be printed when the service starts:

com.alibaba.druid.pool.DruidDataSource   : {dataSource-1,first} inited
com.alibaba.druid.pool.DruidDataSource   : {dataSource-2,second} inited
druid监控页面:

References: https://blog.csdn.net/u011974797/article/details/130109195

Guess you like

Origin blog.csdn.net/qq_20957669/article/details/130410579