• 反射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(); } }; }
  • 相关阅读:
    CSU
    ACM-ICPC Beijing Online A The Book List
    约瑟夫环问题
    HDOJ-1124 Factorial 数论
    玲珑杯 ACM热身赛 #2.5 A 记忆化搜索+瞎搞
    CF 711B
    hdu-4289 最大流Dinic模板题
    最大流朴素算法
    HDU-3729 二分匹配 匈牙利算法
    二分图匹配-匈牙利算法
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/11867904.html
Copyright © 2020-2023  润新知