一個項目裡面有多個資料源,配置如下
import javax.sql.DataSource;
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;
@Configuration
public class DataSourceConfig {
@Bean(name = "db2")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.first") // application.properteis中對應屬性的字首
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "mysql")
@ConfigurationProperties(prefix = "spring.datasource.second") // application.properteis中對應屬性的字首
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
}
首先建立資料源,資料庫位置資訊
其次配置雙資料源所需要掃描的實體類以及xml的配置資訊
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.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
@MapperScan(basePackages = { "com.pactera.vds.member.mapper.second" }, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {
@Autowired
@Qualifier("db2")
private DataSource db2;
@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(db2); // 使用titan資料源, 連接配接titan庫
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factoryBean.setVfs(SpringBootVFS.class); // 必須這麼樣設定,否則,不能識别别名
factoryBean.setTypeAliasesPackage("com.pactera.vds.member.model");
factoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/second/*.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1());
// 使用上面配置的Factory
return template;
}
}
修改資訊basePackages = “” 後面的值,辨別着mapper位置, factoryBean.setTypeAliasesPackage("com.pactera.vds.member.model");對應着實體對象的包名, factoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/second/*.xml"));對應這着xml的配置資訊,第二個資料源配置同理
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.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
@MapperScan(basePackages = { "com.pactera.vds.member.mapper" }, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {
@Autowired
@Qualifier("mysql")
private DataSource mysql;
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(mysql);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factoryBean.setVfs(SpringBootVFS.class); // 必須這麼樣設定,否則,不能識别别名
factoryBean.setTypeAliasesPackage("com.pactera.vds.member.model");
factoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
return template;
}
}
建立兩個包的位置 : com.pactera.vds.member.mapper;com.pactera.vds.member.mapper.second
其次建立xml檔案位置 : /member/src/main/resources/mybatis ;/member/src/main/resources/mybatis/second
最後在配置資訊上寫一下資料源的資訊
spring:
session:
store-type: none
datasource:
first:
url: xxxxxxxxxx
username: xxxxxxxxxx
password: xxxxxxxxxx
driver-class-name: com.mysql.jdbc.Driver
second:
url: xxxxxxxxxx
username: xxxxxxxxxx
password: xxxxxxxxxx
driver-class-name: com.mysql.jdbc.Driver
driver-class-name 辨別驅動
以上就完成了雙資料源的資訊配置,
package com.pactera.vds.member.mapper;
包裡面寫檔案讀取的是資料庫mysql 這個資料源