MyBatis-Plus數(shù)據(jù)庫配置與數(shù)據(jù)源整合方案
在使用 MyBatis-Plus(MP)進(jìn)行開發(fā)時(shí),正確配置和管理數(shù)據(jù)源是非常重要的一步。MyBatis-Plus 不僅支持 單數(shù)據(jù)源 的配置,還支持 多數(shù)據(jù)源 的配置和切換,適用于分布式系統(tǒng)中不同的數(shù)據(jù)庫管理和訪問場(chǎng)景。
本文將介紹 MyBatis-Plus 數(shù)據(jù)庫配置與數(shù)據(jù)源整合 的常見方法,包括單數(shù)據(jù)源與多數(shù)據(jù)源的配置、配置文件與 Spring 配置類的使用、以及如何進(jìn)行數(shù)據(jù)源切換和管理。
1. 單數(shù)據(jù)源配置
1.1 單數(shù)據(jù)源配置概述
在大多數(shù)應(yīng)用中,通常只需要一個(gè)數(shù)據(jù)源進(jìn)行數(shù)據(jù)庫的操作。MyBatis-Plus 支持通過 Spring Boot 自動(dòng)配置來簡(jiǎn)化單數(shù)據(jù)源的配置。
1.2 數(shù)據(jù)源配置步驟
- 添加數(shù)據(jù)庫依賴
首先,在 pom.xml
文件中添加 MyBatis-Plus 和數(shù)據(jù)庫相關(guān)的依賴。
例如,使用 H2 數(shù)據(jù)庫作為示例:
<dependencies> <!-- MyBatis-Plus 核心依賴 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-core</artifactId> <version>3.4.3</version> </dependency> <!-- Spring Boot MyBatis starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- 數(shù)據(jù)庫連接池(H2 示例) --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies>
- 配置數(shù)據(jù)庫連接
在 application.properties
或 application.yml
配置文件中添加數(shù)據(jù)源配置。
application.properties
配置示例:
# 數(shù)據(jù)庫連接配置 spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=password spring.datasource.hibernate.ddl-auto=update # MyBatis-Plus 配置 mybatis-plus.mapper-locations=classpath:/mappers/**/*.xml mybatis-plus.type-aliases-package=com.example.domain
在該配置中:
spring.datasource
用于配置數(shù)據(jù)庫的連接信息。mybatis-plus
配置了 MyBatis-Plus 的基礎(chǔ)配置。
- 數(shù)據(jù)源自動(dòng)配置
Spring Boot 會(huì)自動(dòng)加載數(shù)據(jù)源和 MyBatis-Plus 配置,無需手動(dòng)創(chuàng)建 DataSource
和 SqlSessionFactory
等對(duì)象。你只需要?jiǎng)?chuàng)建 Mapper
接口和實(shí)體類即可。
1.3 Spring Boot 自動(dòng)配置
MyBatis-Plus 提供了開箱即用的自動(dòng)配置。只要在 Spring Boot 項(xiàng)目中配置好數(shù)據(jù)源,MyBatis-Plus 會(huì)自動(dòng)進(jìn)行配置,無需額外的手動(dòng)配置。
2. 多數(shù)據(jù)源配置
2.1 多數(shù)據(jù)源配置概述
在一些復(fù)雜的應(yīng)用中,可能需要連接多個(gè)數(shù)據(jù)庫源。比如,可能需要一個(gè)主數(shù)據(jù)庫和多個(gè)副本數(shù)據(jù)庫,或者在分布式系統(tǒng)中連接不同的數(shù)據(jù)庫。
MyBatis-Plus 支持多數(shù)據(jù)源的配置和切換,具體可以通過配置不同的 DataSource
來實(shí)現(xiàn)。
2.2 多數(shù)據(jù)源配置步驟
- 添加數(shù)據(jù)庫依賴
首先,確保你已在 pom.xml
文件中添加所需數(shù)據(jù)庫的依賴。
- 配置多個(gè)數(shù)據(jù)源
在 application.yml
中定義多個(gè)數(shù)據(jù)源配置。
例如,配置一個(gè) 主數(shù)據(jù)源 和一個(gè) 副數(shù)據(jù)源:
spring: datasource: # 主數(shù)據(jù)源配置 primary: url: jdbc:mysql://localhost:3306/primarydb driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root # 副數(shù)據(jù)源配置 secondary: url: jdbc:mysql://localhost:3306/secondarydb driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root
2.3 配置數(shù)據(jù)源和事務(wù)管理器
你需要在配置類中為每個(gè)數(shù)據(jù)源配置 DataSource
、SqlSessionFactory
和 TransactionManager
。
@Configuration @EnableTransactionManagement @MapperScan(basePackages = "com.example.mapper") public class DataSourceConfig { @Bean(name = "primaryDataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource dataSourcePrimary() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource dataSourceSecondary() { return DataSourceBuilder.create().build(); } // 配置主數(shù)據(jù)源的事務(wù)管理器 @Bean(name = "transactionManagerPrimary") @Primary public DataSourceTransactionManager transactionManagerPrimary(@Qualifier("primaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } // 配置副數(shù)據(jù)源的事務(wù)管理器 @Bean(name = "transactionManagerSecondary") public DataSourceTransactionManager transactionManagerSecondary(@Qualifier("secondaryDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } // 配置主數(shù)據(jù)源的 SqlSessionFactory @Bean(name = "sqlSessionFactoryPrimary") @Primary public SqlSessionFactory sqlSessionFactoryPrimary(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); } // 配置副數(shù)據(jù)源的 SqlSessionFactory @Bean(name = "sqlSessionFactorySecondary") public SqlSessionFactory sqlSessionFactorySecondary(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); } }
2.4 動(dòng)態(tài)切換數(shù)據(jù)源
在應(yīng)用中使用動(dòng)態(tài)數(shù)據(jù)源切換,Spring 提供了 @Primary
和 @Qualifier
注解來指定哪個(gè)數(shù)據(jù)源為主數(shù)據(jù)源,哪一個(gè)為副數(shù)據(jù)源。在進(jìn)行操作時(shí),通過動(dòng)態(tài)切換數(shù)據(jù)源來控制操作目標(biāo)。
public class DynamicDataSourceContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); public static void setDataSource(String dataSource) { contextHolder.set(dataSource); } public static String getDataSource() { return contextHolder.get(); } public static void clearDataSource() { contextHolder.remove(); } }
切換數(shù)據(jù)源時(shí):
DynamicDataSourceContextHolder.setDataSource("primary"); // 切換到主數(shù)據(jù)源
3. 自動(dòng)配置與手動(dòng)配置的區(qū)別
3.1 自動(dòng)配置
Spring Boot 提供了自動(dòng)配置功能,MyBatis-Plus 默認(rèn)會(huì)通過配置文件自動(dòng)配置數(shù)據(jù)源、SqlSessionFactory
和事務(wù)管理器等組件。如果項(xiàng)目只涉及單一數(shù)據(jù)源,Spring Boot 會(huì)根據(jù) application.properties
或 application.yml
中的配置自動(dòng)進(jìn)行數(shù)據(jù)源配置。
自動(dòng)配置的好處
- 減少配置:只需在配置文件中配置數(shù)據(jù)源,Spring Boot 會(huì)自動(dòng)完成其余的配置工作。
- 簡(jiǎn)化管理:對(duì)于單數(shù)據(jù)源應(yīng)用,使用自動(dòng)配置更為簡(jiǎn)單,開發(fā)者只需關(guān)注業(yè)務(wù)代碼。
自動(dòng)配置示例
spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=root
Spring Boot 會(huì)根據(jù)以上配置自動(dòng)初始化 DataSource
、SqlSessionFactory
、TransactionManager
等組件。
3.2 手動(dòng)配置
對(duì)于復(fù)雜的項(xiàng)目,尤其是涉及多數(shù)據(jù)源的情況,開發(fā)者通常需要手動(dòng)配置數(shù)據(jù)源、SqlSessionFactory
、事務(wù)管理器等。
這種方式提供了更大的靈活性,可以在不同的數(shù)據(jù)源之間進(jìn)行細(xì)粒度的控制和切換。
手動(dòng)配置的好處
- 靈活性:可以手動(dòng)控制每個(gè)數(shù)據(jù)源的配置,例如動(dòng)態(tài)數(shù)據(jù)源切換、多個(gè)數(shù)據(jù)庫連接等。
- 擴(kuò)展性:適用于需要自定義配置、插件等場(chǎng)景。
手動(dòng)配置示例
@Configuration public class MyBatisConfig { @Bean public DataSource dataSource() { return DataSourceBuilder.create().url("jdbc:mysql://localhost:3306/mydb") .username("root") .password("root") .build(); } @Bean public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); return factoryBean.getObject(); } }
4. 總結(jié)
- 單數(shù)據(jù)源配置:對(duì)于大多數(shù)應(yīng)用,MyBatis-Plus 配置單數(shù)據(jù)源時(shí),可以通過 Spring Boot 的自動(dòng)配置功能快速完成。
- 多數(shù)據(jù)源配置:在復(fù)雜的應(yīng)用場(chǎng)景中,可以手動(dòng)配置多個(gè)數(shù)據(jù)源,通過動(dòng)態(tài)切換實(shí)現(xiàn)數(shù)據(jù)庫的負(fù)載均衡和高可用性。
- 自動(dòng)配置與手動(dòng)配置的區(qū)別:自動(dòng)配置適用于單數(shù)據(jù)源的場(chǎng)景,簡(jiǎn)化了配置過程,而手動(dòng)配置則提供了更大的靈活性,適用于多數(shù)據(jù)源的管理和動(dòng)態(tài)切換。
通過合適的配置和管理,MyBatis-Plus 可以幫助開發(fā)者在 Spring Boot 應(yīng)用中高效管理數(shù)據(jù)源、簡(jiǎn)化數(shù)據(jù)庫操作,并根據(jù)需求實(shí)現(xiàn)更加復(fù)雜的數(shù)據(jù)庫架構(gòu)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot+Poi導(dǎo)入Excel表格實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Springboot+Poi導(dǎo)入Excel表格實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09SpringCloud OpenFeign Post請(qǐng)求400錯(cuò)誤解決方案
這篇文章主要介紹了SpringCloud OpenFeign Post請(qǐng)求400錯(cuò)誤解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Spring?@Conditional注解示例詳細(xì)講解
@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊(cè)bean,這篇文章主要介紹了Spring?@Conditional注解示例詳細(xì)講解,需要的朋友可以參考下2022-11-11Java執(zhí)行Linux命令簡(jiǎn)單代碼舉例
這篇文章主要給大家介紹了關(guān)于Java執(zhí)行Linux命令的相關(guān)資料,在開發(fā)的過程中要善于利用JAVA面向?qū)ο缶幊痰膬?yōu)勢(shì),與Linux/Unix命令或Shell腳本的優(yōu)勢(shì),并將二者相結(jié)合,需要的朋友可以參考下2023-12-12Spring?Security+JWT簡(jiǎn)述(附源碼)
SpringSecurity是一個(gè)強(qiáng)大的可高度定制的認(rèn)證和授權(quán)框架,對(duì)于Spring應(yīng)用來說它是一套Web安全標(biāo)準(zhǔn),下面這篇文章主要給大家介紹了關(guān)于Spring?Security+JWT簡(jiǎn)述的相關(guān)資料,需要的朋友可以參考下2023-04-04Java實(shí)現(xiàn)文件上傳的兩種方法(uploadify和Spring)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)文件上傳的兩種方法,uploadify和Spring實(shí)現(xiàn)文件上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Java文件下載ZIP報(bào)錯(cuò):Out of Memory的問題排查
本文主要介紹了Java項(xiàng)目中下載大文件(超過2G的ZIP文件)時(shí)出現(xiàn)內(nèi)存溢出(OutOfMemory:JavaHeapSpace)的問題,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01java中建立0-10m的消息(字符串)實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava中建立0-10m的消息(字符串)實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Java開發(fā)實(shí)現(xiàn)的Socket雙向通信功能示例
這篇文章主要介紹了Java開發(fā)實(shí)現(xiàn)的Socket雙向通信功能,結(jié)合實(shí)例形式分析了java基于socket實(shí)現(xiàn)的服務(wù)器端與客戶端雙向通信相關(guān)操作技巧,需要的朋友可以參考下2018-01-01