mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
commit
6a2c3010ef
@ -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)
|
||||
|
||||
@ -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'
|
||||
```
|
||||
|
||||
## 自定义字符串列名
|
||||
|
||||
|
||||
|
||||
@ -2564,6 +2564,13 @@ public class QueryMethods {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 括号条件。
|
||||
*/
|
||||
public static QueryCondition bracket(QueryCondition condition) {
|
||||
return new Brackets(condition);
|
||||
}
|
||||
|
||||
// === 构建 QueryWrapper 查询 ===
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user