反射2-spring boot jpa 注入model即实现查询

spring boot jpa 
使用方法:
将对应的model类注入即可
//
fixed parameter type private Specification<TargetModel> findByConditionSpecification(TargetModel formalParameter) { return new Specification<TargetModel>() { // fixed parameter type @Override public Predicate toPredicate(Root<TargetModel> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { List<Predicate> predicates = new ArrayList<Predicate>(); // fixed parameter type Class clsEmployeeBase = TargetServiceImpl.class; Method[] methods = clsEmployeeBase.getDeclaredMethods(); try { for (Method method : methods) { if (method.getName().equals("findByConditionSpecification")) { Class clsEmployee = formalParameter.getClass(); Field[] fields = clsEmployee.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (!"serialVersionUID".equals(field.getName()) && field.get(formalParameter) != null) { switch (field.getName()) { case "id": { // TODO fixed parameter,formal patameter primary key TargetIdentity id = (TargetIdentity) field.get(formalParameter); Field[] idFields = id.getClass().getDeclaredFields(); for (Field idField : idFields) { idField.setAccessible(true); if (!"serialVersionUID".equals(idField.getName()) && idField.get(id) != null) { String paramType = String.valueOf(idField.getType()).split(" ")[1]; switch (paramType) { case "java.lang.String": { String[] strs = String.valueOf(idField.get(id)).split(","); Expression<String> express = root.get("id").get(idField.getName()); CriteriaBuilder.In<String> in = criteriaBuilder.in(express); Arrays.asList(strs).forEach(n -> { in.value(n); }); predicates.add(in); break; } case "java.util.Date": { if (idField.getName().contains("start")) { Expression<Date> express = root.get("id") .get(idField.getName()); predicates.add(criteriaBuilder.greaterThanOrEqualTo(express, (Date) (idField.get(id)))); break; } if (idField.getName().contains("end")) { Expression<Date> express = root.get("id") .get(idField.getName()); predicates.add(criteriaBuilder.lessThanOrEqualTo(express, (Date) (idField.get(id)))); break; } } default: { Expression express = root.get("id").get(idField.getName()); predicates.add(criteriaBuilder.equal(express, idField.get(id))); break; } } } } break; } // 非联合主键 default: { String paramType = String.valueOf(field.getType()).split(" ")[1]; switch (paramType) { case "java.lang.String": { String[] strs = String.valueOf(field.get(formalParameter)).split(","); Expression<String> express = root.get(field.getName()); CriteriaBuilder.In<String> in = criteriaBuilder.in(express); Arrays.asList(strs).forEach(n -> { in.value(n); }); predicates.add(in); break; } case "java.util.Date": { if (field.getName().contains("start")) { Expression<Date> express = root.get(field.getName()); predicates.add(criteriaBuilder.greaterThanOrEqualTo(express, (Date) (field.get(formalParameter)))); break; } if (field.getName().contains("end")) { Expression<Date> express = root.get(field.getName()); predicates.add(criteriaBuilder.lessThanOrEqualTo(express, (Date) (field.get(formalParameter)))); break; } } default: { Expression express = root.get(field.getName()); predicates.add(criteriaBuilder.equal(express, field.get(formalParameter))); break; } } break; } } } } } } } catch (Exception e) { // TODO: handle exception } query.where(predicates.toArray(new Predicate[predicates.size()])); return query.getRestriction(); } }; }

 

posted @ 2019-11-21 15:53  小傻孩丶儿  阅读(477)  评论(0编辑  收藏  举报