Skip to content

Commit 62ad39f

Browse files
ydqabel533
authored andcommitted
example / wrapper 新增字符串形式的排序方法重载
1 parent b1b3e68 commit 62ad39f

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

mapper/src/main/java/io/mybatis/mapper/example/Example.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import io.mybatis.mapper.fn.Fn;
2020
import io.mybatis.provider.EntityColumn;
2121

22-
import java.util.ArrayList;
23-
import java.util.Arrays;
24-
import java.util.Collection;
25-
import java.util.List;
22+
import java.util.*;
23+
import java.util.function.Supplier;
2624
import java.util.regex.Matcher;
25+
import java.util.stream.Collectors;
26+
import java.util.stream.Stream;
2727

2828
import static io.mybatis.provider.EntityTable.DELIMITER;
2929

@@ -272,6 +272,42 @@ public Example<T> orderBy(Fn<T, Object> fn, Order order) {
272272
return this;
273273
}
274274

275+
/**
276+
* 用于一些非常规的排序 或 简单的字符串形式的排序 <br>
277+
* 本方法 和 example.setOrderByClause 方法的区别是 <strong>本方法不会覆盖已有的排序内容</strong> <br>
278+
* eg: ORDER BY status = 5 DESC 即将 status = 5 的放在最前面<br>
279+
* 此时入参为:<pre><code>example.orderBy("status = 5 DESC")</code></pre>
280+
* @param orderByCondition 字符串排序表达式
281+
* @return Example
282+
*/
283+
public Example<T> orderBy(String orderByCondition) {
284+
if (orderByCondition != null && orderByCondition.length() > 0) {
285+
if (orderByClause == null) {
286+
orderByClause = "";
287+
} else {
288+
orderByClause += ", ";
289+
}
290+
orderByClause += orderByCondition;
291+
}
292+
return this;
293+
}
294+
295+
296+
/**
297+
* 用于一些特殊的非常规的排序,排序字符串需要通过一些函数或者方法来构造出来<br>
298+
* eg: ORDER BY FIELD(id,3,1,2) 即将 id 按照 3,1,2 的顺序排序<br>
299+
* 此时入参为:<pre><code>example.orderBy(()-> {
300+
* return Stream.of(3,1,2)
301+
* .map(Objects::toString)
302+
* .collect(Collectors.joining("," , "FIELD( id ," , ")"));
303+
* })</code></pre>
304+
* @param orderByCondition 字符串排序表达式
305+
* @return Example
306+
*/
307+
public Example<T> orderBy(Supplier<String> orderByCondition) {
308+
return orderBy(orderByCondition.get());
309+
}
310+
275311
/**
276312
* 通过方法引用方式设置排序字段,升序排序
277313
*

mapper/src/main/java/io/mybatis/mapper/example/ExampleWrapper.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Optional;
1313
import java.util.function.Function;
14+
import java.util.function.Supplier;
1415
import java.util.stream.Collectors;
1516
import java.util.stream.Stream;
1617

@@ -92,6 +93,36 @@ public ExampleWrapper<T, I> orderBy(Fn<T, Object> fn, Example.Order order) {
9293
return this;
9394
}
9495

96+
/**
97+
* 支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案
98+
* @param orderByCondition 排序字符串(不会覆盖已有的排序内容)
99+
* @return Example
100+
*/
101+
public ExampleWrapper<T, I> orderBy(String orderByCondition) {
102+
this.example.orderBy(orderByCondition);
103+
return this;
104+
}
105+
106+
/**
107+
* 支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案
108+
* @param orderByCondition 排序字符串构造方法,比如通过数组集合循环拼接等
109+
* @return Example
110+
*/
111+
public ExampleWrapper<T, I> orderBy(Supplier<String> orderByCondition) {
112+
this.example.orderBy(orderByCondition);
113+
return this;
114+
}
115+
116+
/**
117+
* 支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案
118+
* @param useOrderBy 条件表达式,true使用,false不使用 字符串排序
119+
* @param orderByCondition 排序字符串构造方法,比如通过数组集合循环拼接等
120+
* @return Example
121+
*/
122+
public ExampleWrapper<T, I> orderBy(boolean useOrderBy, Supplier<String> orderByCondition) {
123+
return useOrderBy ? this.orderBy(orderByCondition) : this;
124+
}
125+
95126
/**
96127
* 通过方法引用方式设置排序字段,升序排序
97128
*

0 commit comments

Comments
 (0)