• 反射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(); } }; }
  • 相关阅读:
    p1012拼数题解
    LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
    UVA 11827 Maximum GCD
    LightOJ1336 Sigma Function(约数和为偶数的个数)
    LightOJ 1197 Help Hanzo(区间素数筛选)
    LightOJ 1236
    BZOJ3339 Rmq Problem
    COJ983 WZJ的数据结构(负十七)
    LCA的五种解法
    hdu4223(dp)
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/11867904.html
Copyright © 2020-2023  润新知