Spring Boot整合MyBatis-Flex全過程
說明:MyBatis-Flex(官網(wǎng)地址:https://mybatis-flex.com/),是一款數(shù)據(jù)訪問層框架,可實(shí)現(xiàn)項(xiàng)目中對(duì)數(shù)據(jù)庫的訪問,類比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è)試,沒得問題

整合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ù)訪問層對(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);
}
一般來說,還需要Services過一層,校驗(yàn)參數(shù)等,這里是是一個(gè)Demo。數(shù)據(jù)庫內(nèi)容如下:

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

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- MyBatis-Flex實(shí)現(xiàn)分頁查詢的示例代碼
- Mybatis-flex整合達(dá)夢(mèng)數(shù)據(jù)庫的實(shí)現(xiàn)示例
- SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫訪問
- 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)文章
如何解決java.util.concurrent.CancellationException問題
這篇文章主要介紹了如何解決java.util.concurrent.CancellationException問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
nacos gateway動(dòng)態(tài)路由實(shí)戰(zhàn)
這篇文章主要介紹了nacos gateway動(dòng)態(tài)路由實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
ZooKeeper官方文檔之Java客戶端開發(fā)案例翻譯
網(wǎng)上有很多ZooKeeper的java客戶端例子,我也看過很多,不過大部分寫的都不好,有各種問題。兜兜轉(zhuǎn)轉(zhuǎn)還是覺得官方給的例子最為經(jīng)典,在學(xué)習(xí)之余翻譯下來,供朋友們參考2022-01-01
springBoot 插件工具熱部署 Devtools的步驟詳解
這篇文章主要介紹了springBoot 插件工具 熱部署 Devtools,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

