概述
在开发中,常常会遇到需要连接多个数据库的情况。使用 Mybatis 和 Mybatis-plus 框架可以很方便地配置多数据源,以连接不同的数据库。本文将介绍如何配置多数据源,并提供示例代码。
步骤
以下将详细讲解配置多数据源的步骤:
1. 添加依赖
在项目的 pom.xml 文件中添加 Mybatis 和 Mybatis-plus 的依赖。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.2</version>
</dependency>
2. 配置数据源
在项目的配置文件中配置数据源。在这里,我们需要配置多个数据源,以连接不同的数据库。可以使用 Spring Boot 自带的 DataSourceProperties 类来配置数据源。
application.properties 配置文件示例
## 数据源1配置
spring.datasource.first.url=jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC
spring.datasource.first.username=root
spring.datasource.first.password=root
## 数据源2配置
spring.datasource.second.url=jdbc:mysql://localhost:3306/db2?useSSL=false&serverTimezone=UTC
spring.datasource.second.username=root
spring.datasource.second.password=root
application.yaml 配置文件示例
## 数据源1配置
spring:
datasource:
first:
url: jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC
username: root
password: root
## 数据源2配置
second:
url: jdbc:mysql://localhost:3306/db2?useSSL=false&serverTimezone=UTC
username: root
password: root
3. 配置数据源连接池
在配置文件中配置数据源连接池。可以使用 Spring Boot 自带的 DataSourceBuilder 类来创建数据源。
DataSourceConfig.java 配置文件示例
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.first")
public DataSource firstDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
}
4. 配置 Mybatis 和 Mybatis-plus
在配置文件中配置 Mybatis 和 Mybatis-plus。使用 Spring Boot 自带的 MapperScan 注解来扫描 Mapper 接口。
MybatisConfig.java 配置文件示例
@Configuration
@MapperScan(basePackages = {"com.example.mapper.first"}, sqlSessionTemplateRef = "firstSqlSessionTemplate")
public class MybatisConfig {
@Autowired
@Qualifier("firstDataSource")
private DataSource firstDataSource;
@Bean
public SqlSessionFactory firstSqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(firstDataSource);
return bean.getObject();
}
@Bean
public SqlSessionTemplate firstSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(firstSqlSessionFactory());
}
}
@Configuration
@MapperScan(basePackages = {"com.example.mapper.second"}, sqlSessionTemplateRef = "secondSqlSessionTemplate")
public class MybatisPlusConfig {
@Autowired
@Qualifier("secondDataSource")
private DataSource secondDataSource;
@Bean
public SqlSessionFactory secondSqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(secondDataSource);
return bean.getObject();
}
@Bean
public SqlSessionTemplate secondSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(secondSqlSessionFactory());
}
}
5. 配置事务
在配置文件中配置事务。在这里,我们使用的是 Spring Boot 自带的注解 @EnableTransactionManagement 来启用事务功能。
Application.java 配置文件示例
@SpringBootApplication
@EnableTransactionManagement
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6. 示例代码
下面是一个示例的 Mapper 接口和 Service 类。
// 数据源1的 Mapper 接口
public interface FirstMapper {
// ...
}
// 数据源2的 Mapper 接口
public interface SecondMapper {
// ...
}
// 数据源1的 Service 类
@Service
@Transactional(transactionManager = "firstTransactionManager")
public class FirstService {
@Autowired
private FirstMapper firstMapper;
// ...
}
// 数据源2的 Service 类
@Service
@Transactional(transactionManager = "secondTransactionManager")
public class SecondService {
@Autowired
private SecondMapper secondMapper;
// ...
}
结论
通过以上步骤,我们可以很容易地配置多数据源,以连接多个数据库。利用 Mybatis 和 Mybatis-plus 框架,我们可以轻松地进行数据库操作。希望本文对你的开发工作有所帮助。
参考资料
- Spring Boot官方文档
- Mybatis官方文档
- Mybatis-plus官方文档
- Spring Boot多数据源配置教程
本文来自极简博客,作者:星河之舟,转载请注明原文链接:Mybatis Mybatis-plus配置多数据源,连接多数据库
微信扫一扫,打赏作者吧~