SpecificationUtil 工具类
/**
* @author :GuangxiZhong
* @date :Created in 2021/7/12 9:28
* @description:
* @modified By:
* @version: 1.0
*/
public class SpecificationUtil {
Map<String, String> conditionMap = new HashMap<>();
public class JpaCondition {
public static final String eq = "eq";
public static final String ne = "ne";
public static final String gt = "gt";
public static final String ge = "ge";
public static final String lt = "lt";
public static final String le = "le";
public static final String alike = "alike";
public static final String llike = "llike";
public static final String rlike = "rlike";
}
public <T> Specification<T> build(Object object) {
Specification<T> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
Class<?> clz = object.getClass();
Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
try {
String fieldName = field.getName();
Method m = object.getClass().getMethod("get" + getMethodName(fieldName));
Object val = m.invoke(object);
if (val != null) {
if (conditionMap == null || conditionMap.size() == 0) {
Predicate predicate = criteriaBuilder.equal(root.get(fieldName), val);
predicates.add(predicate);
} else {
String conditionStr = conditionMap.get(fieldName);
if (StringUtils.isEmpty(conditionStr)) {
Predicate predicate = criteriaBuilder.equal(root.get(fieldName), val);
predicates.add(predicate);
} else {
String type = field.getGenericType().toString();
Predicate predicate;
switch (conditionStr) {
case JpaCondition.eq:
predicates.add(criteriaBuilder.equal(root.get(fieldName), val));
break;
case JpaCondition.ne:
predicates.add(criteriaBuilder.notEqual(root.get(fieldName), val));
break;
case JpaCondition.gt:
// TODO 未调试
if (type.equals("class java.util.Date")) {
Date v = (Date) val;
predicate = criteriaBuilder.gt(root.get(fieldName).as(Long.class), v.getTime());
} else {
predicate = criteriaBuilder.gt(root.get(fieldName).as(BigDecimal.class), new BigDecimal(val.toString()));
}
predicates.add(predicate);
break;
case JpaCondition.ge:
predicate = criteriaBuilder.greaterThanOrEqualTo(root.get(fieldName).as(String.class), type.equals("class java.util.Date") ? DateUtils.DATETIME.format(val) : val.toString());
// if (type.equals("class java.util.Date")) {
// predicate = criteriaBuilder.greaterThanOrEqualTo(root.get(fieldName).as(String.class), DateUtils.DATETIME.format(val));
// }else{
// predicate = criteriaBuilder.greaterThanOrEqualTo(root.get(fieldName).as(String.class), val.toString());
// }
predicates.add(predicate);
break;
case JpaCondition.lt:
// TODO 未调试
String type1 = field.getGenericType().toString();
Predicate predicate1;
if (type1.equals("class java.util.Date")) {
Date v = (Date) val;
predicate1 = criteriaBuilder.lt(root.get(fieldName).as(Long.class), v.getTime());
} else {
predicate1 = criteriaBuilder.lt(root.get(fieldName).as(BigDecimal.class), new BigDecimal(val.toString()));
}
predicates.add(predicate1);
break;
case JpaCondition.le:
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(fieldName).as(String.class), type.equals("class java.util.Date") ? DateUtils.DATETIME.format(val) : val.toString()));
break;
case JpaCondition.alike:
predicates.add(criteriaBuilder.like(root.get(fieldName), SqlUtils.containsLikeValue(val.toString())));
break;
case JpaCondition.llike:
predicates.add(criteriaBuilder.like(root.get(fieldName), SqlUtils.leftLikeValue(val.toString())));
break;
case JpaCondition.rlike:
predicates.add(criteriaBuilder.like(root.get(fieldName), SqlUtils.rightLikeValue(val.toString())));
break;
default:
predicates.add(criteriaBuilder.equal(root.get(fieldName), val));
break;
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
Predicate[] f = new Predicate[]{};
f = predicates.toArray(f);
criteriaQuery.where(f);
return null;
};
return specification;
}
private static String getMethodName(String fildeName) {
byte[] items = fildeName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}
public SpecificationUtil alike(String field) {
conditionMap.put(field, JpaCondition.alike);
return this;
}
public SpecificationUtil llike(String field) {
conditionMap.put(field, JpaCondition.llike);
return this;
}
public SpecificationUtil rlike(String field) {
conditionMap.put(field, JpaCondition.rlike);
return this;
}
public SpecificationUtil eq(String field) {
conditionMap.put(field, JpaCondition.eq);
return this;
}
public SpecificationUtil ne(String field) {
conditionMap.put(field, JpaCondition.ne);
return this;
}
/**
* 大于
*
* @param field
* @return
*/
public SpecificationUtil gt(String field) {
conditionMap.put(field, JpaCondition.gt);
return this;
}
public SpecificationUtil ge(String field) {
conditionMap.put(field, JpaCondition.ge);
return this;
}
/**
* 小于
*
* @param field
* @return
*/
public SpecificationUtil lt(String field) {
conditionMap.put(field, JpaCondition.lt);
return this;
}
public SpecificationUtil le(String field) {
conditionMap.put(field, JpaCondition.le);
return this;
}
}
使用
SpecificationUtil su = new SpecificationUtil();
Specification<InspectionTicket> specification = su.alike("departmentName").alike("initPersonName").alike("employeeName").alike("hospitalName").ge("startTime").le("endTime").build(condition);
inspectionTicketRepository.findAll(specification, p)
service类需要实现JpaSpecificationExecutor
public interface InspectionTicketRepository extends JpaSpecificationExecutor<InspectionTicket> {}