MybatisPlus configures multiple data sources

foreword

Today, a newcomer in the team who just graduated has a task in hand to change the project into a multi-data source. It took a long time to get it out, so he asked me for help. I have never done it before, so I will record the pitfalls I have stepped on.

Write data source configuration class

I am using a static multi-data source configuration here

The first data source configuration

  • Note that the @Primary annotation indicates that this is the main library
  • Don't use MybatisSqlSessionFactoryBean wrongly. If Mybatis is not added, the native methods of MybatisPlus will be invalid.
  • Configuration to add pagination
package com.xxx.xxx.configuration;

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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.xxx.xxx.stats.mapper.xxx",sqlSessionFactoryRef = "sepdamSqlSessionFactory")
public class SepdamDataSourceConfig {
    
    
  @Primary
  @Bean(name = "sepdamDataSource")
  @ConfigurationProperties("spring.datasource.sepdam")
  public DataSource sepdamDataSource(){
    
    
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "sepdamSqlSessionFactory")
  public SqlSessionFactory sqlSessionFactory(@Qualifier("sepdamDataSource") DataSource dataSource) throws Exception {
    
    
    MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
    Interceptor interceptor = new PageInterceptor();
    Properties properties = new Properties();
    //数据库
    properties.setProperty("helperDialect", "mysql");
    //是否将参数offset作为PageNum使用
    properties.setProperty("offsetAsPageNum", "true");
    //是否进行count查询
    properties.setProperty("rowBoundsWithCount", "true");
    //是否分页合理化
    properties.setProperty("reasonable", "false");

    interceptor.setProperties(properties);
    sessionFactoryBean.setPlugins(new Interceptor[] {
    
    interceptor});
    return sessionFactoryBean.getObject();
  }
}

The second data source configuration class

  • Note: Compared with the first configuration class, there cannot be @Primary annotation, because there can only be one main library
  • Others are the same as the first configuration class, pay attention to the details, the location of mapper, the location of xml, the name of the data source in the configuration file, etc.
package com.xxx.xxx.configuration;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
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.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;
import java.util.Properties;

@Configuration
@MapperScan(basePackages = "com.xxx.xxx.stats.mapper.db2",sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSource2Config {
    
    
  @Bean(name = "db2DataSource")
  @ConfigurationProperties("spring.datasource.sepdam2")
  public DataSource db2DataSource(){
    
    
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "db2SqlSessionFactory")
  public SqlSessionFactory sqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
    
    
    MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);
    sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
    Interceptor interceptor = new PageInterceptor();
    Properties properties = new Properties();
    //数据库
    properties.setProperty("helperDialect", "mysql");
    //是否将参数offset作为PageNum使用
    properties.setProperty("offsetAsPageNum", "true");
    //是否进行count查询
    properties.setProperty("rowBoundsWithCount", "true");
    //是否分页合理化
    properties.setProperty("reasonable", "false");

    interceptor.setProperties(properties);
    sessionFactoryBean.setPlugins(new Interceptor[] {
    
    interceptor});
    return sessionFactoryBean.getObject();
  }
}

Guess you like

Origin blog.csdn.net/zhang0305/article/details/125520738