Spring Boot整合MyBatis-Flex全過程
說明:MyBatis-Flex(官網(wǎng)地址:https://mybatis-flex.com/),是一款數(shù)據(jù)訪問層框架,可實現(xiàn)項目中對數(shù)據(jù)庫的訪問,類比MyBatis-Plus。
本文介紹,在Spring Boot項目整合MyBatis-Flex。
創(chuàng)建項目
首先,創(chuàng)建一個Spring boot項目,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)建一個實體類對象,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)建一個接口,如下:
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
啟動項目,測試,沒得問題

整合MyBatis-Flex
引入下面這兩個依賴;
<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>
在實體類上關(guān)聯(lián)表,包括實體類對應的表名,以及對應的字段,像主鍵、別名(如果有)等;
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ù)訪問層對象,可繼承MyBatis-Flex提供的接口,并指定泛型為當前操作的實體類對象
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)建一個接口,根據(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過一層,校驗參數(shù)等,這里是是一個Demo。數(shù)據(jù)庫內(nèi)容如下:

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

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

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

