我们在进行双源读写的项目时,需要在yml文件中配置好两个数据源,
server:
port: 9009
eureka:
client:
service-url:
defaultZone: http://localhost:9999/eureka/
register-with-eureka: false
fetch-registry: false
security:
basic:
enabled: false
spring:
datasource:
fc:
driver-class-name: oracle.jdbc.OracleDriver
jdbc-url: jdbc:oracle:thin:@10.0.14.20:1521:stdb
username: formicarydb
password: formicarydb
schema:
- classpath:InitSql/*schema.sql
schema-username: formicarydb
schema-password: formicarydb
data:
- classpath:InitSql/*data.sql
data-password: formicarydb
data-username: formicarydb
initialization-mode: always
nc:
driver-class-name: oracle.jdbc.OracleDriver
jdbc-url: jdbc:oracle:thin:@10.0.14.20:1521:stdb
username: stnc_train
password: stnc_train
jpa:
show-sql: true
mybatis:
fc:
#config-location: classpath:/mapper/config/mybatisConfig.xml #可以注射掉,没用到该配置文件
mapper-locations*: classpath:mapper/FC/*Mapper.xml
# mapper-locations: classpath:mapper/FC/FCBdMarbasclassMapper.xml
type-aliases-package: com.siter.mapper.FC
nc:
mapper-locations*: classpath:mapper/NC/*Mapper.xml
# mapper-location后面的“*”特别重要,缺少“*”号的话后面的通配符就不起作用,然后就会引起意想不到的错误;
# mapper-locations: classpath:mapper/NC/BdMarbasclassMapper.xml
type-aliases-package: com.siter.mapper.NC
#热部署
更改完双数据源后,我们应该分别对两个数据源进行配置类的编写;
首先,我们对主配置类进行编写:
package com.siter.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
/*在此类上使用@PropertiesSource注解寻找这个数据源配置类关联的数据库连接信息的配置文件的地址
(此配置文件可以放在项目外边,便于修改配置文件的连接信息)
*/
@PropertySource("jdbc.properties")
/**
* 该类属于数据源的一个配置类,主要用来绑定我们在yml文件中的数据源(绑定数据源配置);
*/
public class DataSourceConfig {
// destroy-method="close":当数据库连接不使用的时候,将该连接重新放到数据池中
@Bean(name = "NC",destroyMethod = "close")
// @Bean(name = "NC")
// @Qualifier("NC")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.nc")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
//配置数据源二: destroy-method="close":当数据库连接不使用的时候,将该连接重新放到数据池中
@Bean(name = "FC",destroyMethod = "close")
// @Bean(name = "FC")
// @Qualifier("FC")
// @Primary
@ConfigurationProperties(prefix = "spring.datasource.fc")
public DataSource datasource2() {
return DataSourceBuilder.create().build();
}
}
后面是分别两个数据源的配置:
package com.siter.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.nio.file.Path;
/**
* 主数据源的一个配置
*/
@Configuration
//NC接口类的一些包名扫描
@MapperScan(basePackages = {"com.siter.mapper.NC"}, sqlSessionFactoryRef = "NCSqlSessionFactoryBean")
public class NcConfig {
// 必须指定注入哪个数据源,否则找到多个会注入失败
@Autowired
@Qualifier("NC")
private DataSource db1;
@Bean(name = "NCSqlSessionFactoryBean")
@Primary
@ConfigurationProperties(prefix = "mybatis.nc")
public SqlSessionFactoryBean NCSqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 配置数据源
sqlSessionFactoryBean.setDataSource(db1);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/NC/*.xml"));
// sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/FC/FCBdMarbasclassMapper.xml"));
return sqlSessionFactoryBean;
}
// 可选,如果需要通过SqlSessionTemplate来操作持久层就通过@Bean实例化,我们这个例子中没用到,随手写出来了
@Bean(name = "NCSqlSessionTemplate")
@Primary
public SqlSessionTemplate NCSqlSessionTemplate() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(NCSqlSessionFactoryBean().getObject());
return template;
}
}
package com.siter.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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
//FC接口类的一些包名扫描
@MapperScan(basePackages = {"com.siter.mapper.FC"}, sqlSessionFactoryRef = "FCSqlSessionFactoryBean")
public class FcConfig {
// 必须指定注入哪个数据源,否则找到多个会注入失败
@Autowired
@Qualifier("FC")
private DataSource db2;
@Bean(name = "FCSqlSessionFactoryBean")
// @Primary
@ConfigurationProperties(prefix = "mybatis.fc") // 和 配置文件中的前缀保持一致
// @Primary 如果SqlSessionFactoryBean的名字和MybatisDB2Config中的一致(默认方法名),需要加上这个注解,优先注入该SqlSessionFactoryBean
// 这里我们通过bean指定了name,并且方法名也不一样,所以如果情况不一样,看是否需要加入@Primary 。 如果需要两个方法上加一个就行了,都加的话,spring又找不到bean注入啦。。
public SqlSessionFactoryBean FCSqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 配置数据源
sqlSessionFactoryBean.setDataSource(db2);
// 如下的两行代码仅仅用于*.xml文件,如果整个持久层操作没用到xml文件的话,比如使用注解的方式,则无需加
// 解决配置到配置文件中通过*配置找不到mapper文件的问题。 如果不设置这一行,在配置文件中,只能使用数组的方式一个个的罗列出来,并且要指定具体的文件名
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/FC/*.xml"));
// sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(" classpath:mapper/FC/FCBdMarbasclassMapper.xml"));
// 也可以通过在application.yml中配置
//sqlSessionFactoryBean.setTypeAliasesPackage("net.sitir.ergatemd2mq.entity.FC");
return sqlSessionFactoryBean;
}
// 可选,如果需要通过SqlSessionTemplate来操作持久层就通过@Bean实例化
@Bean(name = "FCSqlSessionTemplate")
// @Primary
public SqlSessionTemplate FCSqlSessionTemplate() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(FCSqlSessionFactoryBean().getObject());
return template;
}
}