SpringBoot adds mybatis plus and configures dual data sources

MyBatis plus (MP for short) is an enhancement tool of MyBatis. Based on MyBatis, only enhancements are made and no changes are made to simplify development and improve efficiency.

Mybatis plus official website address

1. Create sb project

2. Add mybatis plus dependency

<!-- Omission above sb This is web Project dependence -->
<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- mybatis-plus rely on -->
<dependency>
		<groupId>com.baomidou</groupId>
		<artifactId>mybatis-plus-boot-starter</artifactId>
		<version>3.0.6</version>
</dependency>

<!-- Connection pool dependency+Database driver package dependency -->
<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<scope>runtime</scope>
</dependency>
<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.1.10</version>
</dependency>

3. Add configuration class

Two data sources, we configure two classes here

Configuration class 1

package com.yanva.dm.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 = DruidConfig.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory1")
public class DruidConfig {
    static final String PACKAGE = "com.yanva.dm.*.mapper";
    static final String MAPPER_LOCATION = "classpath:mapper/*/*.xml";


    @Bean("db1")
    @ConfigurationProperties(prefix = "spring.datasource.druid.master")
    public DataSource druidDataSource1() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

    @Bean("sqlSessionFactory1")
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("db1")DataSource db1) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(db1);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(DruidConfig.MAPPER_LOCATION));
        return sqlSessionFactoryBean.getObject();

    }




}

Configuration class 2

package com.yanva.dm.common.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 = DruidConfig1.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory2")
public class DruidConfig1 {
    static final String PACKAGE = "com.yanva.dm.slavedb.*.mapper";
    static final String MAPPER_LOCATION = "classpath:mapper/slavedb/*.xml";

    @Bean("db2")
    @ConfigurationProperties(prefix = "spring.datasource.druid.slave")
    public DataSource druidDataSource1() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

    @Bean("sqlSessionFactory2")
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("db2")DataSource db2) throws Exception {
        MybatisSqlSessionFactoryBean  sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(db2);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(DruidConfig1.MAPPER_LOCATION));
        return sqlSessionFactoryBean.getObject();

    }




}

* note that PACKAGE and mapper location also have @ ConfigurationProperties(prefix = "spring.datasource.druid.slave")

All need to be configured according to the specific situation.

 

4. Modify the application.yml file (configuration of data source and mybatis plus)

 

server:
  port: 9898
spring:
  datasource:
    name: druidDataSource
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      master:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/home?allowMultiQueries=true&useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
        username: root
        password: root
        initial-size: 10
        max-active: 100
        min-idle: 10
        max-wait: 60000
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
      slave:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/yinzi_database?allowMultiQueries=true&useUnicode=true&serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
        username: root
        password: root

        initial-size: 10
        max-active: 100
        min-idle: 10
        max-wait: 60000
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000

        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
  freemarker:
    template-loader-path: classpath:templates/
    suffix: .ftl

mybatis-plus:
  mapper-locations: classpath:dao/*Mapper.xml
  type-aliases-package: com.yanva.dm.entity

*Note that in the yml file, there must be spaces for attributes and values, and special attention must be paid to indentation. For the same level, the indentation must be the same.

*Note that tables of two data sources cannot have the same name, and mapper files corresponding to different data sources need to be placed in different packages. Because the framework automatically distinguishes data sources by mapping

Tags: Mybatis Spring Druid MySQL

Posted on Fri, 08 Nov 2019 14:34:36 -0500 by Romeo20