append querydsl predicate 或基于筛选器值构建 predicate

svmlkihl  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(273)

我是querydsl的新手,正在寻找一种基于过滤器附加/构建 predicate 的方法。您可以在示例中看到,如果筛选器中的某个字段没有值,则不应将其放入 predicate 中。
如果这样的事情是不可能的,那么最好的方法是什么呢?
例如,filter(getpaintingrequest)包含以下字段(filter中有更多字段,但并不重要):

public class GetPaintingsRequest {
    private String author;
    private Integer style;
    private String paintingName;

    //.....getters and setters
}

所以在一个服务类中,我想像这样过滤绘画

public List<Painting> getFilteredPaintings(GetPaintingsRequest request) {

        QPainting painting = QPainting.painting;

        //predicate for all paintings that are not deleted and not sold
        Predicate query = painting.deleted.isFalse().and(painting.isSold.isFalse());

        if (!request.getAuthor().equals("")) {
            //append to existing predicate name of the author
            query.and.painting.autor.eq(request.getAuthor()); //obviously not working - just as an example what I want

        }

        if (!request.getPaintingName().equals("")) {
            //append to existing predicate painting name
            query.and.painting.paintingName.eq(request.getPaintingName()); //obviously not working - just as an example what I want
        }

        var paintingList = paintingRepository.findAll(query);

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题