标题最近工作的时候遇到里一种特殊情况,需要在一个项目中去查询两个不同的数据库,于是去研究了一下双数据源的使用
我们这里用db1和db2来简单区分两个数据源
spring:
datasource:
db1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://ip:port/数据库名称?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true
username:
password:
db2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://ip:port/数据库名称?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true
username:
password:
server:
port: 8081
包结构如下
Db1Config
package com.wjn.doubledatasource.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(
//指定扫描的包
basePackages = "com.wjn.doubledatasource.db1.mapper",
//指定这些包下的类使用的数据库会话工厂
sqlSessionFactoryRef = "db1SqlSessionFactory")
public class Db1Config {
/**
* 自定义数据源配置信息
* 注解@Primary表示该数据源作为默认数据源
* 注解@ConfigurationProperties(prefix = "spring.datasource.db1")对应配置文件中配置的数据源连接信息
* 方法名作为数据源bean的名称,可使用@Qualifier("")注解引用使用
* @return
*/
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource db1DataSource() {
return DataSourceBuilder.create().build();
}
/**
* 返回data1数据库的会话工厂
* @param dataSource
* @return
* @throws Exception
**/
@Bean
@Primary
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
//扫描mapper文件,我这里为了简单,没有用mapper文件
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("com.wjn.doubledatasource.db1.mapper.*"));
return bean.getObject();
}
/** 返回data1数据库的会话模板
* @param sessionFactory
* @return
* @throws Exception
**/
@Bean(name = "data1SqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sessionFactory) throws Exception{
return new SqlSessionTemplate(sessionFactory);
}
/** 返回data1数据库的事务
* @param dataSource
* @return
**/
@Bean(name = "data1TransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("db1DataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
Db2Config
package com.wjn.doubledatasource.config;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.wjn.doubledatasource.db2.mapper",
sqlSessionFactoryRef = "db2SqlSessionFactory")
public class Db2Config {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource db2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("com.wjn.doubledatasource.db2.mapper.*"));
return bean.getObject();
}
@Bean(name = "data1SqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sessionFactory) throws Exception{
return new SqlSessionTemplate(sessionFactory);
}
@Bean(name = "data1TransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("db1DataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
controller、service、mapper照常写就可以,db1包下的会去访问第一个数据源,db2下的会去访问第二个数据源