|
@@ -15,7 +15,6 @@ import org.springframework.data.jpa.domain.Specifications;
|
|
|
|
|
|
import javax.persistence.criteria.Path;
|
|
import javax.persistence.criteria.Path;
|
|
import javax.persistence.criteria.Predicate;
|
|
import javax.persistence.criteria.Predicate;
|
|
-import javax.validation.constraints.NotNull;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
@@ -26,10 +25,12 @@ import java.util.List;
|
|
* @since: 2018/10/17
|
|
* @since: 2018/10/17
|
|
*/
|
|
*/
|
|
public class SpecUtils {
|
|
public class SpecUtils {
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 默认当前页数
|
|
* 默认当前页数
|
|
*/
|
|
*/
|
|
public static final int DEFAULT_PAGE_NO = 1;
|
|
public static final int DEFAULT_PAGE_NO = 1;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 默认每页条数
|
|
* 默认每页条数
|
|
*/
|
|
*/
|
|
@@ -43,7 +44,7 @@ public class SpecUtils {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static Pageable buildPageable(Integer pageNo, Integer pageSize) {
|
|
public static Pageable buildPageable(Integer pageNo, Integer pageSize) {
|
|
- return buildPageable(pageNo, pageSize, null);
|
|
|
|
|
|
+ return buildPageable(pageNo, pageSize, Sort.unsorted());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -58,10 +59,12 @@ public class SpecUtils {
|
|
if (pageNo == null || pageNo < 1) {
|
|
if (pageNo == null || pageNo < 1) {
|
|
pageNo = DEFAULT_PAGE_NO;
|
|
pageNo = DEFAULT_PAGE_NO;
|
|
}
|
|
}
|
|
|
|
+
|
|
if (pageSize == null || pageSize < 1) {
|
|
if (pageSize == null || pageSize < 1) {
|
|
pageSize = DEFAULT_PAGE_SIZE;
|
|
pageSize = DEFAULT_PAGE_SIZE;
|
|
}
|
|
}
|
|
- return new PageRequest(pageNo - 1, pageSize, sort);
|
|
|
|
|
|
+
|
|
|
|
+ return PageRequest.of(pageNo - 1, pageSize, sort);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -89,17 +92,20 @@ public class SpecUtils {
|
|
if (searchers == null || searchers.size() == 0) {
|
|
if (searchers == null || searchers.size() == 0) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
+
|
|
return (root, query, builder) -> {
|
|
return (root, query, builder) -> {
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
List<Predicate> predicates = new ArrayList<>();
|
|
for (SearchBuilder.Searcher filter : searchers) {
|
|
for (SearchBuilder.Searcher filter : searchers) {
|
|
if (filter.getFieldName() == null || "".equals(filter.getFieldName())) {
|
|
if (filter.getFieldName() == null || "".equals(filter.getFieldName())) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
+
|
|
String[] names = filter.getFieldName().split("\\.");
|
|
String[] names = filter.getFieldName().split("\\.");
|
|
Path expression = root.get(names[0]);
|
|
Path expression = root.get(names[0]);
|
|
for (int i = 1; i < names.length; i++) {
|
|
for (int i = 1; i < names.length; i++) {
|
|
expression = expression.get(names[i]);
|
|
expression = expression.get(names[i]);
|
|
}
|
|
}
|
|
|
|
+
|
|
switch (filter.getOp()) {
|
|
switch (filter.getOp()) {
|
|
case EQ:
|
|
case EQ:
|
|
predicates.add(builder.equal(expression, filter.getValue()));
|
|
predicates.add(builder.equal(expression, filter.getValue()));
|
|
@@ -142,6 +148,7 @@ public class SpecUtils {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
if (!predicates.isEmpty()) {
|
|
if (!predicates.isEmpty()) {
|
|
if (isOR) {
|
|
if (isOR) {
|
|
//将所有条件用 or 联合起来
|
|
//将所有条件用 or 联合起来
|
|
@@ -161,10 +168,11 @@ public class SpecUtils {
|
|
* @param specs
|
|
* @param specs
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static Specification andMerge(@NotNull Specification... specs) {
|
|
|
|
|
|
+ public static Specification andMerge(Specification... specs) {
|
|
if (specs == null || specs.length == 0) {
|
|
if (specs == null || specs.length == 0) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
+
|
|
Specifications result = null;
|
|
Specifications result = null;
|
|
for (int i = 0; i < specs.length; i++) {
|
|
for (int i = 0; i < specs.length; i++) {
|
|
if (i == 0) {
|
|
if (i == 0) {
|
|
@@ -173,6 +181,7 @@ public class SpecUtils {
|
|
}
|
|
}
|
|
result = Specifications.where(result).and(specs[i]);
|
|
result = Specifications.where(result).and(specs[i]);
|
|
}
|
|
}
|
|
|
|
+
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -184,10 +193,11 @@ public class SpecUtils {
|
|
* @param specs
|
|
* @param specs
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static Specification orMerge(@NotNull Specification... specs) {
|
|
|
|
|
|
+ public static Specification orMerge(Specification... specs) {
|
|
if (specs == null || specs.length == 0) {
|
|
if (specs == null || specs.length == 0) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
+
|
|
Specifications result = null;
|
|
Specifications result = null;
|
|
for (int i = 0; i < specs.length; i++) {
|
|
for (int i = 0; i < specs.length; i++) {
|
|
if (i == 0) {
|
|
if (i == 0) {
|
|
@@ -196,6 +206,7 @@ public class SpecUtils {
|
|
}
|
|
}
|
|
result = Specifications.where(result).or(specs[i]);
|
|
result = Specifications.where(result).or(specs[i]);
|
|
}
|
|
}
|
|
|
|
+
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|