springboot多Oracle数据库配置

标题

application.yml文件配置

spring:
  datasource:
    数据库1:
      driver-class-name: oracle.jdbc.driver.OracleDriver
      url: jdbc:oracle:thin:@ip地址/数据库名
      username: xxx
      password: xxx
    数据库2:
      driver-class-name: oracle.jdbc.driver.OracleDriver
      url: jdbc:oracle:thin:@ip地址/数据库名
      username: xxx
      password: xxx

代码文件配置:

	数据库1Config.java
	数据库2Config.java
	mybatis-config.xml

数据库1Config.java:

import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
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
@MapperScan(basePackages = "包名", sqlSessionFactoryRef = "数据库1SqlSessionFactory")

public class 数据库1DataSourceConfig {
    // 将这个对象放入Spring容器中
    @Bean(name = "数据库1DataSource")
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.数据库1")
    public DataSource getDateSource数据库1() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "数据库1SqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory 数据库1SqlSessionFactory(@Qualifier("数据库1DataSource") 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:xx/数据库1mapper/*.xml"));

        return bean.getObject();
    }
    @Bean("数据库1SqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate 数据库1sqlsessiontemplate(
            @Qualifier("数据库1SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}


数据库2Config.java


import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
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 = "包名", sqlSessionFactoryRef = "数据库2SqlSessionFactory")
public class 数据库2DataSourceConfig {
    @Bean(name = "数据库2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.数据库2")
    public DataSource getDateSource数据库2() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "数据库2SqlSessionFactory")
    public SqlSessionFactory 数据库2SqlSessionFactory(@Qualifier("数据库2DataSource") 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:包名/数据库2mapper/*.xml"));
        return bean.getObject();
    }
}

mybatis-config.xml




    
        
    

注意:

1. 一个数据库有一个专用的dao包和mapper包
2. 自定义数据库1和数据库2 还有包名
		注意包名格式
		mapper
			数据库1mapper
				xxxmapper.xml
			数据库2mapper
				xxxmapper.xml
		dao
			数据库1dao
				xxxdao.java
			数据库2dao
				xxxdao.java
3. emmmmm 没有3了,都这么细致了,不会错了吧?

你可能感兴趣的:(日常bug)