The Simplest Understanding of StringBoot Configuration Multi-Data Source Tutorial

Keywords: Spring Session Mybatis JDBC

Bullshit does not say much directly on the code "novice on the road God do not spray!"

1 First, you need to build a Springboot project. I won't show you how to build it.

2. Configure two data sources in the configuration file

3. Setting up two config classes


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.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.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 org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

import java.io.IOException;
@Configuration
@MapperScan(basePackages = "com.happyeasygo.mapper.socks",sqlSessionTemplateRef = "soSqlSessionTemplate")
public class Socksdatasource {
	
	  	@Value("${spring.datasource.socks.driver-class-name}")
	    private String driverClassName;
	    @Value("${spring.datasource.socks.url}")
	    private String url;
	    @Value("${spring.datasource.socks.username}")
	    private String username;
	    @Value("${spring.datasource.socks.password}")
	    private String password;
	    
	    /**
	     * Configuring data sources
	     * @return
	     */
	    @Primary
	    @Bean("SoDataSource")
	    public DataSource SoDataSource(){
	        DataSourceBuilder builder = DataSourceBuilder.create();
	        DataSource dataSource = builder
	                .driverClassName(driverClassName)
	                .url(url)
	                .username(username)
	                .password(password)
	                .build();
	        return dataSource;
	    }

	    /**
	     * Configuring Session Factory
	     * @param dataSource
	     * @return
	     */
	    @Primary
	    @Bean("soSqlSessionFactory")
	    public SqlSessionFactory soSqlSessionFactory(@Qualifier("SoDataSource") DataSource dataSource){
	        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	        sqlSessionFactoryBean.setDataSource(dataSource);
	        //Configure the xml Mapping file for the current data source
	        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

	        try {
	            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/socks/*.xml"));
	            return sqlSessionFactoryBean.getObject();
	        } catch (IOException e) {
	            e.printStackTrace();
	            throw new RuntimeException(e);
	        } catch (Exception e) {
	            e.printStackTrace();
	        throw new RuntimeException(e);
	    }
	    }

	    /**
	     * Configuration session
	     * @param sqlSessionFactory
	     * @return
	     */
	    @Primary
	    @Bean(name = "soSqlSessionTemplate")
	    public SqlSessionTemplate soSqlSessionTemplate(@Qualifier("soSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
	        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
	        return sqlSessionTemplate;
	    }

	    /**
	     * affair
	     * @param dataSource
	     * @return
	     */
	    @Primary
	    @Bean(name = "soDataSourceTransactionManager")
	    public DataSourceTransactionManager soDataSourceTransactionManager(@Qualifier("SoDataSource") DataSource dataSource){
	        return new DataSourceTransactionManager(dataSource);
	    }
}

Another one

import java.io.IOException;

import javax.sql.DataSource;

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.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

@Configuration
@MapperScan(basePackages = "com.happyeasygo.mapper.security",sqlSessionTemplateRef = "seSqlSessionTemplate")
public class Securitydatasource {
	         

  	@Value("${spring.datasource.security.driver-class-name}")
    private String driverClassName;
    @Value("${spring.datasource.security.url}")
    private String url;
    @Value("${spring.datasource.security.username}")
    private String username;
    @Value("${spring.datasource.security.password}")
    private String password;
    
    /**
     * Configuring data sources
     * @return
     */
    @Bean("SeDataSource")
    public DataSource SeDataSource(){
        DataSourceBuilder builder = DataSourceBuilder.create();
        DataSource dataSource = builder
                .driverClassName(driverClassName)
                .url(url)
                .username(username)
                .password(password)
                .build();
        return dataSource;
    }

    /**
     * Configuring Session Factory
     * @param dataSource
     * @return
     */
    @Bean("SeSqlSessionFactory")
    public SqlSessionFactory SeSqlSessionFactory(@Qualifier("SeDataSource") DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //Configure the xml Mapping file for the current data source
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        try {
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/security/*.xml"));
            return sqlSessionFactoryBean.getObject();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        throw new RuntimeException(e);
    }
    }

    /**
     * Configuration session
     * @param sqlSessionFactory
     * @return
     */
    @Bean(name = "seSqlSessionTemplate")
    public SqlSessionTemplate seSqlSessionTemplate(@Qualifier("SeSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
        return sqlSessionTemplate;
    }

    /**
     * affair
     * @param dataSource
     * @return
     */
    @Bean(name = "seDataSourceTransactionManager")
    public DataSourceTransactionManager seDataSourceTransactionManager(@Qualifier("SeDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}

Once configured, write mapper, which must correspond to the basePackages and sqlSessionFactoryBean paths in config

 

 

Start after configuring

 

Two interfaces access different databases and query different data OK.

Posted by rocram16 on Sat, 30 Mar 2019 08:57:29 -0700