Skip to content

Commit 78b5078

Browse files
committed
修正一波bug,以及去除最后一位的or
1 parent 644fbaa commit 78b5078

File tree

5 files changed

+64
-52
lines changed

5 files changed

+64
-52
lines changed

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/AbstractWrapper.java

-10
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,6 @@ public This or(boolean condition) {
214214
return doIt(condition, OR);
215215
}
216216

217-
@Override
218-
public This in(boolean condition, String sql) {
219-
return addNestedCondition(condition, sql, IN);
220-
}
221-
222-
@Override
223-
public This notIn(boolean condition, String sql) {
224-
return not(condition).in(condition, sql);
225-
}
226-
227217
@Override
228218
public This apply(boolean condition, String applySql) {
229219
return doIt(condition, () -> applySql);

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/interfaces/Func.java

+37-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
package com.baomidou.mybatisplus.core.conditions.interfaces;
1717

18+
import static java.util.stream.Collectors.toList;
19+
1820
import java.io.Serializable;
1921
import java.util.Arrays;
2022
import java.util.Collection;
23+
import java.util.Collections;
2124
import java.util.Optional;
22-
import java.util.stream.Collectors;
2325

2426
/**
2527
* <p>
@@ -81,7 +83,7 @@ default This in(R column, Object... values) {
8183
*/
8284
default This in(boolean condition, R column, Object... values) {
8385
return in(condition, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
84-
.collect(Collectors.toList()));
86+
.collect(toList()));
8587
}
8688

8789
/**
@@ -108,7 +110,39 @@ default This notIn(R column, Object... value) {
108110
*/
109111
default This notIn(boolean condition, R column, Object... values) {
110112
return notIn(condition, column, Arrays.stream(Optional.ofNullable(values).orElseGet(() -> new Object[]{}))
111-
.collect(Collectors.toList()));
113+
.collect(toList()));
114+
}
115+
116+
/**
117+
* 拼接 IN ( sql 语句 )
118+
* 例: in("id", "1,2,3,4,5,6")
119+
*/
120+
default This in(R column, String inValue) {
121+
return in(true, column, inValue);
122+
}
123+
124+
/**
125+
* 拼接 IN ( sql 语句 )
126+
* 例: in("id", "1,2,3,4,5,6")
127+
*/
128+
default This in(boolean condition, R column, String inValue) {
129+
return in(condition, column, Collections.singleton(inValue));
130+
}
131+
132+
/**
133+
* 拼接 NOT IN ( sql 语句 )
134+
* 例: notIn("id", "1,2,3,4,5,6")
135+
*/
136+
default This notIn(R column, String inValue) {
137+
return notIn(true, column, inValue);
138+
}
139+
140+
/**
141+
* 拼接 NOT IN ( sql 语句 )
142+
* 例: notIn("id", "1,2,3,4,5,6")
143+
*/
144+
default This notIn(boolean condition, R column, String inValue) {
145+
return notIn(condition, column, Collections.singleton(inValue));
112146
}
113147

114148
/**

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/interfaces/Join.java

-28
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,6 @@ default This or() {
4040
*/
4141
This or(boolean condition);
4242

43-
/**
44-
* 拼接 IN ( sql 语句 )
45-
* 例: in("1,2,3,4,5,6")
46-
*/
47-
default This in(String sql) {
48-
return in(true, sql);
49-
}
50-
51-
/**
52-
* 拼接 IN ( sql 语句 )
53-
* 例: in("1,2,3,4,5,6")
54-
*/
55-
This in(boolean condition, String sql);
56-
57-
/**
58-
* 拼接 NOT IN ( sql 语句 )
59-
* 例: notIn("1,2,3,4,5,6")
60-
*/
61-
default This notIn(String sql) {
62-
return notIn(true, sql);
63-
}
64-
65-
/**
66-
* 拼接 NOT IN ( sql 语句 )
67-
* 例: notIn("1,2,3,4,5,6")
68-
*/
69-
This notIn(boolean condition, String sql);
70-
7143
/**
7244
* 拼接 sql
7345
* 例: apply("date_format(column,'%Y-%m-%d') = '2008-08-08'")

mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/conditions/segments/NormalSegmentList.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class NormalSegmentList extends ArrayList<ISqlSegment> implements ISqlSeg
3838
* 最后一个值
3939
*/
4040
private ISqlSegment lastValue = null;
41+
private boolean executeNot = true;
4142

