说明
- 随着应用用户数量的增加,相应的并发请求的数量也会跟着不断增加,慢慢地,单个数据库已经没有办法满足我们频繁的数据库操作请求了。
- 在某些场景下,我们可能会需要配置多个数据源,使用多个数据源(例如实现数据库的读写分离)来缓解系统的压力等,同样的,SpringBoot官方提供了相应的实现来帮助开发者们配置多数据源,据我目前所了解到的,一般分为两种方式静态与动态(分包和AOP)。本文使用的是静态的方式。
- 因为我们控制是多数据源DataSource,而并不用关心到底是用哪种方式去使用,比如源生JDBC、MyBatis、Hibernate等上层的使用方法都是一样的。所以本文以本人常用的MyBatis-Plus操作数据源为例。
- 本文中数据库用的是mysql5.7。完整代码地址在结尾!!
- 静态方式方式说白了,其实就是根据不同的包路径定义多个数据源,想用哪个用哪个呗。
第一步,分别创建两个数据库db1,db2,sql如下
db1
CREATE DATABASE db1;
use db1;
CREATE TABLE user
(
id INT NOT NULL COMMENT '主键ID',
name VARCHAR(50) NULL DEFAULT NULL COMMENT '姓名',
age INT NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
)CHARSET=utf8 ENGINE=InnoDB COMMENT '用户表';
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
select * from user;
db2
CREATE DATABASE db2;
use db2;
CREATE TABLE task
(
id INT NOT NULL COMMENT '主键ID',
name VARCHAR(50) NULL DEFAULT NULL COMMENT '姓名',
age INT NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
)CHARSET=utf8 ENGINE=InnoDB COMMENT '任务表';
INSERT INTO task (id, name, age, email) VALUES
(1, '李白', 18, 'test1@baomidou.com'),
(2, '露娜', 20, 'test2@baomidou.com'),
(3, '韩信', 28, 'test3@baomidou.com'),
(4, '橘右京', 21, 'test4@baomidou.com'),
(5, '百里玄刺', 24, 'test5@baomidou.com');
select * from task;
第二步,在pom.xml加入依赖,如下
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatisPlus 核心库 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
第三步,在application.yml配置多数据源,mybatis-plus相关配置
spring:
datasource:
db1:
jdbc-url: jdbc:mysql://xxx:3306/db1?useUnicode=true&characterEncoding=utf-8
username: xxx
password: xxx
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
最小空闲连接数量
minimum-idle: 5
连接池最大连接数,默认是10
maximum-pool-size: 15
连接池名称
pool-name: MyHikariCPOfMaster
此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
max-lifetime: 1800000
数据库连接超时时间,默认30秒,即30000
connection-timeout: 20000
空闲连接存活最大时间,默认600000(10分钟)
idle-timeout: 180000
此属性控制从池返回的连接的默认自动提交行为,默认值:true
auto-commit: true
connection-test-query: SELECT 1
db2:
jdbc-url: jdbc:mysql://xxx:3306/db2?useUnicode=true&characterEncoding=utf-8
username: xxx
password: xxx
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
最小空闲连接数量
minimum-idle: 5
连接池最大连接数,默认是10
maximum-pool-size: 15
连接池名称
pool-name: MyHikariCPOfMaster
此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
max-lifetime: 1800000
数据库连接超时时间,默认30秒,即30000
connection-timeout: 20000
空闲连接存活最大时间,默认600000(10分钟)
idle-timeout: 180000
此属性控制从池返回的连接的默认自动提交行为,默认值:true
auto-commit: true
connection-test-query: SELECT 1
application:
name: staticmultipledatasources-demo-server
# mybatis-plus相关配置
mybatis-plus:
configuration:
#不开启二级缓存
cache-enabled: false
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
port: 8092
第四步,将spring boot自带的DataSourceAutoConfiguration禁掉,因为它会读取application.yml文件的spring.datasource.*属性并自动配置单数据源。在@SpringBootApplication注解中添加exclude属性即可,如下
StaticMultipleDataSourcesApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class StaticMultipleDataSourcesApplication {
public static void main(String[] args) {
SpringApplication.run(StaticMultipleDataSourcesApplication.class, args);
}
}
第五步,创建配置文件DB1DataSourceConfig,DB2DataSourceConfig,MybatisPlusConfig,如下
DB1DataSourceConfig
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @Description:
* @Author: jinhaoxun
* @Date: 2020/5/9 8:27 下午
* @Version: 1.0.0
*/
@Configuration
@MapperScan(basePackages = {"com.luoyu.staticmultipledatasources.mapper.db1"}, sqlSessionTemplateRef ="db1SqlSessionTemplate")
public class DB1DataSourceConfig {
/**
* 创建数据源
* @return
*/
@Bean(name = "db1DataSource")
@ConfigurationProperties(prefix="spring.datasource.db1")
@Primary
public DataSource getDB1DataSource() {
return DataSourceBuilder.create().build();
}
/**
* 创建SessionFactory
* @param db1DataSource
* @return
* @throws Exception
*/
@Bean(name = "db1SqlSessionFactory")
@Primary
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource db1DataSource, @Qualifier("mybatisplusConfiguration") org.apache.ibatis.session.Configuration configuration) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(db1DataSource);
// 使mybatis-plus配置生效,加载顺序问题
bean.setConfiguration(configuration);
// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/luoyu/staticmultipledatasources/mapper/db1/xml/*.xml"));
bean.setTypeAliasesPackage("com.luoyu.staticmultipledatasources.entity");
return bean.getObject();
}
/**
* 创建事务管理器
* @param db1DataSource
* @return
*/
@Bean("db1TransactionManger")
@Primary
public DataSourceTransactionManager db1TransactionManger(@Qualifier("db1DataSource") DataSource db1DataSource){
return new DataSourceTransactionManager(db1DataSource);
}
/**
* 创建SqlSessionTemplate
* @param db1SqlSessionFactory
* @return
*/
@Bean(name = "db1SqlSessionTemplate")
@Primary
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory db1SqlSessionFactory){
return new SqlSessionTemplate(db1SqlSessionFactory);
}
}
DB2DataSourceConfig
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @Description:
* @Author: jinhaoxun
* @Date: 2020/5/9 8:26 下午
* @Version: 1.0.0
*/
@Configuration
@MapperScan(basePackages = "com.luoyu.staticmultipledatasources.mapper.db2",sqlSessionTemplateRef ="db2SqlSessionTemplate")
public class DB2DataSourceConfig {
/**
* 创建数据源
* @return
*/
@Bean(name = "db2DataSource")
@ConfigurationProperties(prefix="spring.datasource.db2")
@Primary
public DataSource getDB2DataSource() {
return DataSourceBuilder.create().build();
}
/**
* 创建SessionFactory
* @param db2DataSource
* @return
* @throws Exception
*/
@Bean(name = "db2SqlSessionFactory")
@Primary
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource db2DataSource, @Qualifier("mybatisplusConfiguration") org.apache.ibatis.session.Configuration configuration) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(db2DataSource);
// 使mybatis-plus配置生效,加载顺序问题
bean.setConfiguration(configuration);
// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/luoyu/staticmultipledatasources/mapper/db2/xml/*.xml"));
bean.setTypeAliasesPackage("com.luoyu.staticmultipledatasources.entity");
return bean.getObject();
}
/**
* 创建事务管理器
* @param db2DataSource
* @return
*/
@Bean("db2TransactionManger")
@Primary
public DataSourceTransactionManager db2TransactionManger(@Qualifier("db2DataSource") DataSource db2DataSource){
return new DataSourceTransactionManager(db2DataSource);
}
/**
* 创建SqlSessionTemplate
* @param db2SqlSessionFactory
* @return
*/
@Bean(name = "db2SqlSessionTemplate")
@Primary
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory db2SqlSessionFactory){
return new SqlSessionTemplate(db2SqlSessionFactory);
}
}
MybatisPlusConfig
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @Description: MybatisPlus配置类
* @Author: jinhaoxun
* @Date: 2020/2/13 上午11:34
* @Version: 1.0.0
*/
@EnableTransactionManagement
@Configuration
@Order(-1)
public class MybatisPlusConfig {
/**
* 使application.properties配置生效,如果不主动配置,由于@Order配置顺序不同,将导致配置不能及时生效,多数据源配置驼峰法生效
* @return 数据源
*/
@Bean("mybatisplusConfiguration")
@ConfigurationProperties(prefix = "mybatis-plus.configuration")
@Scope("prototype")
public org.apache.ibatis.session.Configuration globalConfiguration() {
return new org.apache.ibatis.session.Configuration();
}
/**
* mybatis-plus SQL执行效率插件【生产环境可以关闭】,设置 dev test 环境开启
*/
// @Bean
// @Profile({"dev", "qa"})
// public PerformanceInterceptor performanceInterceptor() {
// PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
// performanceInterceptor.setMaxTime(1000);
// performanceInterceptor.setFormat(true);
// return performanceInterceptor;
// }
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}
第六步,创建Task,User,如下
Task
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* <p>
*
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class Task implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 邮箱
*/
private String email;
}
User
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 邮箱
*/
private String email;
}
第七步,分别创建UserMapper,UserMapper.xml,TaskMapper,TaskMapper.xml,放在不同的包下,如下
UserMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.luoyu.staticmultipledatasources.entity.User;
import org.apache.ibatis.annotations.Param;
/**
* <p>
* Mapper 接口
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
public interface UserMapper extends BaseMapper<User> {
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @Date: 2020/2/13 下午12:06
* @Throws:
*/
User selectById(Integer id);
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @param name 姓名
* @Date: 2020/2/13 下午12:06
* @Return: int
* @Throws:
*/
int updateName(@Param("id") int id, @Param("name") String name);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.luoyu.staticmultipledatasources.mapper.db1.UserMapper">
<select id="selectById" parameterType="java.lang.Integer" resultType="com.luoyu.staticmultipledatasources.entity.User">
select * from user where id = #{id};
</select>
<update id="updateName">
update user set name = #{name} where id = #{id};
</update>
</mapper>
TaskMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.luoyu.staticmultipledatasources.entity.Task;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* <p>
* Mapper 接口
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
@Mapper
public interface TaskMapper extends BaseMapper<Task> {
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @Date: 2020/2/13 下午12:06
* @Throws:
*/
Task selectById(Integer id);
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @param name 姓名
* @Date: 2020/2/13 下午12:06
* @Return: int
* @Throws:
*/
int updateName(@Param("id") int id, @Param("name") String name);
}
TaskMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.luoyu.staticmultipledatasources.mapper.db2.TaskMapper">
<select id="selectById" parameterType="java.lang.Integer" resultType="com.luoyu.staticmultipledatasources.entity.Task">
select * from task where id = #{id};
</select>
<update id="updateName">
update task set name = #{name} where id = #{id};
</update>
</mapper>
路径如下:
第八步,创建IUserService,TaskServiceImpl,ITaskService,TaskServiceImpl,如下
IUserService
import com.baomidou.mybatisplus.extension.service.IService;
import com.luoyu.staticmultipledatasources.entity.User;
/**
* <p>
* 服务类
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
public interface IUserService extends IService<User> {
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @Date: 2020/2/13 下午12:06
* @Throws:
*/
User selectById(Integer id);
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @param name 姓名
* @Date: 2020/2/13 下午12:06
* @Return: int
* @Throws:
*/
int updateName(int id, String name);
}
UserServiceImpl
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoyu.staticmultipledatasources.entity.User;
import com.luoyu.staticmultipledatasources.mapper.db1.UserMapper;
import com.luoyu.staticmultipledatasources.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* <p>
* 服务实现类
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Resource
private UserMapper userMapper;
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @Date: 2020/2/13 下午12:06
* @Throws:
*/
@Override
public User selectById(Integer id) {
return userMapper.selectById(id);
}
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @param name 姓名
* @Date: 2020/2/13 下午12:06
* @Return: int
* @Throws:
*/
@Transactional(value = "db1TransactionManger")
@Override
public int updateName(int id, String name) {
int count = userMapper.updateName(id, name);
int i = 1/0;
return count;
}
}
ITaskService
import com.baomidou.mybatisplus.extension.service.IService;
import com.luoyu.staticmultipledatasources.entity.Task;
/**
* <p>
* 服务类
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
public interface ITaskService extends IService<Task> {
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @Date: 2020/2/13 下午12:06
* @Throws:
*/
Task selectById(Integer id);
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @param name 姓名
* @Date: 2020/2/13 下午12:06
* @Return: int
* @Throws:
*/
int updateName(int id, String name);
}
TaskServiceImpl
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoyu.staticmultipledatasources.entity.Task;
import com.luoyu.staticmultipledatasources.mapper.db2.TaskMapper;
import com.luoyu.staticmultipledatasources.service.ITaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
/**
* <p>
* 服务实现类
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {
@Resource
private TaskMapper taskMapper;
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @Date: 2020/2/13 下午12:06
* @Throws:
*/
@Override
public Task selectById(Integer id) {
return taskMapper.selectById(id);
}
/**
* @Author: jinhaoxun
* @Description:
* @param id id
* @param name 姓名
* @Date: 2020/2/13 下午12:06
* @Return: int
* @Throws:
*/
@Transactional(value = "db2TransactionManger")
@Override
public int updateName(int id, String name) {
int count = taskMapper.updateName(id, name);
int i = 1/0;
return count;
}
}
第九步,创建TaskController,UserController,如下
TaskController
import com.luoyu.staticmultipledatasources.entity.Task;
import com.luoyu.staticmultipledatasources.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 前端控制器
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
@RestController
public class TaskController {
@Autowired
private ITaskService iTaskService;
/**
* 查询db2
*/
@GetMapping(value = "/task")
public Task getTaskById(@RequestParam("id") Integer id) {
return iTaskService.selectById(id);
}
/**
* 测试事物
*/
@PutMapping(value = "/task")
public int updateName(@RequestParam("id") Integer id, @RequestParam("name") String name) {
return iTaskService.updateName(id, name);
}
}
UserController
import com.luoyu.staticmultipledatasources.entity.User;
import com.luoyu.staticmultipledatasources.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 前端控制器
* </p>
*
* @author jinhaoxun
* @since 2020-02-13
*/
@RestController
public class UserController {
@Autowired
private IUserService iUserService;
/**
* 查询db1
*/
@GetMapping(value = "/user")
public User getUserById(@RequestParam("id") Integer id) {
return iUserService.selectById(id);
}
/**
* 测试事物
*/
@PutMapping(value = "/user")
public int updateName(@RequestParam("id") Integer id, @RequestParam("name") String name) {
return iUserService.updateName(id, name);
}
}
第十步,启动项目,使用postman进行测试,如下
注:此工程包含多个module,本文所用代码均在staticmultipledatasources-demo模块下