Spring data jpa 使用Specification实现动态查询

Spring data jpa 使用Specification实现动态查询

在开发过程中,存在一个查询方法会收到不同参数的情况,下面这段代码可以用jpa实现动态查询,完全面向对象,在复杂查询方面虽然不及mybatis 灵活,但仍然有其可取之处, 但是我遇到的问题就是,我无法将这样的代码封装起来用于不同的实体类的查询,因为传入的对象无法确定,反射功能无法实现。希望有人能一起探讨一下。

@RestController
@RequestMapping("/productions")
public class ProductionController {
    @Resource
    ProductionRepository productionRepository;
    @GetMapping("{currentPage}/{pageSize}")
    public RestBean<Page<Production>> dymamicEnquire(@RequestBody Production production, @PathVariable int currentPage, @PathVariable int pageSize) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        //通过反射获取实体类的全部属性
        Field[] fields = Production.class.getDeclaredFields();
        //构造分页对象
        Pageable pageable = PageRequest.of(currentPage,pageSize, Sort.Direction.DESC,"id");
        //匿名内部类的方式构造Specification对象,其实可以用Lambda表达式,格式会更简单,但是不便于理解
        Specification<Production> specification = new Specification() {
            @SneakyThrows
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) {
                //新建一个查询条件数组
                List<Predicate> predicates = new ArrayList<>();
                //通过使用Apache的工具类将实体转换成map
                Map<String, Object> conditions = PropertyUtils.describe(production);
                for (int i = 1; i < fields.length; i++) {
                    //设置实体类属性可访问
                    fields[i].setAccessible(true);
                    String name = fields[i].getName();
                    //添加需要的查询条件
                    if (conditions.containsKey(name)&&(!ObjectUtils.isEmpty(conditions.get(name)))){
                        predicates.add(cb.equal(root.get(name), conditions.get(name)));
                    }
                }
                return cq.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
            }
        };
        //查询
        Page<Production>list= productionRepository.findAll(specification,pageable);
        //封装返回对象
        RestBean bean = new RestBean(true,currentPage,pageSize,200,"分页查询成功",list);
        return bean;

    }


  }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值