From 5b6126738a7003a27886038e52490411776885d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 22 Jul 2023 15:24:02 +0800 Subject: [PATCH] docs: update docs --- docs/.vitepress/config.ts | 1 + docs/zh/base/query-wrapper-chain.md | 75 +++++++++++++++++++ .../test/service/ArticleServiceTest.java | 3 +- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 docs/zh/base/query-wrapper-chain.md diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 23de0a52..7bb627d8 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -61,6 +61,7 @@ export default defineConfig({ {text: '关联查询', link: '/zh/base/relations-query'}, {text: '批量操作', link: '/zh/base/batch'}, {text: 'QueryWrapper', link: '/zh/base/querywrapper'}, + {text: 'QueryWrapperChain', link: '/zh/base/query-wrapper-chain'}, {text: 'Db + Row', link: '/zh/base/db-row'}, {text: 'IService', link: '/zh/base/service'}, {text: 'SpringBoot 配置文件', link: '/zh/base/configuration'}, diff --git a/docs/zh/base/query-wrapper-chain.md b/docs/zh/base/query-wrapper-chain.md new file mode 100644 index 00000000..fdd13998 --- /dev/null +++ b/docs/zh/base/query-wrapper-chain.md @@ -0,0 +1,75 @@ +# QueryWrapperChain + +`QueryWrapperChain.java` 是一个对 `QueryWrapper` 进行链式调用封装的一个类,在 Service 中, +我们可以调用 `service.queryChain()` 获得该实例。 + + +例如,查询文章列表代码如下: + +```java +@SpringBootTest +class ArticleServiceTest { + + @Autowired + ArticleService articleService; + + @Test + void testChain() { + List
articles = articleService.queryChain() + .select(ARTICLE.ALL_COLUMNS) + .from(ARTICLE) + .where(ARTICLE.ID.ge(100)) + .list(); + } +} +``` + +若不是在 Service 中,我们也可以通过 `QueryWrapperChain.create` 方法,自己创建一个 `QueryWrapperChain` 实例,代码如下: + +```java +List
articles = QueryWrapperChain.create(mapper) + .select(ARTICLE.ALL_COLUMNS) + .from(ARTICLE) + .where(ARTICLE.ID.ge(100)) + .list(); +``` + +## QueryWrapperChain 的方法 + +- one():获取一条数据 +- list():获取多条数据 +- page():分页查询 +- remove():删除数据 +- update(entity):更新数据 +- count():查询数据条数 +- exists():是否存在,判断 count 是否大于 0 + +## 扩展方法 + +### `one()` 系列方法 + +- one():获取一条数据 +- oneAs(asType):查询数据,并直接转换为 vo、dto 等 +- oneWithRelations:查询一条数据及其关联数据 +- oneWithRelationsAs:查询一条数据及其关联数据,并直接转换为 vo、dto 等 +- oneOpt:返回 Optional 类型,获取一条数据 +- oneAsOpt(asType):返回 Optional 类型,查询数据,并直接转换为 vo、dto 等 +- oneWithRelationsOpt:返回 Optional 类型,查询一条数据及其关联数据 +- oneWithRelationsAsOpt:返回 Optional 类型,查询一条数据及其关联数据,并直接转换为 vo、dto 等 + + +### `list()` 系列方法 + +- list():查询数据列表 +- listWithRelations():查询数据列表极其关联数据 +- listAs():查询数据列表,并直接转换为 vo、dto 等 +- listWithRelationsAs():查询数据列表,及其关联数据,并直接转换为 vo、dto 等 + + +### `page()` 系列方法 + +- page(page):分页查询数据列表 +- pageWithRelations(page):分页查询数据列表极其关联数据 +- pageAs(page):分页查询数据列表,并直接转换为 vo、dto 等 +- pageWithRelationsAs(page):分页查询数据列表,及其关联数据,并直接转换为 vo、dto 等 + diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/service/ArticleServiceTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/service/ArticleServiceTest.java index 439278c4..830bce31 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/service/ArticleServiceTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/service/ArticleServiceTest.java @@ -16,7 +16,6 @@ package com.mybatisflex.test.service; -import com.mybatisflex.test.model.Article; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -38,7 +37,7 @@ class ArticleServiceTest { articleService.queryChain() .select(ARTICLE.ALL_COLUMNS) .from(ARTICLE) - .where(Article::getAccountId).eq(1) + .where(ARTICLE.ID.ge(100)) .list() .forEach(System.out::println); }