mybatis-flex實(shí)現(xiàn)鏈?zhǔn)讲僮鞯氖纠a
鏈?zhǔn)讲僮?a rel="external nofollow" target="_blank">?
在 MyBatis-Flex 中,內(nèi)置了 QueryChain.java 、 UpdateChain.java 以及 DbChain.java 用于對(duì)數(shù)據(jù)進(jìn)行鏈?zhǔn)讲樵儾僮骱玩準(zhǔn)讲僮鳎ㄐ薷暮蛣h除)。
- QueryChain:鏈?zhǔn)讲樵?/li>
- UpdateChain:鏈?zhǔn)礁?/li>
- DbChain:鏈?zhǔn)秸{(diào)用 Db + Row
QueryChain 示例?
例如,查詢文章列表代碼如下:
@SpringBootTest
class ArticleServiceTest {
@Autowired
ArticleService articleService;
@Test
void testChain() {
List<Article> articles = articleService.queryChain()
.select(ARTICLE.ALL_COLUMNS)
.from(ARTICLE)
.where(ARTICLE.ID.ge(100))
.list();
}
}若不是在 Service 中,我們也可以通過(guò) QueryChain.of(mapper) 方法,自己創(chuàng)建一個(gè) QueryChain 實(shí)例,代碼如下:
List<Article> articles = QueryChain.of(mapper)
.select(ARTICLE.ALL_COLUMNS)
.from(ARTICLE)
.where(ARTICLE.ID.ge(100))
.list();UpdateChain 示例?
假設(shè)我們要更新 Account 的 userName 為 "張三",更新年齡在之前的基礎(chǔ)上加 1,更新代碼如下:
@Test
public void testUpdateChain1() {
UpdateChain.of(Account.class)
.set(Account::getUserName, "張三")
.setRaw(Account::getAge, "age + 1")
.where(Account::getId).eq(1)
.update();
}以上方法調(diào)用時(shí),MyBatis-Flex 內(nèi)部執(zhí)行的 SQL 如下:
UPDATE `tb_account` SET `user_name` = '張三' , `age` = age + 1 WHERE `id` = 1
另一個(gè)示例:
@Test
public void testUpdateChain2() {
//更新數(shù)據(jù)
UpdateChain.of(Account.class)
.set(Account::getAge, ACCOUNT.AGE.add(1))
.where(Account::getId).ge(100)
.and(Account::getAge).eq(18)
.update();
//查詢所有數(shù)據(jù)并打印
QueryChain.of(accountMapper)
.where(Account::getId).ge(100)
.and(Account::getAge).eq(18)
.list()
.forEach(System.out::println);
}通過(guò) UpdateChain 進(jìn)行 update(),其執(zhí)行的 SQL 如下:
UPDATE `tb_account` SET `age` = `age` + 1 WHERE `id` >= 100 AND `age` = 18
QueryChain 的方法?
- one():獲取一條數(shù)據(jù)
- list():獲取多條數(shù)據(jù)
- page():分頁(yè)查詢
- obj():當(dāng) SQL 查詢只返回 1 列數(shù)據(jù)的時(shí)候,且只有 1 條數(shù)據(jù)時(shí),可以使用此方法
- objList():當(dāng) SQL 查詢只返回 1 列數(shù)據(jù)的時(shí)候,可以使用此方法
- count():查詢數(shù)據(jù)條數(shù)
- exists():是否存在,判斷 count 是否大于 0
QueryChain 擴(kuò)展方法?
one() 系列方法?
- one():獲取一條數(shù)據(jù)
- oneAs(asType):查詢數(shù)據(jù),并直接轉(zhuǎn)換為 vo、dto 等
- oneWithRelations:查詢一條數(shù)據(jù)及其關(guān)聯(lián)數(shù)據(jù)
- oneWithRelationsAs:查詢一條數(shù)據(jù)及其關(guān)聯(lián)數(shù)據(jù),并直接轉(zhuǎn)換為 vo、dto 等
- oneOpt:返回 Optional 類型,獲取一條數(shù)據(jù)
- oneAsOpt(asType):返回 Optional 類型,查詢數(shù)據(jù),并直接轉(zhuǎn)換為 vo、dto 等
- oneWithRelationsOpt:返回 Optional 類型,查詢一條數(shù)據(jù)及其關(guān)聯(lián)數(shù)據(jù)
- oneWithRelationsAsOpt:返回 Optional 類型,查詢一條數(shù)據(jù)及其關(guān)聯(lián)數(shù)據(jù),并直接轉(zhuǎn)換為 vo、dto 等
list() 系列方法?
- list():查詢數(shù)據(jù)列表
- listWithRelations():查詢數(shù)據(jù)列表及其關(guān)聯(lián)數(shù)據(jù)
- listAs():查詢數(shù)據(jù)列表,并直接轉(zhuǎn)換為 vo、dto 等
- listWithRelationsAs():查詢數(shù)據(jù)列表,及其關(guān)聯(lián)數(shù)據(jù),并直接轉(zhuǎn)換為 vo、dto 等
page() 系列方法?
- page(page):分頁(yè)查詢數(shù)據(jù)列表
- pageAs(page):分頁(yè)查詢數(shù)據(jù)列表,并直接轉(zhuǎn)換為 vo、dto 等
obj() 系列方法?
- obj():查詢第一列,且第一條數(shù)據(jù)
- objAs(asType):查詢第一列,且第一條數(shù)據(jù)并轉(zhuǎn)換為指定類型,比如 Long, String 等
- objOpt():返回 Optional 類型,查詢第一列,且第一條數(shù)據(jù)
- objAsOpt(asType):返回 Optional 類型,查詢第一列,且第一條數(shù)據(jù)并轉(zhuǎn)換為指定類型,比如 Long, String 等
objList() 系列方法?
- objList():查詢第一列
- objListAs(asType):查詢第一列,并轉(zhuǎn)換為指定類型,比如 Long, String 等
代碼實(shí)戰(zhàn)示例?
示例 1:查詢 Entity 列表?
List<Article> articles = articleService.queryChain()
.select(ARTICLE.ALL_COLUMNS)
.from(ARTICLE)
.where(ARTICLE.ID.ge(100))
.list();示例 2:查詢 1 條 Entity 數(shù)據(jù)?
Article article = articleService.queryChain()
.select(ARTICLE.ALL_COLUMNS)
.from(ARTICLE)
.where(ARTICLE.ID.ge(100))
.limit(1)
.one();示例 3:查詢 VO 數(shù)據(jù)(ArticleVo)?
ArticleVo.java
public class ArticleVo {
private Long id;
private Long accountId;
private String title;
private String content;
//評(píng)論量最多的內(nèi)容
private Long maxComments;
//getter setter
}查詢代碼:
ArticleVo articleVo = articleService.queryChain()
.select(
ARTICLE.ALL_COLUMNS,
max(ARTICLE.comments).as(ArticleVo::maxComments)
).from(ARTICLE)
.where(ARTICLE.ID.ge(100))
.limit(1)
.oneAs(ArticleVo.class);示例 4:多對(duì)多關(guān)聯(lián)查詢 VO 數(shù)據(jù)(ArticleVo)?
ArticleVo.java 及其 文章分類 定義:
public class ArticleVo {
private Long id;
private Long accountId;
private String title;
private String content;
//文章和分類的 多對(duì)多 關(guān)系配置
@RelationManyToMany(
joinTable = "tb_article_category_mapping", // 中間表
selfField = "id", joinSelfColumn = "article_id",
targetField = "id", joinTargetColumn = "category_id"
)
private List<ArticleCategory> categories;
//getter setter
}查詢代碼:
ArticleVo articleVo = articleService.queryChain()
.select()
.from(ARTICLE)
.where(ARTICLE.ID.ge(100))
.limit(1)
.oneWithRelationsAs(ArticleVo.class);通過(guò) oneWithRelationsAs 方法查詢 ArticleVo 及其關(guān)聯(lián)數(shù)據(jù)(多對(duì)多的文章分類)。 更多關(guān)于關(guān)聯(lián)查詢的內(nèi)容請(qǐng)參考章節(jié):《關(guān)聯(lián)查詢》。
DbChain 示例?
使用 DbChain 之后無(wú)需將 QueryWrapper 與 Row 的構(gòu)建分離,直接即可進(jìn)行操作。
// 新增 Row 構(gòu)建
DbChain.table("tb_account")
.setId(RowKey.AUTO)
.set("user_name", "zhang san")
.set("age", 18)
.set("birthday", new Date())
.save();
// 查詢 QueryWrapper 構(gòu)建
DbChain.table("tb_account")
.select("id", "user_name", "age", "birthday")
.where("age > ?", 18)
.list()
.forEach(System.out::println);到此這篇關(guān)于mybatis-flex實(shí)現(xiàn)鏈?zhǔn)讲僮鞯氖纠a的文章就介紹到這了,更多相關(guān)mybatis-flex鏈?zhǔn)讲僮鲀?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis-Flex實(shí)現(xiàn)分頁(yè)查詢的示例代碼
- Mybatis-flex整合達(dá)夢(mèng)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
- Spring Boot整合MyBatis-Flex全過(guò)程
- SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn)
- 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?DOC如何生成標(biāo)準(zhǔn)的JAVA?API文檔詳解
這篇文章主要給大家介紹了關(guān)于JAVA?DOC如何生成標(biāo)準(zhǔn)的JAVA?API文檔的相關(guān)資料,Javadoc是Sun公司提供的一種工具,它可以從程序源代碼中抽取類、方法、成員等注釋,然后形成一個(gè)和源代碼配套的API幫助文檔,需要的朋友可以參考下2024-06-06
RestClient?通過(guò)攔截器實(shí)現(xiàn)請(qǐng)求加密的示例
本文介紹了如何通過(guò)攔截器實(shí)現(xiàn)請(qǐng)求加密,并通過(guò)RestClient優(yōu)化了加密過(guò)程,傳統(tǒng)的加密方法依賴對(duì)象轉(zhuǎn)換和序列化處理,容易導(dǎo)致加密不一致或難以調(diào)試的問(wèn)題,通過(guò)引入攔截器,可以直接操作請(qǐng)求體,避免了不必要的轉(zhuǎn)換步驟,確保加密過(guò)程與請(qǐng)求體完全一致,感興趣的朋友一起看看吧2025-02-02
java以json格式向后臺(tái)服務(wù)器接口發(fā)送請(qǐng)求的實(shí)例
下面小編就為大家分享一篇java以json格式向后臺(tái)服務(wù)器接口發(fā)送請(qǐng)求的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(16)
下面小編就為大家?guī)?lái)一篇Java基礎(chǔ)的幾道練習(xí)題(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望可以幫到你2021-07-07
一篇文章大家徹底學(xué)會(huì)Java之格式化輸出
Java提供了多種格式化輸出的方式,包括String.format()、System.out.printf()和Formatter類,這些方法支持?jǐn)?shù)字、日期、字符串等多種格式化操作,這篇文章主要介紹了Java格式化輸出的相關(guān)資料,需要的朋友可以參考下2025-04-04
Jackson2的JsonSchema實(shí)現(xiàn)java實(shí)體類生成json方式
這篇文章主要介紹了Jackson2的JsonSchema實(shí)現(xiàn)java實(shí)體類生成json,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
@PathVariable為空時(shí)指定默認(rèn)值的操作
這篇文章主要介紹了@PathVariable為空時(shí)指定默認(rèn)值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02

