JPA生成全属性条件查询通用Specification

1、使用Specification+JPA不作封装的时候每次都要挨个判断属性,很是麻烦

以下对生成Specification做一个封装

/**
     * 生成全属性条件查询通用Specification
     *
     * @param tableMap    属性参数
     * @param clazz       要查询的实体类或vo类
     * @param excludeAttr 不使用模糊搜索的字符串属性
     * @param map         外键关联查询
     * @param <S>
     * @return
     */
    public <S> Specification<S> createSpecification(Map<String, String> tableMap, Class clazz, List<String> excludeAttr, Map map) {
        Specification<S> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

            //未删除的数据
            try {
                clazz.getDeclaredField(Constants.VALID);
                if (!StringUtils.isEmpty(tableMap.get(Constants.VALID))) {
                    predicates.add(cb.equal(root.get(Constants.VALID), Integer.valueOf(tableMap.get(Constants.VALID))));
                } else {
                    predicates.add(cb.equal(root.get(Constants.VALID), 1));
                }
            } catch (NoSuchFieldException e) {
                logger.warn("没有找到 {valid} 属性");
            }

            Field[] declaredFields = clazz.getDeclaredFields();

            if (tableMap != null) {
                for (Field field : declaredFields) {
                    String fieldName = field.getName();
                    if (!StringUtils.isEmpty(tableMap.get(fieldName))) {
                        String typeName = field.getGenericType().getTypeName();
                        Class<?> aClass = null;
                        try {
                            aClass = Class.forName(typeName);
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                        //属性不包含特定的属性并且是字符串采用模糊搜索
                        if (aClass == String.class && (CollectionUtils.isEmpty(excludeAttr) || !excludeAttr.contains(fieldName))) {
                            String queryFieldName = "%" + tableMap.get(fieldName).replace("/", "\\/")
                                    .replaceAll("_", "\\\\_").replaceAll("%", "\\\\%") + "%";
                            predicates.add(cb.like(root.get(fieldName), queryFieldName));
                        } else {
                            predicates.add(cb.equal(root.get(fieldName), tableMap.get(fieldName)));
                        }
                    }
                }
                if (!CollectionUtils.isEmpty(map)) {
                    Iterator iterator = map.keySet().iterator();
                    while (iterator.hasNext()) {
                        String sourceKey = iterator.next().toString();
                        Map mapping = (Map) map.get(sourceKey);
                        Join join = root.join(sourceKey, JoinType.INNER);
                        Iterator mappingItr = mapping.keySet().iterator();
                        while (mappingItr.hasNext()) {
                            String joinKey = mappingItr.next().toString();
                            String joinAttr = mapping.get(joinKey).toString();
                            if (!StringUtils.isEmpty(tableMap.get(joinKey))) {
                                predicates.add(cb.equal(join.get(joinAttr), tableMap.get(joinKey)));
                            }
                        }
                    }
                }
            }
            Predicate[] pre = new Predicate[predicates.size()];
            Predicate preAnd = cb.and(predicates.toArray(pre));
            return query.where(preAnd).getRestriction();
        };
        return specification;
    }

2、使用方式

Map<String, Map<String, String>> joinAttr = new HashMap(4);
        //mapping为请求参数与实体属性名之间的映射关系
        Map mapping = new HashMap(4);
        mapping.put("tankAreaId", "id");
        mapping.put("tankAreaCode", "tankAreaCode");
         //joinAttr为当前实体关联字段名称
        joinAttr.put("tankArea", mapping);
        Specification<TankMovementEntity> specification = reflectUtil.createSpecification(tableMap, TankMovementVo.class, null, createJoinField());

3、很方便,不需要再一个一个去判断

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Spring Data JPA进行多条件组合条件查询时,可以通过使用Specification来实现。Specification是一个接口,我们可以自定义一个实现该接口的类,然后在查询方法中传入该Specification对象来指定查询条件。 首先,我们需要在仓库接口中定义一个方法,该方法接收一个Specification参数,并返回查询结果。如下所示: ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { List<User> findAll(Specification<User> spec); } ``` 接下来,我们在Specification实现类中重写toPredicate方法,在该方法中使用CriteriaBuilder构建查询条件,并返回一个Predicate对象表示查询条件。例如,我们可以按用户名和年龄进行查询,如下所示: ```java public class UserSpecification implements Specification<User> { private String username; private int age; public UserSpecification(String username, int age) { this.username = username; this.age = age; } @Override public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { List<Predicate> predicates = new ArrayList<>(); if (username != null) { predicates.add(criteriaBuilder.equal(root.get("username"), username)); } if (age != 0) { predicates.add(criteriaBuilder.equal(root.get("age"), age)); } return criteriaBuilder.and(predicates.toArray(new Predicate[0])); } } ``` 最后,我们可以在服务类中调用仓库方法并传入自定义的Specification对象来进行查询。例如: ```java @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> getUsers(String username, int age) { UserSpecification spec = new UserSpecification(username, age); return userRepository.findAll(spec); } } ``` 这样,我们就可以根据传入的条件来进行组合条件查询了。当传入的条件为空时,不会加入到查询中。当传入的条件有值时,则会根据该条件进行查询。这样,就实现了多条件组合条件查询

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值