Spring Boot整合MyBatis-Flex全過(guò)程
說(shuō)明:MyBatis-Flex(官網(wǎng)地址:https://mybatis-flex.com/),是一款數(shù)據(jù)訪問(wèn)層框架,可實(shí)現(xiàn)項(xiàng)目中對(duì)數(shù)據(jù)庫(kù)的訪問(wèn),類比MyBatis-Plus。
本文介紹,在Spring Boot項(xiàng)目整合MyBatis-Flex。
創(chuàng)建項(xiàng)目
首先,創(chuàng)建一個(gè)Spring boot項(xiàng)目,pom.xml文件內(nèi)容如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/>
</parent>
<groupId>com.hezy</groupId>
<artifactId>mybatis-flex-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Archetype - mybatis-flex-demo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
</project>
創(chuàng)建一個(gè)實(shí)體類對(duì)象,User,如下:
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String password;
}
創(chuàng)建一個(gè)接口,如下:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
application.yml,配置文件如下:
server:
port: 8080
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/demo
username: postgres
password: 123456
啟動(dòng)項(xiàng)目,測(cè)試,沒(méi)得問(wèn)題

整合MyBatis-Flex
引入下面這兩個(gè)依賴;
<dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-spring-boot-starter</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-processor</artifactId> <version>1.9.3</version> <scope>provided</scope> </dependency>
在實(shí)體類上關(guān)聯(lián)表,包括實(shí)體類對(duì)應(yīng)的表名,以及對(duì)應(yīng)的字段,像主鍵、別名(如果有)等;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import lombok.Data;
import java.io.Serializable;
@Data
@Table("tb_user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
private String username;
private String password;
}
創(chuàng)建數(shù)據(jù)訪問(wèn)層對(duì)象,可繼承MyBatis-Flex提供的接口,并指定泛型為當(dāng)前操作的實(shí)體類對(duì)象
import com.hezy.pojo.User;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
創(chuàng)建一個(gè)接口,根據(jù)ID查詢User,可直接調(diào)用MyBatis-Flex提供的相關(guān)API,如下:
@Autowired
private UserMapper userMapper;
@GetMapping("/getUser/{id}")
public User getUserById(@PathVariable("id") Integer id) {
return userMapper.selectOneById(id);
}
一般來(lái)說(shuō),還需要Services過(guò)一層,校驗(yàn)參數(shù)等,這里是是一個(gè)Demo。數(shù)據(jù)庫(kù)內(nèi)容如下:

啟動(dòng)項(xiàng)目,調(diào)用接口,如下,查詢完成

總結(jié)
本文介紹了如何在Spring Boot項(xiàng)目中整合MyBatis-Flex,當(dāng)然,MyBatis-Flex還提供了許多數(shù)據(jù)訪問(wèn)的API,以及擴(kuò)展功能,如多數(shù)據(jù)源訪問(wèn)、數(shù)據(jù)庫(kù)配置加密、多租戶、讀寫分離等等,可在官網(wǎng)上學(xué)習(xí)。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- MyBatis-Flex實(shí)現(xiàn)分頁(yè)查詢的示例代碼
- Mybatis-flex整合達(dá)夢(mèng)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
- SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn)
- mybatis-flex實(shí)現(xiàn)鏈?zhǔn)讲僮鞯氖纠a
- mybatis-flex實(shí)現(xiàn)多數(shù)據(jù)源操作
- MyBatis-Flex實(shí)現(xiàn)多表聯(lián)查(自動(dòng)映射)
- Springboot集成Mybatis-Flex的示例詳解
- mybatis-flex與springBoot整合的實(shí)現(xiàn)示例
- MyBatis-Flex 邏輯刪除的用法小結(jié)
相關(guān)文章
Docker環(huán)境下Spring Boot應(yīng)用內(nèi)存飆升分析與解決場(chǎng)景分析
當(dāng)運(yùn)行一個(gè)Spring Boot項(xiàng)目時(shí),如果未設(shè)置JVM內(nèi)存參數(shù),Spring Boot默認(rèn)會(huì)采用JVM自身默認(rèn)的配置策略,接下來(lái)通過(guò)本文給大家介紹Docker環(huán)境下Spring Boot應(yīng)用內(nèi)存飆升分析與解決方法,需要的朋友參考下吧2021-08-08
Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Cloud具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java類的繼承實(shí)例詳解(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
在Java開發(fā)中,我們常常用到繼承這一概念,可以說(shuō)繼承是Java這類面向?qū)ο缶幊陶Z(yǔ)言的基石,今天小編一起和大家一起學(xué)習(xí)java類的繼承2017-04-04
JAVA正則表達(dá)式及字符串的替換與分解相關(guān)知識(shí)總結(jié)
今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí)總結(jié),文章圍繞著JAVA正則表達(dá)式及字符串的替換與分解展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
idea全局搜索快捷鍵超詳細(xì)總結(jié)(推薦!)
在實(shí)際開發(fā)中項(xiàng)目會(huì)非常多,如何在項(xiàng)目中快速定位,你說(shuō)需要找到的類或方法,可以利用idea的全局搜索功能,下面這篇文章主要給大家分享介紹了關(guān)于idea全局搜索快捷鍵超詳細(xì)總結(jié)的相關(guān)資料,需要的朋友可以參考下2023-01-01
SpringBoot整合Echarts繪制靜態(tài)數(shù)據(jù)柱狀圖和餅圖
這篇文章給大家介紹了SpringBoot整合Echarts繪制靜態(tài)數(shù)據(jù)柱狀圖和餅圖,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03
解決Mybatis中foreach嵌套使用if標(biāo)簽對(duì)象取值的問(wèn)題
這篇文章主要介紹了解決Mybatis中foreach嵌套使用if標(biāo)簽對(duì)象取值的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

