Mybatisplus configuring multiple data sources (I)

goodlook0123 2022-02-13 08:12:07 阅读数:82

mybatisplus configuring multiple data sources

I haven't posted for a long time , One of them was ready to sum up the knowledge , However, due to various reasons, it has been delayed so far 9 More than a month. , Many of the prepared themes cannot be written again for various reasons , The knowledge of writing is not very deep , Just as a summary of myself . Have good ideas to comment on , Learning together .

Today I'm going to share mybatisplus Configure multiple data sources ( By way of configuration file ). This configuration is suitable for determining the number of scene data sources , And they all have the need to develop according to the data source .

The version information used in the project is as follows :

springbooot:2.3.2.RELEASE

mybatisplus:3.4.3.3

Go straight to the code , establish Consolidated profile DruidConfiguration :

package com.xx.xx.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* Project name :md
* founder :
* Creation time :2021/11/15 17:58
* Class name :DruidConfiguration
* Class description : Class description
*/
@Configuration
public class DruidConfiguration {
/**
* @author:
* Creation time : 2021/11/17 10:20
* describe : xx
* param
* return
*/
@ConfigurationProperties(prefix = "spring.datasource.xx")
@Bean
public DataSource xx(){
return new DruidDataSource();
}
/**
* @author: xx
* Creation time : 2021/11/17 10:20
* describe : xx
* param
* return
*/
@ConfigurationProperties(prefix = "spring.datasource.mddatasource")
@Bean
public DataSource mdDatasource(){
return new DruidDataSource();
}
// To configure Druid Monitoring of
//1、 Configure a management background Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","sa");
initParams.put("loginPassword","zjiang*76");
initParams.put("allow","");// The default is to allow all access
initParams.put("deny","223.124.153.078");
bean.setInitParameters(initParams);
return bean;
}
//2、 To configure a web Monitored filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}

Create a configuration file for the data source :XXErpDataSourceConfig

package com.xx.mdstream.config;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.stdout.StdOutImpl;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* Project name :middleground
* founder :
* Creation time :2021/11/15 18:09
* Class name :SelfDataSourceConfig
* Class description : xx
*/
@Configuration
@MapperScan(basePackages = "com.xx.mdstream.dao.xxerpDataSource", sqlSessionTemplateRef = "sourceSqlSessionTemplate")
public class XXErpDataSourceConfig {
@Bean
@Primary
public SqlSessionFactory xxErpSqlSessionFactory(@Qualifier("xxErp1076datasource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
// scanning xml File path
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/xxerp/*.xml"));
// Set up printing sql sentence
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
mybatisConfiguration.setLogImpl(StdOutImpl.class);
bean.setConfiguration(mybatisConfiguration);
return bean.getObject();
}
@Bean
@Primary
public DataSourceTransactionManager sourceTransactionManager(@Qualifier("xxErp1076datasource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
@Primary
public SqlSessionTemplate sourceSqlSessionTemplate(@Qualifier("cqErpSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

Create configuration file of data source 2 MdDataSourceConfig

package com.xx.mdstream.config;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.stdout.StdOutImpl;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* Project name :middleground
* founder :
* Creation time :2021/11/15 18:09
* Class name :SelfDataSourceConfig
* Class description :
*/
@Configuration
@MapperScan(basePackages = {"com.xx.mdstream.dao.mdDataSource","com.xx.common.dao"}, sqlSessionTemplateRef = "mdDataSourceSqlSessionTemplate")
public class MdDataSourceConfig {
@Bean
@Primary
public SqlSessionFactory mdSqlSessionFactory(@Qualifier("mdDatasource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
// scanning xml File path
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/md/*.xml"));
// Set up printing sql sentence
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
mybatisConfiguration.setLogImpl(StdOutImpl.class);
bean.setConfiguration(mybatisConfiguration);
return bean.getObject();
}
@Bean
@Primary
public DataSourceTransactionManager mdDataSourceTransactionManager(@Qualifier("mdDatasource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
@Primary
public SqlSessionTemplate mdDataSourceSqlSessionTemplate(@Qualifier("mdSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

The structure of the project is as follows :

  It's very simple to realize , Copy the above code and start to run , The source code supporting database type translation is as follows :

copyright:author[goodlook0123],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130812048225.html