最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis-flex實現(xiàn)鏈式操作的示例代碼

 更新時間:2024年06月24日 10:25:46   作者:kunkun2580  
MyBatis-Flex它提供了一種鏈式操作方式,本文主要介紹了mybatis-flex實現(xiàn)鏈式操作的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

鏈式操作?

在 MyBatis-Flex 中,內(nèi)置了 QueryChain.java 、 UpdateChain.java 以及 DbChain.java 用于對數(shù)據(jù)進行鏈式查詢操作和鏈式操作(修改和刪除)。

  • QueryChain:鏈式查詢
  • UpdateChain:鏈式更新
  • DbChain:鏈式調(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 中,我們也可以通過 QueryChain.of(mapper) 方法,自己創(chuàng)建一個 QueryChain 實例,代碼如下:

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)用時,MyBatis-Flex 內(nèi)部執(zhí)行的 SQL 如下:

UPDATE `tb_account` SET `user_name` = '張三' , `age` = age + 1
WHERE `id` = 1

另一個示例:

@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);
}

通過 UpdateChain 進行 update(),其執(zhí)行的 SQL 如下:

UPDATE `tb_account` SET `age` = `age` + 1
WHERE  `id` >= 100 AND `age` = 18

QueryChain 的方法?

  • one():獲取一條數(shù)據(jù)
  • list():獲取多條數(shù)據(jù)
  • page():分頁查詢
  • obj():當 SQL 查詢只返回 1 列數(shù)據(jù)的時候,且只有 1 條數(shù)據(jù)時,可以使用此方法
  • objList():當 SQL 查詢只返回 1 列數(shù)據(jù)的時候,可以使用此方法
  • count():查詢數(shù)據(jù)條數(shù)
  • exists():是否存在,判斷 count 是否大于 0

QueryChain 擴展方法?

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):分頁查詢數(shù)據(jù)列表
  • pageAs(page):分頁查詢數(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 等

代碼實戰(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;

    //評論量最多的內(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:多對多關(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;

    //文章和分類的 多對多 關(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);

通過 oneWithRelationsAs 方法查詢 ArticleVo 及其關(guān)聯(lián)數(shù)據(jù)(多對多的文章分類)。 更多關(guān)于關(guān)聯(lián)查詢的內(nèi)容請參考章節(jié):《關(guān)聯(lián)查詢》。

DbChain 示例?

使用 DbChain 之后無需將 QueryWrapper 與 Row 的構(gòu)建分離,直接即可進行操作。

// 新增 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實現(xiàn)鏈式操作的示例代碼的文章就介紹到這了,更多相關(guān)mybatis-flex鏈式操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論

普兰县| 丁青县| 淮北市| 六枝特区| 合水县| 盐源县| 青州市| 颍上县| 柳江县| 屏东市| 五河县| 西畴县| 崇州市| 勃利县| 遵义县| 博白县| 博乐市| 年辖:市辖区| 鹤庆县| 五莲县| 化州市| 航空| 井冈山市| 连南| 文山县| 富锦市| 嫩江县| 岳阳县| 余干县| 阳西县| 吉首市| 瑞金市| 浦江县| 北川| 潼南县| 百色市| 余姚市| 沽源县| 陇南市| 建德市| 稻城县|