4243
@Override
4344
public boolean addAll(Collection<? extends ISqlSegment> c) {
@@ -66,9 +67,18 @@ public boolean addAll(Collection<? extends ISqlSegment> c) {
6667
removeLast();
6768
}
6869
}
70+
} else {
71+
executeNot = false;
72+
return false;
73+
}
74+
} else {
75+
if (!executeNot) {
76+
list.add(1, SqlKeyword.NOT);
77+
executeNot = true;
78+
}
79+
if (!MatchSegment.AND_OR.match(lastValue) && !isEmpty()) {
80+
add(SqlKeyword.AND);
6981
}
70-
} else if (!MatchSegment.AND_OR.match(lastValue) && !isEmpty()) {
71-
add(SqlKeyword.AND);
7282
}
7383
//后置处理
7484
this.flushLastValue(list);
@@ -79,12 +89,15 @@ private void flushLastValue(List<? extends ISqlSegment> list) {
7989
lastValue = list.get(list.size() - 1);
8090
}
8191

82-
private void removeLast() {//todo
92+
private void removeLast() {
8393
remove(size() - 1);
8494
}
8595

8696
@Override
8797
public String getSqlSegment() {
98+
if (MatchSegment.AND_OR.match(lastValue)) {
99+
removeLast();
100+
}
88101
return this.stream().map(ISqlSegment::getSqlSegment).collect(Collectors.joining(" "));
89102
}
90103
}

mybatis-plus-core/src/test/java/com/baomidou/mybatisplus/core/test/WrapperTest.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,28 @@ public void test3() {
7373

7474
@Test
7575
public void testQueryWrapper() {
76-
logSqlSegment("去除第一个 or,以及自动拼接 and,以及手动拼接 or", new QueryWrapper<User>().or()
77-
.ge("age", 3).or().ge("age", 3).ge("age", 3));
76+
logSqlSegment("去除第一个 or,以及自动拼接 and,以及手动拼接 or,以及去除最后的多个or", new QueryWrapper<User>().or()
77+
.ge("age", 3).or().ge("age", 3).ge("age", 3).or().or().or().or());
7878

7979
logSqlSegment("多个 or 相连接,去除多余的 or", new QueryWrapper<User>()
8080
.ge("age", 3).or().or().or().ge("age", 3).or().or().ge("age", 3));
8181

82-
logSqlSegment("嵌套测试,正常嵌套", new QueryWrapper<User>()
82+
logSqlSegment("嵌套,正常嵌套", new QueryWrapper<User>()
8383
.nested(i -> i.eq("id", 1)).eq("id", 1));
8484

85-
logSqlSegment("嵌套测试,第一个套外的 and 自动消除", new QueryWrapper<User>()
85+
logSqlSegment("嵌套,第一个套外的 and 自动消除", new QueryWrapper<User>()
8686
.and(i -> i.eq("id", 1)).eq("id", 1));
8787

88-
logSqlSegment("嵌套测试,多层嵌套", new QueryWrapper<User>()
88+
logSqlSegment("嵌套,多层嵌套", new QueryWrapper<User>()
8989
.and(i -> i.eq("id", 1).and(j -> j.eq("id", 1))));
9090

91-
logSqlSegment("嵌套测试,第一个套外的 or 自动消除", new QueryWrapper<User>()
91+
logSqlSegment("嵌套,第一个套外的 or 自动消除", new QueryWrapper<User>()
9292
.or(i -> i.eq("id", 1)).eq("id", 1));
9393

94-
logSqlSegment("嵌套测试,套内外自动拼接 and", new QueryWrapper<User>()
94+
logSqlSegment("嵌套,套内外自动拼接 and", new QueryWrapper<User>()
9595
.eq("id", 11).and(i -> i.eq("id", 1)).eq("id", 1));
9696

97-
logSqlSegment("嵌套测试,套内外手动拼接 or,去除套内第一个 or", new QueryWrapper<User>()
97+
logSqlSegment("嵌套,套内外手动拼接 or,去除套内第一个 or", new QueryWrapper<User>()
9898
.eq("id", 11).or(i -> i.or().eq("id", 1)).or().eq("id", 1));
9999

100100
logSqlSegment("多个 order by 和 group by 拼接,自动优化顺序,last方法拼接在最后", new QueryWrapper<User>()
@@ -108,6 +108,9 @@ public void testQueryWrapper() {
108108

109109
logSqlSegment("只存在 group by", new QueryWrapper<User>()
110110
.groupBy("id", "name", "sex").groupBy("id", "name"));
111+
112+
logSqlSegment("not... 自动拼接 and,手动拼接 or", new QueryWrapper<User>()
113+
.notBetween("id", 1, 6).or().notIn("id", "1,2,3,4,5,6"));
111114
}
112115

113116
// public void test() {

0 commit comments

Comments
 (0)