最简单理解的 StringBoot 配置多数据源教程

废话不多说 直接上代码  “新手上路 大神勿喷!”

1首先需要需要搭建一个Springboot的项目 这里就不介绍怎么去搭建了

最简单理解的 StringBoot 配置多数据源教程_第1张图片

2  配置文件中配置两个数据源 最简单理解的 StringBoot 配置多数据源教程_第2张图片

设置2个config类

最简单理解的 StringBoot 配置多数据源教程_第3张图片



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;
	    
	    /**
	     * 配置数据源
	     * @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;
	    }

	    /**
	     * 配置会话工厂
	     * @param dataSource
	     * @return
	     */
	    @Primary
	    @Bean("soSqlSessionFactory")
	    public SqlSessionFactory soSqlSessionFactory(@Qualifier("SoDataSource") DataSource dataSource){
	        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	        sqlSessionFactoryBean.setDataSource(dataSource);
	        //配置当前数据源的xml映射文件
	        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);
	    }
	    }

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

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

另外一个


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;
    
    /**
     * 配置数据源
     * @return
     */
    @Bean("SeDataSource")
    public DataSource SeDataSource(){
        DataSourceBuilder builder = DataSourceBuilder.create();
        DataSource dataSource = builder
                .driverClassName(driverClassName)
                .url(url)
                .username(username)
                .password(password)
                .build();
        return dataSource;
    }

    /**
     * 配置会话工厂
     * @param dataSource
     * @return
     */
    @Bean("SeSqlSessionFactory")
    public SqlSessionFactory SeSqlSessionFactory(@Qualifier("SeDataSource") DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //配置当前数据源的xml映射文件
        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);
    }
    }

    /**
     * 配置会话
     * @param sqlSessionFactory
     * @return
     */
    @Bean(name = "seSqlSessionTemplate")
    public SqlSessionTemplate seSqlSessionTemplate(@Qualifier("SeSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
        return sqlSessionTemplate;
    }

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

配置好了 之后  编写mapper 这里一定要和config里面的basePackages 还有sqlSessionFactoryBean的路径相对应

最简单理解的 StringBoot 配置多数据源教程_第4张图片

 

 最简单理解的 StringBoot 配置多数据源教程_第5张图片

 配置好了之后启动 

最简单理解的 StringBoot 配置多数据源教程_第6张图片

最简单理解的 StringBoot 配置多数据源教程_第7张图片

两个接口访问不同的数据库查询出来不同的数据 OK啦就  

下一篇文章介绍StringBoot  多数据源的动态定时轮询

你可能感兴趣的:(Stringboot,java,数据源)