!376 feat:带括号条件的构建

Merge pull request !376 from 王帅/main
This commit is contained in:
Michael Yang 2023-10-28 23:14:37 +00:00 committed by Gitee
commit 6a2c3010ef
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 56 additions and 2 deletions

View File

@ -62,6 +62,8 @@
- [MyBatis-Flex 视频教程 - 51 数据缓存的简单使用](https://www.bilibili.com/video/BV1F8411X7EB) - [MyBatis-Flex 视频教程 - 51 数据缓存的简单使用](https://www.bilibili.com/video/BV1F8411X7EB)
- [MyBatis-Flex 视频教程 - 52 动态表名](https://www.bilibili.com/video/BV1D8411Q7tD) - [MyBatis-Flex 视频教程 - 52 动态表名](https://www.bilibili.com/video/BV1D8411Q7tD)
- [MyBatis-Flex 视频教程 - 53 Active Record 模式](https://www.bilibili.com/video/BV1Lh4y1T7Ey) - [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 视频教程 - 56 MyBatis-Flex APT 配置](https://www.bilibili.com/video/BV1fh4y1T7o1)
- [MyBatis-Flex 视频教程 - 57 代码生成器(风格一)](https://www.bilibili.com/video/BV1o14y1C7pJ) - [MyBatis-Flex 视频教程 - 57 代码生成器(风格一)](https://www.bilibili.com/video/BV1o14y1C7pJ)
- [MyBatis-Flex 视频教程 - 58 代码生成器(风格二)](https://www.bilibili.com/video/BV14P411h7eM) - [MyBatis-Flex 视频教程 - 58 代码生成器(风格二)](https://www.bilibili.com/video/BV14P411h7eM)

View File

@ -664,6 +664,30 @@ AND (sex = ? OR sex = ? )
OR (age IN (?,?,?) AND user_name LIKE ? ) 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'
```
## 自定义字符串列名 ## 自定义字符串列名

View File

@ -2564,6 +2564,13 @@ public class QueryMethods {
return QueryCondition.createEmpty(); return QueryCondition.createEmpty();
} }
/**
* 括号条件
*/
public static QueryCondition bracket(QueryCondition condition) {
return new Brackets(condition);
}
// === 构建 QueryWrapper 查询 === // === 构建 QueryWrapper 查询 ===
/** /**

View File

@ -20,12 +20,14 @@ import com.mybatisflex.core.constant.SqlConnector;
import com.mybatisflex.core.query.*; import com.mybatisflex.core.query.*;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; 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 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); 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);
}
} }

View File

@ -7,7 +7,7 @@ spring:
# driver-class-name: com.mysql.cj.jdbc.Driver # driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/flex_test url: jdbc:mysql://localhost:3306/flex_test
username: root username: root
password: Aa123456# password: 12345678
# driver-class-name: # driver-class-name:
# datasource: # datasource:
# driver-class-name: org.h2.Driver # driver-class-name: org.h2.Driver