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<Production> specification = new Specification() {
@SneakyThrows
@Override
public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
Map<String,String> conditions = BeanUtils.describe(production);
for (int i = 0; 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;
}
}
|