|
@@ -16,13 +16,13 @@
|
|
|
package com.baomidou.mybatisplus.core.test;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
+import org.assertj.core.api.Assertions;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
import java.time.LocalDate;
|
|
@@ -34,9 +34,11 @@ class WrapperTest {
|
|
|
System.out.println(message);
|
|
|
}
|
|
|
|
|
|
- private void logSqlSegment(String explain, ISqlSegment sqlSegment) {
|
|
|
+ private void logSqlSegment(String explain, Wrapper<?> wrapper, String targetSql) {
|
|
|
System.out.println(String.format(" ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ->(%s)<- ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓", explain));
|
|
|
- System.out.println(sqlSegment.getSqlSegment());
|
|
|
+ System.out.println(wrapper.getSqlSegment());
|
|
|
+ System.out.println(wrapper.getTargetSql());
|
|
|
+ Assertions.assertThat(wrapper.getTargetSql().trim()).isEqualTo(targetSql);
|
|
|
}
|
|
|
|
|
|
private <T> void logParams(QueryWrapper<T> wrapper) {
|
|
@@ -59,15 +61,17 @@ class WrapperTest {
|
|
|
@Test
|
|
|
void test1() {
|
|
|
QueryWrapper<User> ew = new QueryWrapper<User>() {
|
|
|
- /**
|
|
|
- * serialVersionUID
|
|
|
- */
|
|
|
- private static final long serialVersionUID = 4719966531503901490L;
|
|
|
- {
|
|
|
- eq("xxx", 123);
|
|
|
- and(i -> i.eq("andx", 65444).le("ande", 66666));
|
|
|
- ne("xxx", 222);
|
|
|
- }};
|
|
|
+ /**
|
|
|
+ * serialVersionUID
|
|
|
+ */
|
|
|
+ private static final long serialVersionUID = 4719966531503901490L;
|
|
|
+
|
|
|
+ {
|
|
|
+ eq("xxx", 123);
|
|
|
+ and(i -> i.eq("andx", 65444).le("ande", 66666));
|
|
|
+ ne("xxx", 222);
|
|
|
+ }
|
|
|
+ };
|
|
|
log(ew.getSqlSegment());
|
|
|
log(ew.getSqlSegment());
|
|
|
ew.gt("x22", 333);
|
|
@@ -101,40 +105,51 @@ class WrapperTest {
|
|
|
@Test
|
|
|
void testQueryWrapper() {
|
|
|
logSqlSegment("去除第一个 or,以及自动拼接 and,以及手动拼接 or,以及去除最后的多个or", new QueryWrapper<User>().or()
|
|
|
- .ge("age", 3).or().ge("age", 3).ge("age", 3).or().or().or().or());
|
|
|
+ .ge("age", 3).or().ge("age", 3).ge("age", 3).or().or().or().or(),
|
|
|
+ "(age >= ? OR age >= ? AND age >= ?)");
|
|
|
|
|
|
logSqlSegment("多个 or 相连接,去除多余的 or", new QueryWrapper<User>()
|
|
|
- .ge("age", 3).or().or().or().ge("age", 3).or().or().ge("age", 3));
|
|
|
+ .ge("age", 3).or().or().or().ge("age", 3).or().or().ge("age", 3),
|
|
|
+ "(age >= ? OR age >= ? OR age >= ?)");
|
|
|
|
|
|
logSqlSegment("嵌套,正常嵌套", new QueryWrapper<User>()
|
|
|
- .nested(i -> i.eq("id", 1)).eq("id", 1));
|
|
|
+ .nested(i -> i.eq("id", 1)).eq("id", 1),
|
|
|
+ "((id = ?) AND id = ?)");
|
|
|
|
|
|
logSqlSegment("嵌套,第一个套外的 and 自动消除", new QueryWrapper<User>()
|
|
|
- .and(i -> i.eq("id", 1)).eq("id", 1));
|
|
|
+ .and(i -> i.eq("id", 1)).eq("id", 1),
|
|
|
+ "((id = ?) AND id = ?)");
|
|
|
|
|
|
logSqlSegment("嵌套,多层嵌套", new QueryWrapper<User>()
|
|
|
- .and(i -> i.eq("id", 1).and(j -> j.eq("id", 1))));
|
|
|
+ .and(i -> i.eq("id", 1).and(j -> j.eq("id", 1))),
|
|
|
+ "((id = ?) AND (id = ?))");
|
|
|
|
|
|
logSqlSegment("嵌套,第一个套外的 or 自动消除", new QueryWrapper<User>()
|
|
|
- .or(i -> i.eq("id", 1)).eq("id", 1));
|
|
|
+ .or(i -> i.eq("id", 1)).eq("id", 1),
|
|
|
+ "((id = ?) AND id = ?)");
|
|
|
|
|
|
logSqlSegment("嵌套,套内外自动拼接 and", new QueryWrapper<User>()
|
|
|
- .eq("id", 11).and(i -> i.eq("id", 1)).eq("id", 1));
|
|
|
+ .eq("id", 11).and(i -> i.eq("id", 1)).eq("id", 1),
|
|
|
+ "(id = ? AND (id = ?) AND id = ?)");
|
|
|
|
|
|
logSqlSegment("嵌套,套内外手动拼接 or,去除套内第一个 or", new QueryWrapper<User>()
|
|
|
- .eq("id", 11).or(i -> i.or().eq("id", 1)).or().eq("id", 1));
|
|
|
+ .eq("id", 11).or(i -> i.or().eq("id", 1)).or().eq("id", 1),
|
|
|
+ "(id = ? OR (id = ?) OR id = ?)");
|
|
|
|
|
|
logSqlSegment("多个 order by 和 group by 拼接,自动优化顺序,last方法拼接在最后", new QueryWrapper<User>()
|
|
|
- .eq("id", 11)
|
|
|
- .last("limit 1")
|
|
|
- .orderByAsc("id", "name", "sex").orderByDesc("age", "txl")
|
|
|
- .groupBy("id", "name", "sex").groupBy("id", "name"));
|
|
|
+ .eq("id", 11)
|
|
|
+ .last("limit 1")
|
|
|
+ .orderByAsc("id", "name", "sex").orderByDesc("age", "txl")
|
|
|
+ .groupBy("id", "name", "sex").groupBy("id", "name"),
|
|
|
+ "(id = ?) GROUP BY id,name,sex,id,name ORDER BY id ASC,name ASC,sex ASC,age DESC,txl DESC limit 1");
|
|
|
|
|
|
logSqlSegment("只存在 order by", new QueryWrapper<User>()
|
|
|
- .orderByAsc("id", "name", "sex").orderByDesc("age", "txl"));
|
|
|
+ .orderByAsc("id", "name", "sex").orderByDesc("age", "txl"),
|
|
|
+ "ORDER BY id ASC,name ASC,sex ASC,age DESC,txl DESC");
|
|
|
|
|
|
logSqlSegment("只存在 group by", new QueryWrapper<User>()
|
|
|
- .groupBy("id", "name", "sex").groupBy("id", "name"));
|
|
|
+ .groupBy("id", "name", "sex").groupBy("id", "name"),
|
|
|
+ "GROUP BY id,name,sex,id,name");
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -147,7 +162,7 @@ class WrapperTest {
|
|
|
.or().between("id", 1, 2).notBetween("id", 1, 3)
|
|
|
.like("id", 1).notLike("id", 1)
|
|
|
.or().likeLeft("id", 1).likeRight("id", 1);
|
|
|
- logSqlSegment("测试 Compare 下的方法", queryWrapper);
|
|
|
+ logSqlSegment("测试 Compare 下的方法", queryWrapper, null);
|
|
|
logParams(queryWrapper);
|
|
|
}
|
|
|
|
|
@@ -161,7 +176,7 @@ class WrapperTest {
|
|
|
.in("inArray").notIn("notInArray", 1, 2, 3)
|
|
|
.inSql("inSql", "1,2,3,4,5").notInSql("inSql", "1,2,3,4,5")
|
|
|
.having("sum(age) > {0}", 1).having("id is not null");
|
|
|
- logSqlSegment("测试 Func 下的方法", queryWrapper);
|
|
|
+ logSqlSegment("测试 Func 下的方法", queryWrapper, null);
|
|
|
logParams(queryWrapper);
|
|
|
}
|
|
|
|
|
@@ -173,7 +188,7 @@ class WrapperTest {
|
|
|
.apply("date_format(column,'%Y-%m-%d') = {0}", LocalDate.now())
|
|
|
.or().exists("select id from table where age = 1")
|
|
|
.or().notExists("select id from table where age = 1");
|
|
|
- logSqlSegment("测试 Join 下的方法", queryWrapper);
|
|
|
+ logSqlSegment("测试 Join 下的方法", queryWrapper, null);
|
|
|
logParams(queryWrapper);
|
|
|
}
|
|
|
|
|
@@ -183,7 +198,7 @@ class WrapperTest {
|
|
|
.and(i -> i.eq("id", 1).nested(j -> j.ne("id", 2)))
|
|
|
.or(i -> i.eq("id", 1).and(j -> j.ne("id", 2)))
|
|
|
.nested(i -> i.eq("id", 1).or(j -> j.ne("id", 2)));
|
|
|
- logSqlSegment("测试 Nested 下的方法", queryWrapper);
|
|
|
+ logSqlSegment("测试 Nested 下的方法", queryWrapper, null);
|
|
|
logParams(queryWrapper);
|
|
|
}
|
|
|
|
|
@@ -193,14 +208,14 @@ class WrapperTest {
|
|
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
|
|
queryWrapper.lambda().eq(User::getName, "sss");
|
|
|
queryWrapper.lambda().eq(User::getName, "sss2");
|
|
|
- logSqlSegment("测试 PluralLambda", queryWrapper);
|
|
|
+ logSqlSegment("测试 PluralLambda", queryWrapper, null);
|
|
|
logParams(queryWrapper);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
void testInEmptyColl() {
|
|
|
QueryWrapper<User> queryWrapper = new QueryWrapper<User>().in("xxx", Collections.emptyList());
|
|
|
- logSqlSegment("测试 empty 的 coll", queryWrapper);
|
|
|
+ logSqlSegment("测试 empty 的 coll", queryWrapper, null);
|
|
|
}
|
|
|
|
|
|
private List<Object> getList() {
|