springdatajpa条件查询

首先当然是在controller中增加一个方法:

@RequestMapping(value = "/search",method = RequestMethod.POST)
    public Result findSearch(@RequestBody Label label){
        List

这样,我们通过访问localhost:9001/label/search就可以查询啦,然后是实现labelService中的findSearch:

public List

可以直接使用labelDao.findAll,其中new Specification()就是根据条件去查询,toPredicate方法中有3个参数,root根对象,也就是要把条件封装到哪个对象中。where 类名 = label.getid,query封装的都是查询关键字,比如group by order by 等cb用来封装条件对象的,如果直接返回null,表示不需要任何条件。

 Predicate predicate = cb.like(root.get("labelname").as(String.class), "%" + label.getLabelname()+"%");

这句代码就相当于sql语句where labelname like “%“小明”%”,注意,"%"需自己拼接。

Predicate predicate = cb.equal(root.get("state").as(String.class),  label.getState());

这句代码相当于where state = 1"
记得把他们添加到list列表中,最后return cd.and(parr)。

你可能感兴趣的:(springdatajpa条件查询)