Java中SringBoot服务连接多个MySQL数据源案例实战
- 1.场景
- 2.取消默认的单数据源配置
- 3.自定义多数据源配置文件
- 4.自定义多数据源配置类
- 1.DB1Config.java
- 2.DB2Config.java
- 5.启动项目,测试操作多数据源
1.场景
A服务(供应商)有一套自己的数据库db1;B服务(二次开发)也有一套自己的数据库db2;从B服务查询db1中的一些数据,需要B服务同时链接2个数据库源。
2.取消默认的单数据源配置
修改启动类的@SpringBootApplication注解,(将org.example.db.**.mapper替换为自己项目的mapper文件目录)
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@MapperScan({"org.example.db1.**.mapper","org.example.db1.**.mapper"})
public class TestApplication{
...
}
3.自定义多数据源配置文件
在application.properties 文件(或nacos配置文件)中配置以下数据库连接源:
spring:
# 数据源配置
datasource:
db1:
jdbc-url: xxxxxxxxxxxxxx
username: xxxxxxxxxxxxxx
password: xxxxxxxxxxxxxx
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
db2:
jdbc-url: xxxxxxxxxxxxxx
username: xxxxxxxxxxxxxx
password: xxxxxxxxxxxxxx
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
4.自定义多数据源配置类
1.DB1Config.java
将org.example.db1.**.mapper替换为自己项目的mapper文件目录
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
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 javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "org.example.db1.**.mapper",sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DB1Config {
@Primary
@Bean(name = "db1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource initDataSource(){
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "db1SqlSessionFactory")
public SqlSessionFactory initSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setDbConfig(new GlobalConfig.DbConfig().setUpdateStrategy(FieldStrategy.IGNORED));
bean.setGlobalConfig(globalConfig);
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
mybatisConfiguration.setMapUnderscoreToCamelCase(false);
bean.setConfiguration(mybatisConfiguration);
bean.setPlugins(new Interceptor[]{mybatisPlusInterceptor()});
SqlSessionFactory sessionFactory = bean.getObject();
org.apache.ibatis.session.Configuration configuration = sessionFactory.getConfiguration();
configuration.setMapUnderscoreToCamelCase(false);
return sessionFactory;
}
@Primary
@Bean(name = "db1mybatisPlusInterceptor")
public Interceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return mybatisPlusInterceptor;
}
@Primary
@Bean(name = "db1DataSourceTransactionManager")
public DataSourceTransactionManager initDataSourceTransactionManager(@Qualifier("db1DataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "db1SqlSessionTemplate")
public SqlSessionTemplate initSqlSessionTemplate(@Qualifier("db1SqlSessionFactory")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
2.DB2Config.java
数据库 db2 的配置类要将 @Primary 注解去掉
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
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 javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "org.example.db2.**.mapper",sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DB2Config {
@Bean(name = "db2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource initDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "db2SqlSessionFactory")
public SqlSessionFactory initSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setDbConfig(new GlobalConfig.DbConfig().setUpdateStrategy(FieldStrategy.IGNORED));
bean.setGlobalConfig(globalConfig);
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
mybatisConfiguration.setMapUnderscoreToCamelCase(false);
bean.setConfiguration(mybatisConfiguration);
bean.setPlugins(new Interceptor[]{mybatisPlusInterceptor()});
SqlSessionFactory sessionFactory = bean.getObject();
org.apache.ibatis.session.Configuration configuration = sessionFactory.getConfiguration();
configuration.setMapUnderscoreToCamelCase(false);
return sessionFactory;
}
@Bean(name = "db2mybatisPlusInterceptor")
public Interceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return mybatisPlusInterceptor;
}
@Bean(name = "db2DataSourceTransactionManager")
public DataSourceTransactionManager initDataSourceTransactionManager(@Qualifier("db2DataSource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "db2SqlSessionTemplate")
public SqlSessionTemplate initSqlSessionTemplate(@Qualifier("db2SqlSessionFactory")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
5.启动项目,测试操作多数据源
项目启动后,控制台中看到以下内容即为配置成功
HikariPool-1 - Starting…
HikariPool-1 - Start completed.
HikariPool-2 - Starting…
HikariPool-2 - Start completed.