Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example / wrapper 新增字符串形式的排序方法重载 #34

Merged
merged 1 commit into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions mapper/src/main/java/io/mybatis/mapper/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import io.mybatis.mapper.fn.Fn;
import io.mybatis.provider.EntityColumn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.*;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

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

/**
* 用于一些非常规的排序 或 简单的字符串形式的排序 <br>
* 本方法 和 example.setOrderByClause 方法的区别是 <strong>本方法不会覆盖已有的排序内容</strong> <br>
* eg: ORDER BY status = 5 DESC 即将 status = 5 的放在最前面<br>
* 此时入参为:<pre><code>example.orderBy("status = 5 DESC")</code></pre>
* @param orderByCondition 字符串排序表达式
* @return Example
*/
public Example<T> orderBy(String orderByCondition) {
if (orderByCondition != null && orderByCondition.length() > 0) {
if (orderByClause == null) {
orderByClause = "";
} else {
orderByClause += ", ";
}
orderByClause += orderByCondition;
}
return this;
}


/**
* 用于一些特殊的非常规的排序,排序字符串需要通过一些函数或者方法来构造出来<br>
* eg: ORDER BY FIELD(id,3,1,2) 即将 id 按照 3,1,2 的顺序排序<br>
* 此时入参为:<pre><code>example.orderBy(()-> {
* return Stream.of(3,1,2)
* .map(Objects::toString)
* .collect(Collectors.joining("," , "FIELD( id ," , ")"));
* })</code></pre>
* @param orderByCondition 字符串排序表达式
* @return Example
*/
public Example<T> orderBy(Supplier<String> orderByCondition) {
return orderBy(orderByCondition.get());
}

/**
* 通过方法引用方式设置排序字段,升序排序
*
Expand Down
31 changes: 31 additions & 0 deletions mapper/src/main/java/io/mybatis/mapper/example/ExampleWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

/**
* 支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案
* @param orderByCondition 排序字符串(不会覆盖已有的排序内容)
* @return Example
*/
public ExampleWrapper<T, I> orderBy(String orderByCondition) {
this.example.orderBy(orderByCondition);
return this;
}

/**
* 支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案
* @param orderByCondition 排序字符串构造方法,比如通过数组集合循环拼接等
* @return Example
*/
public ExampleWrapper<T, I> orderBy(Supplier<String> orderByCondition) {
this.example.orderBy(orderByCondition);
return this;
}

/**
* 支持使用字符串形式来设置 order by,用以支持一些特殊的排序方案
* @param useOrderBy 条件表达式,true使用,false不使用 字符串排序
* @param orderByCondition 排序字符串构造方法,比如通过数组集合循环拼接等
* @return Example
*/
public ExampleWrapper<T, I> orderBy(boolean useOrderBy, Supplier<String> orderByCondition) {
return useOrderBy ? this.orderBy(orderByCondition) : this;
}

/**
* 通过方法引用方式设置排序字段,升序排序
*
Expand Down