MyBatis-Plus多數(shù)據(jù)源的示例代碼
下面是一個完整的 MyBatis-Plus 多數(shù)據(jù)源支持的示例代碼,包括依賴配置、數(shù)據(jù)源配置、Mapper 和 Service 的定義。
1. 添加依賴
在 pom.xml 中添加 MyBatis-Plus 和多數(shù)據(jù)源相關(guān)的依賴:
<dependencies>
<!-- MyBatis-Plus 依賴 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<!-- HikariCP 連接池依賴 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<!-- MySQL 驅(qū)動依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
2. 配置數(shù)據(jù)源
在 application.yml 中配置多個數(shù)據(jù)源:
spring:
datasource:
primary:
jdbc-url: jdbc:mysql://localhost:3306/primary_db
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
secondary:
jdbc-url: jdbc:mysql://localhost:3306/secondary_db
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
3. 數(shù)據(jù)源配置類
創(chuàng)建數(shù)據(jù)源配置類 DataSourceConfig.java:
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
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.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return new HikariDataSource();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return new HikariDataSource();
}
@Primary
@Bean(name = "primaryTransactionManager")
public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
return new DataSourceTransactionManager(primaryDataSource);
}
@Bean(name = "secondaryTransactionManager")
public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
return new DataSourceTransactionManager(secondaryDataSource);
}
@Primary
@Bean(name = "primarySqlSessionFactory")
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception {
MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
sessionFactoryBean.setDataSource(primaryDataSource);
return sessionFactoryBean.getObject();
}
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) throws Exception {
MybatisSqlSessionFactoryBean sessionFactoryBean = new MybatisSqlSessionFactoryBean();
sessionFactoryBean.setDataSource(secondaryDataSource);
return sessionFactoryBean.getObject();
}
}
4. 指定 Mapper 掃描路徑
在不同的包路徑下創(chuàng)建 Mapper 接口,并使用 @MapperScan 注解指定使用的 SqlSessionFactory。
創(chuàng)建 PrimaryDataSourceConfig.java:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan(basePackages = "com.example.primary.mapper", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSourceConfig {
}
創(chuàng)建 SecondaryDataSourceConfig.java:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan(basePackages = "com.example.secondary.mapper", sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class SecondaryDataSourceConfig {
}
5. 創(chuàng)建實體類和 Mapper 接口
創(chuàng)建兩個實體類 PrimaryEntity 和 SecondaryEntity:
// PrimaryEntity.java
package com.example.primary.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("primary_table")
public class PrimaryEntity {
@TableId
private Long id;
private String name;
// Getters and Setters
}
// SecondaryEntity.java
package com.example.secondary.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("secondary_table")
public class SecondaryEntity {
@TableId
private Long id;
private String name;
// Getters and Setters
}
創(chuàng)建 Mapper 接口:
// PrimaryMapper.java
package com.example.primary.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.primary.entity.PrimaryEntity;
public interface PrimaryMapper extends BaseMapper<PrimaryEntity> {
}
// SecondaryMapper.java
package com.example.secondary.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.secondary.entity.SecondaryEntity;
public interface SecondaryMapper extends BaseMapper<SecondaryEntity> {
}
6. 創(chuàng)建 Service 類
創(chuàng)建 Service 類,分別處理不同數(shù)據(jù)源的業(yè)務邏輯:
// PrimaryService.java
package com.example.primary.service;
import com.example.primary.entity.PrimaryEntity;
import com.example.primary.mapper.PrimaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PrimaryService {
@Autowired
private PrimaryMapper primaryMapper;
public List<PrimaryEntity> getAll() {
return primaryMapper.selectList(null);
}
}
// SecondaryService.java
package com.example.secondary.service;
import com.example.secondary.entity.SecondaryEntity;
import com.example.secondary.mapper.SecondaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SecondaryService {
@Autowired
private SecondaryMapper secondaryMapper;
public List<SecondaryEntity> getAll() {
return secondaryMapper.selectList(null);
}
}
7. 創(chuàng)建 Controller 類
最后,為每個數(shù)據(jù)源創(chuàng)建對應的控制器以提供 RESTful 接口:
// PrimaryController.java
package com.example.primary.controller;
import com.example.primary.entity.PrimaryEntity;
import com.example.primary.service.PrimaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/primary")
public class PrimaryController {
@Autowired
private PrimaryService primaryService;
@GetMapping("/all")
public List<PrimaryEntity> getAll() {
return primaryService.getAll();
}
}
// SecondaryController.java
package com.example.secondary.controller;
import com.example.secondary.entity.SecondaryEntity;
import com.example.secondary.service.SecondaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/secondary")
public class SecondaryController {
@Autowired
private SecondaryService secondaryService;
@GetMapping("/all")
public List<SecondaryEntity> getAll() {
return secondaryService.getAll();
}
}
通過上述步驟,就可以在 Spring Boot 項目中使用 MyBatis-Plus 實現(xiàn)多數(shù)據(jù)源支持了。這種配置方式可以方便地管理和使用不同的數(shù)據(jù)源,適應不同的業(yè)務需求。
到此這篇關(guān)于MyBatis-Plus多數(shù)據(jù)源的示例代碼的文章就介紹到這了,更多相關(guān)MyBatis-Plus多數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析Java中StringBuffer和StringBuilder的使用
當對字符串進行修改的時候,需要使用 StringBuffer 和 StringBuilder 類。本文就來和大家簡單聊聊這二者的使用與區(qū)別吧,希望對大家有所幫助2023-04-04
SpringBoot3 整合Docker-Compose的實現(xiàn)步驟
本文主要介紹了SpringBoot3 整合Docker-Compose的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-08-08
Java實現(xiàn)json數(shù)據(jù)處理的常用腳本分享
這篇文章主要為大家詳細介紹了Java實現(xiàn)json數(shù)據(jù)處理的常用腳本,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的小伙伴可以學習一下2023-03-03

