springboot中如何利用mybatis-plus配置多数据源
Spring Boot 是一种用于构建基于 Spring 的应用程序的快速,方便和固定的方法。常用的 ORM 框架之一是 MyBatis-Plus。MyBatis-Plus 为我们提供了方便的多数据源配置,可以在同一个项目里面访问 MySql,Oracle 和 SqlServer 等不同的数据库。在本文中,我们将讨论如何利用 MyBatis-Plus 在 Spring Boot 中配置多数据源。
步骤1:添加依赖
首先,在 pom.xml 文件中增加如下的依赖项。
<!--MyBatis-Plus多数据源配置-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.3</version>
</dependency>
步骤2:添加配置
在定义数据源之前,我们需要在 application.yml 文件中先添加如下配置:
spring:
application:
name: springboot-mutil-datasource
datasource:
#默认数据源
url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: your_username
password: your_password
driverClassName: com.mysql.jdbc.Driver
#数据源1
datasource1:
url: jdbc:mysql://localhost:3306/test1?characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: your_username
password: your_password
driverClassName: com.mysql.jdbc.Driver
#数据源2
datasource2:
url: jdbc:mysql://localhost:3306/test2?characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: your_username
password: your_password
driverClassName: com.mysql.jdbc.Driver
在上述配置中,我们定义了 Spring Boot 应用程序中使用的默认数据源,以及多个其他数据库。现在我们需要在代码中动态添加这些数据源。
步骤3:添加数据源
该项目中我们定义了两个数据源,因此我们需要为每个数据源实例化一个数据源。
@Configuration
public class DataSourceConfig {
/***
* 默认数据源,必须配置,配置默认的可注解 @Primary
*/
@Primary
@Bean(name = "defaultDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource defaultDataSource() {
return DataSourceBuilder.create().build();
}
/***
* 数据源1
*/
@Bean(name = "dataSource1")
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
/***
* 数据源2
*/
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
/***
* 这里我们管理多数据源的管理类 DynamicRoutingDataSource,可以把它理解成多个数据源交给它管理,
* 最终返回给我们一个数据源,数据源的切换,由我们动态的选择数据源的key来切换对应的数据源
*/
@Bean
@Primary
public DynamicDataSource dataSource(@Qualifier("defaultDataSource") DataSource defaultDataSource,
@Qualifier("dataSource1") DataSource dataSource1,
@Qualifier("dataSource2") DataSource dataSource2) {
Map<Object, Object> targetDataSources = new HashMap<>(3);
targetDataSources.put(DataSourceType.DATA_SOURCE_DEFAULT.getType(), defaultDataSource);
targetDataSources.put(DataSourceType.DATA_SOURCE_ONE.getType(), dataSource1);
targetDataSources.put(DataSourceType.DATA_SOURCE_TWO.getType(), dataSource2);
return new DynamicDataSource(defaultDataSource, targetDataSources);
}
}
步骤4:配置 mybatis-plus
我们已经成功地配置了多个数据源,现在我们需要在 MyBatis-Plus 中使用它们。要使用多数据源,我们需要注入一个 Mapper 对象,同时指定数据源。
@Service
public class UserServiceImpl implements IUserService {
/***
* 用户mapper
*/
@Autowired(required = false)
@Qualifier("defaultSqlSessionTemplate")
private UserMapper userMapper;
/***
* 操作userInfo表
*/
@Autowired(required = false)
@Qualifier("oneSqlSessionTemplate")
private UserInfoMapper userInfoMapper;
/***
* 操作user_type表
*/
@Autowired(required = false)
@Qualifier("twoSqlSessionTemplate")
private UserTypeMapper userTypeMapper;
/***
* 根据用户ID获取用户信息
*/
@Override
public User getUserById(Integer userId) {
return userMapper.selectById(userId);
}
/***
* 获取所有用户信息列表
*/
@Override
public List<User> getUserList() {
return userMapper.selectList(null);
}
/***
* 添加用户
*/
@Override
public int addUser(User user) {
return userMapper.insert(user);
}
/***
* 修改用户信息
*/
@Override
public int updateUser(User user) {
return userMapper.updateById(user);
}
/***
* 根据用户ID删除用户
*/
@Override
public int deleteUser(Integer userId) {
return userMapper.deleteById(userId);
}
@Override
public List<UserInfo> getUserInfoList() {
return userInfoMapper.selectList(null);
}
@Override
public List<UserType> getUserTypeList() {
return userTypeMapper.selectList(null);
}
}
在 UserServiceImpl 类中,我们使用了 @ Qualifier 注释来说明我们想要使用哪个数据库。在我们这个例子中,我们在 UserMapper 中使用了默认数据库,在 UserInfoMapper 类中使用了数据源1,在 UserTypeMapper 中使用了 数据源2。
现在我们可以使用 MyBatis-Plus 连接多个数据库,同时访问不同的表。
总结:
利用 MyBatis-Plus 在 Spring Boot 中配合配置多数据源非常简单,只需要依次进行以下步骤:
1. 在 pom.xml 中添加依赖。
2. 配置 application.properties 或 application.yml。
3. 添加数据源。
4. 在 MyBatis-Plus 中使用多个数据源。
此外,在以上步骤中,在需要使用的地方注入Mapper时,需要使用@Qualifier来指定具体使用的数据源。
