在很多业务场景下,我们需要在一个工程里配置多个数据源来实现业务逻辑。在SpringBoot
中也可以实现多数据源并配合Mybatis
框架编写xml文件来执行SQL。在SpringBoot
中,配置多数据源的方式十分便捷。今天我们来实现两个不同方式配置多数据源,一种直接配置,另一种直接获取动态数据源,话不多说,直接上代码!
第一种:配置多个数据源
1. application.properties
server.port=8090
#mysql连接信息
#订单数据源配置
spring.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.master.url=jdbc:mysql://localhost:3306/mall
spring.datasource.master.username=root
spring.datasource.master.password=password
spring.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.master.min-idle=0
spring.datasource.master.max-active=20
#用户数据源配置
spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.second.url=jdbc:mysql://localhost:3306/test
spring.datasource.second.username=root
spring.datasource.second.password=password
spring.datasource.second.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.second.min-idle=0
spring.datasource.second.max-active=20
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=5000
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
#数据源类别
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#初始化大小
spring.datasource.druid.min-idle=0
spring.datasource.druid.max-active=20
2.MasterDataSourceConfig.class
package com.app.multiDataSource.config;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
//master dao层目录
static final String PACKAGE = "com.app.multiDataSource.dao.master";
//master 对应的xml文件
static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
@Primary
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDataSource(){
DataSource dataSource = DruidDataSourceBuilder.create().build();
return dataSource;
}
@Primary
@Bean(name = "masterTransactionManager")
public DataSourceTransactionManager masterTransactionManager(){
return new DataSourceTransactionManager(masterDataSource());
}
@Primary
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MasterDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
3.SecondDataSourceConfig.class
package com.app.multiDataSource.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = SecondDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfig {
//second dao层目录
static final String PACKAGE = "com.app.multiDataSource.dao.second";
//second 对应的xml文件
static final String MAPPER_LOCATION = "classpath:mapper/second/*.xml";
@Value("${spring.datasource.second.url}")
private String url;
@Value("${spring.datasource.second.username}")
private String user;
@Value("${spring.datasource.second.password}")
private String password;
@Value("${spring.datasource.second.driver-class-name}")
private String driverClass;
@Primary
@Bean(name = "secondDataSource")
//@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource secondDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClass);
druidDataSource.setUrl(url);
druidDataSource.setUsername(user);
druidDataSource.setPassword(password);
return druidDataSource;
}
@Primary
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager secondTransactionManager(){
return new DataSourceTransactionManager(secondDataSource());
}
@Primary
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource secondDataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(secondDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(SecondDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
4.Controller
package com.app.multiDataSource.controller;
import com.app.multiDataSource.dao.master.MasterMapper;
import com.app.multiDataSource.dao.second.SecondMapper;
import com.app.multiDataSource.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MasterController {
@Autowired
MasterMapper masterMapper;
@Autowired
SecondMapper secondMapper;
@RequestMapping("/user")
public User getUserInfo(){
User user = masterMapper.getUser("1");
return user;
}
@RequestMapping("/test")
public User getTestUserInfo(){
User user = secondMapper.getUserInfo("1");
return user;
}
}
主要核心方法如上,可以自行编写业务类测试。
第二种,动态配置数据源
https://www.cnblogs.com/xifengxiaoma/p/9888240.html
本人测试 代码已上传git
https://github.com/ZhangNana1111/learning-all.git