From e752b8f404233896e11e8a11115a3c6a09c21216 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 28 Oct 2023 22:23:01 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=8B=AC?= =?UTF-8?q?=E5=8F=B7=E6=9E=84=E5=BB=BA=E6=94=AF=E6=8C=81=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/mybatisflex/core/query/QueryMethods.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java index 8aa83d5e..a51d20ee 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java @@ -2564,6 +2564,13 @@ public class QueryMethods { return QueryCondition.createEmpty(); } + /** + * 括号条件。 + */ + public static QueryCondition bracket(QueryCondition condition) { + return new Brackets(condition); + } + // === 构建 QueryWrapper 查询 === /** From ddaf860bf8d86996a338bad9b9ec7e064c52e4ae Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 28 Oct 2023 22:23:22 +0800 Subject: [PATCH 2/4] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E6=8B=AC=E5=8F=B7=E6=9D=A1=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coretest/DynamicConditionTest.java | 23 ++++++++++++++++++- .../src/main/resources/application.yml | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java index bfab8f3b..4f8f2831 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java @@ -20,12 +20,14 @@ import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.query.*; import com.mybatisflex.core.util.StringUtil; import org.junit.Test; + import java.util.Arrays; import java.util.List; import java.util.Objects; -import static org.junit.Assert.assertEquals; +import static com.mybatisflex.core.query.QueryMethods.bracket; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; +import static org.junit.Assert.assertEquals; /** * 动态条件测试。 @@ -184,4 +186,23 @@ public class DynamicConditionTest { assertEquals("SELECT * FROM `tb_account`", sql); } + @Test + public void test11() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select() + .from(ACCOUNT) + .where(ACCOUNT.IS_DELETE.eq(0)) + .and(ACCOUNT.ID.ge("1").and(bracket(ACCOUNT.AGE.ge(18).or(ACCOUNT.USER_NAME.ge("zs"))))) + .or(ACCOUNT.BIRTHDAY.le("2023-10-28 22:13:36")); + + String printSql = queryWrapper.toSQL(); + + assertEquals("SELECT * FROM `tb_account` " + + "WHERE `is_delete` = 0 " + + "AND (`id` >= '1' AND (`age` >= 18 OR `user_name` >= 'zs')) " + + "OR `birthday` <= '2023-10-28 22:13:36'", printSql); + + System.out.println(printSql); + } + } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index c6e3ecb6..0ce252f9 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -7,7 +7,7 @@ spring: # driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/flex_test username: root - password: Aa123456# + password: 12345678 # driver-class-name: # datasource: # driver-class-name: org.h2.Driver From 746041aec452e70b3aca7f4c68d1f3e324626fe9 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 28 Oct 2023 22:23:39 +0800 Subject: [PATCH 3/4] =?UTF-8?q?doc:=20=E6=B7=BB=E5=8A=A0=E6=8B=AC=E5=8F=B7?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E4=BD=BF=E7=94=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/base/querywrapper.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/zh/base/querywrapper.md b/docs/zh/base/querywrapper.md index f03717e9..57d341f5 100644 --- a/docs/zh/base/querywrapper.md +++ b/docs/zh/base/querywrapper.md @@ -664,6 +664,30 @@ AND (sex = ? OR sex = ? ) OR (age IN (?,?,?) AND user_name LIKE ? ) ``` +## where 括号 + + + +```java 6 +QueryWrapper queryWrapper = QueryWrapper.create() + .select() + .from(ACCOUNT) + .where(ACCOUNT.IS_DELETE.eq(0)) + .and(ACCOUNT.ID.ge("1").and( + bracket(ACCOUNT.AGE.ge(18).or(ACCOUNT.USER_NAME.ge("zs"))) + )) + .or(ACCOUNT.BIRTHDAY.le(new Date())); +``` + +其生成的 SQL 为: + +```sql +SELECT * FROM `tb_account` +WHERE `is_delete` = 0 +AND (`id` >= '1' AND (`age` >= 18 OR `user_name` >= 'zs')) +OR `birthday` <= '2023-10-28 22:13:36' +``` + ## 自定义字符串列名 From 398321e424c0089a40c3c3df5f75612071e101c3 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sat, 28 Oct 2023 22:24:09 +0800 Subject: [PATCH 4/4] =?UTF-8?q?doc:=20=E8=A1=A5=E5=85=A8=E8=A7=86=E9=A2=91?= =?UTF-8?q?=E6=95=99=E7=A8=8B=E5=9C=B0=E5=9D=80=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/awesome-things.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/zh/awesome-things.md b/docs/zh/awesome-things.md index 836b3f8f..bf9d501b 100644 --- a/docs/zh/awesome-things.md +++ b/docs/zh/awesome-things.md @@ -62,6 +62,8 @@ - [MyBatis-Flex 视频教程 - 51 数据缓存的简单使用](https://www.bilibili.com/video/BV1F8411X7EB) - [MyBatis-Flex 视频教程 - 52 动态表名](https://www.bilibili.com/video/BV1D8411Q7tD) - [MyBatis-Flex 视频教程 - 53 Active Record 模式](https://www.bilibili.com/video/BV1Lh4y1T7Ey) +- [MyBatis-Flex 视频教程 - 54 SQL 审计](https://www.bilibili.com/video/BV1kh4y127kz) +- [MyBatis-Flex 视频教程 - 55 SQL 打印](https://www.bilibili.com/video/BV1914y1C7Xk) - [MyBatis-Flex 视频教程 - 56 MyBatis-Flex APT 配置](https://www.bilibili.com/video/BV1fh4y1T7o1) - [MyBatis-Flex 视频教程 - 57 代码生成器(风格一)](https://www.bilibili.com/video/BV1o14y1C7pJ) - [MyBatis-Flex 视频教程 - 58 代码生成器(风格二)](https://www.bilibili.com/video/BV14P411h7eM)