springboot +mybatis+oracle 配置多个数据源,配置两个数据库信息

项目中需要用到两个数据库 采取配置

案例源码下载地址:

https://download.csdn.net/download/qq_39477018/12050958

项目结构

springboot +mybatis+oracle 配置多个数据源,配置两个数据库信息_第1张图片

首先在application.properties中配置两个数据源

springboot +mybatis+oracle 配置多个数据源,配置两个数据库信息_第2张图片

数据源1

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.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 javax.sql.DataSource;

//表示这个类为一个配置类
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "cn.witsky.quota.dao.dao1", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSourceConfig1 {
    // 将这个对象放入Spring容器中
    @Bean(name = "test1DataSource")
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.oracle1")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "test1SqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
VFS.addImplClass(SpringBootVFS.class);//mybatis的facroty需要加载SpringBoot独特的虚拟文件系统,才能识别类路
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/m1/*.xml"));
        return bean.getObject();
    }
    @Bean("test1SqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate test1sqlsessiontemplate(
            @Qualifier("test1SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}
@MapperScan(basePackages = "cn.witsky.quota.dao.dao1", sqlSessionFactoryRef = "test1SqlSessionFactory")这个注解 是把当前数据源注入在cn.witsky.quota.dao.dao1 包下
@Primary设置为主数据源,必须要设置,当不知道使用哪个数据源时候,会默认使用主数据源
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));//加载mybatis配置文件

数据源2

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.boot.autoconfigure.jdbc.DataSourceBuilder;
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 = "cn.witsky.quota.dao.dao2", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSourceConfig2 {
    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.oracle2")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory test2SqlSessionFactory(@Qualifier("test2DataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
VFS.addImplClass(SpringBootVFS.class);//mybatis的facroty需要加载SpringBoot独特的虚拟文件系统,才能识别类路
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/m2/*.xml"));
        return bean.getObject();
    }
    @Bean("test2SqlSessionTemplate")
    public SqlSessionTemplate test2sqlsessiontemplate(
            @Qualifier("test2SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

 

dao层

springboot +mybatis+oracle 配置多个数据源,配置两个数据库信息_第3张图片

mapper.xml层:

springboot +mybatis+oracle 配置多个数据源,配置两个数据库信息_第4张图片

springboot +mybatis+oracle 配置多个数据源,配置两个数据库信息_第5张图片

mybatis-config.xml

springboot +mybatis+oracle 配置多个数据源,配置两个数据库信息_第6张图片

service :

springboot +mybatis+oracle 配置多个数据源,配置两个数据库信息_第7张图片

 

ps:得到两个数据源的sqlsession

@Component
public class DynamicSqlSession {
    private static final Logger logger = LoggerFactory.getLogger(DynamicSqlSession.class);
 
    //注入ApplicationContext 容器
    @Autowired
    private ApplicationContext appContext;
 
    public SqlSession getSqlSession(String name){//name 为bean的名称  @bean(名称)
        logger.info("getSqlSession - {}", name);
        if(StringUtils.isBlank(name)){
            throw new RuntimeException("name can't be empty");
        }
        SqlSessionFactory bean = appContext.getBean(name, SqlSessionFactory.class);
        return bean.openSession();
    }
 
    public  T getMapper(String sessionName, Class mapperClass){
        return getSqlSession(sessionName).getMapper(mapperClass);
    }
}

 

SqlSessionFactory test1SqlSessionFactory=appContext.getBean("test1SqlSessionFactory",SqlSessionFactory .class);
SqlSessionFactory test1SqlSessionFactory2=appContext.getBean("test2SqlSessionFactory",SqlSessionFactory .class);

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java,springboot)