JPA复杂查询(Specification)之排序&反射获取类属性及属性数据类型
JPA复杂查询(Specification)之排序
// JPA复杂查询排序
private static Specification<Order> getOrderSpecLast3Month(String cifNo) {
return (root, query, cb) -> {
Calendar start = Calendar.getInstance();
start.add(Calendar.MONTH, -3);
String startDate = DateUtil.formatTime(start.getTime()).substring(0, 10);
List<Predicate> predicates = new ArrayList<>();
if (!StringUtils.isEmpty(cifNo)) {
predicates.add(cb.equal(root.get("cifNo").as(String.class), cifNo));
}
predicates.add(cb.greaterThanOrEqualTo(root.get("createDate").as(Date.class), cn.hutool.core.date.DateUtil.parse(startDate, "yyyy-MM-dd")));
Predicate[] p = new Predicate[predicates.size()];
query.where(cb.and(predicates.toArray(p)));
query.orderBy(cb.desc(root.get("createDate")));
return query.getRestriction();
};
}
获取类属性名称及数据类型
private void printProperty(Object o){
Class<?> c = o.getClass();
Field[] fields = c.getDeclaredFields();
System.err.println(fields.length);
for (Field field: fields) {
String name = field.getName();
Class<?> type = field.getType();
System.err.println("字段类型:" + type.getSimpleName() + ",字段名称:" + name);
}
}
List Stream收集字段信息并换行
String dealing = dealings.stream().map( o -> o.getOrderNo()).collect(Collectors.joining("<br/>"));
EmailUtil.sendSimpleMail("批量汇款查询结果,訂單號:<br/> " + dealing, "订单银联尚在处理中, 共" + dealings.size() + "个!");