|
springboot使用MybatisPlus 如何同时连接2个MYSQL数据库
在 Spring Boot 中使用 MyBatis-Plus 同时连接两个 MySQL 数据库,可以通过配置多个 `DataSource` 和 `SqlSessionFactory` 来实现。以下是具体的步骤:
### 步骤 1: 添加依赖
确保在 `pom.xml` 中添加 MyBatis-Plus 和 MySQL 的依赖:
- xml
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.27</version> <!-- 使用最新版本 -->
- </dependency>
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.3</version> <!-- 使用最新版本 -->
- </dependency>
复制代码
### 步骤 2: 配置 `application.properties` 或 `application.yml`
在配置文件中定义两个数据源的属性。例如,使用 `application.properties` :
- properties
- # 数据源 1
- spring.datasource.primary.url=jdbc:mysql://localhost:3306/db1
- spring.datasource.primary.username=root
- spring.datasource.primary.password=yourpassword
- spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
- # 数据源 2
- spring.datasource.secondary.url=jdbc:mysql://localhost:3306/db2
- spring.datasource.secondary.username=root
- spring.datasource.secondary.password=yourpassword
- spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
复制代码
### 步骤 3: 创建数据源配置类
为每个数据源创建配置类。
#### 主数据源配置
- import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
- import com.zaxxer.hikari.HikariDataSource;
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Primary;
- import org.springframework.jdbc.datasource.DataSourceTransactionManager;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.mybatis.SqlSessionFactoryBean;
- import org.springframework.transaction.PlatformTransactionManager;
- import javax.sql.DataSource;
- @Configuration
- public class PrimaryDataSourceConfig {
- @Bean(name = "primaryDataSource")
- @ConfigurationProperties("spring.datasource.primary")
- @Primary
- public DataSource primaryDataSource() {
- return new HikariDataSource();
- }
- @Bean(name = "primarySqlSessionFactory")
- @Primary
- public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
- SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
- factoryBean.setDataSource(dataSource);
- return factoryBean.getObject();
- }
- @Bean(name = "primaryTransactionManager")
- @Primary
- public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
- return new DataSourceTransactionManager(dataSource);
- }
- }
复制代码
#### 次数据源配置
- import com.zaxxer.hikari.HikariDataSource;
- import org.mybatis.spring.SqlSessionFactoryBean;
- 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.jdbc.datasource.DataSourceTransactionManager;
- import org.springframework.orm.mybatis.SqlSessionFactoryBean;
- import org.springframework.transaction.PlatformTransactionManager;
- import javax.sql.DataSource;
- @Configuration
- public class SecondaryDataSourceConfig {
- @Bean(name = "secondaryDataSource")
- @ConfigurationProperties("spring.datasource.secondary")
- public DataSource secondaryDataSource() {
- return new HikariDataSource();
- }
- @Bean(name = "secondarySqlSessionFactory")
- public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
- SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
- factoryBean.setDataSource(dataSource);
- return factoryBean.getObject();
- }
- @Bean(name = "secondaryTransactionManager")
- public PlatformTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
- return new DataSourceTransactionManager(dataSource);
- }
- }
复制代码
### 步骤 4: 创建实体和 Mapper
为每个数据库创建相应的实体类和 Mapper 接口,确保它们位于正确的包中。
### 步骤 5: 使用 Mapper
在服务中,可以根据需要注入主数据源和次数据源的 Mapper:
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- @Service
- public class MyService {
- @Autowired
- private PrimaryMapper primaryMapper;
- @Autowired
- private SecondaryMapper secondaryMapper;
- public void saveData() {
- // 使用 primaryMapper 和 secondaryMapper 进行数据操作
- }
- }
复制代码
### 总结
通过以上步骤,你可以在 Spring Boot 应用中同时连接两个 MySQL 数据库,并使用 MyBatis-Plus 进行数据操作。
|
|