在查询DSL中返回页面<object>

时间:2016-03-15 17:37:47

标签: spring querydsl

我正在使用Query DSL并希望我的结果集返回一个Page Object。在Query DSL中有没有办法做到这一点?如果是这样我的查询是什么样的?  我正在使用JPAQuery,我有我的QClasses

方法结构就是这个

public Page<Object> searchPerson(String name,String phone){
 Page<Object> results=null;
 JPQLQuery query = new JPAQuery(entityManager);
 QPerson person = QPerson.person;
   //I am assuming my query would go here 
  results = query.from(person). ?????  


return results;} 

帮助!

2 个答案:

答案 0 :(得分:2)

以下是我使用QueryDSL的Paging实现。 PageRequest定义查询的参数(限制和页面):

public class PageRequest {
    protected Long page = 1l;// 1 is the first page
    protected Integer limit = 10;

    public PageRequest(Long page, Integer limit) {
        this.limit = limit;
        this.page = page;   
    }

    public Long getPage() {
        return page;
    }

    public Integer getLimit() {
        return limit;
    }

    public Long getOffset() {
        return (page - 1l) * limit;
    }
}

Page类包含查询的结果(此处为属性objects),并且可以实现创建好的分页链接的方法。

public class Page<T> extends PageRequest {

    protected Collection<T> objects;
    private Long totalCount;
    private Long pageCount;
    private Boolean hasPageLinkPrev;
    private Boolean hasPageLinkNext;
    private Collection<Long> pageLinks;

    public Page(Long page, Integer limit, Long totalCount, Collection<T> objects) {

        this.page = page;
        this.limit = limit;
        this.totalCount = totalCount;
        this.objects = objects;

        this.pageCount = totalCount / limit;
        if (totalCount % limit > 0) {
            this.pageCount = this.pageCount + 1;
        }

        this.hasPageLinkPrev = page > 1;
        this.hasPageLinkNext = page < this.pageCount;

        this.pageLinks = new ArrayList<>();
        if (this.pageCount != 1) {
            this.pageLinks.add(1l);
            if (page > 3l) {
                this.pageLinks.add(-1l);
            }
            if (page > 2l) {
                if (page.equals(this.pageCount) && this.pageCount > 3l) {
                    this.pageLinks.add(page - 2l);
                }
                this.pageLinks.add(page - 1l);
            }
            if (page != 1l && !page.equals(this.pageCount)) {
                this.pageLinks.add(page);
            }
            if (page < this.pageCount - 1l) {
                this.pageLinks.add(page + 1l);

                if (page == 1l && this.pageCount > 3l) {
                    this.pageLinks.add(page + 2l);
                }
            }
            if (page < this.pageCount - 2l) {
                this.pageLinks.add(-1l);
            }
            this.pageLinks.add(this.pageCount);
        }
    }

    public Page(PageRequest pageRequest, Long totalCount, Collection<T> objects) {
        this(pageRequest.getPage(), pageRequest.getLimit(), totalCount, objects);
    }

    public Long getTotalCount() {
        return this.totalCount;
    }

    public Long getPageCount() {
        return this.pageCount;
    }

    public Long getPage() {
        return this.page;
    }

    public Integer getLimit() {
        return this.limit;
    }

    public Boolean getHasPageLinkPrev() {
        return this.hasPageLinkPrev;
    }

    public Boolean getHasPageLinkNext() {
        return hasPageLinkNext;
    }

    public Collection<Long> getPageLinks() {
        return pageLinks;
    }

    public Collection<T> getObjects() {
        return objects;
    }
}

使用该stuf创建查询并将结果放入页面对象并不是很困难。一种可能性是在存储库类的基类中编写泛型方法:

    protected <T> Page<T> getPage(JPQLQuery<T> query, PageRequest pageRequest) {

        List<T> resultList = query
            .offset(pageRequest.getOffset())
            .limit(pageRequest.getLimit())
            .fetch();

        Long totalCount = query.fetchCount();
        return new Page<T>(pageRequest, totalCount, resultList);
    }

在您的存储库类中,您可以为特定用例创建查询。然后,您可以使用getPage方法将结果生成为Page

public Page<Person> searchPerson(String name,
                                 String phone,
                                 PageRequest request){
  Page<Person> results=null;
  JPQLQuery<Person> query = new JPAQuery<>(entityManager);
  QPerson person = QPerson.person;
  query = query.from(person)
     .where(person.name.eq(name)
         .and(person.phone.eq(phone)));
  return getPage(query, request);
} 

答案 1 :(得分:0)

上面的解决方案是使用在上面的方法上实现的BooleanBuilder并更改方法名称以返回Person Object。 请检查BooleanBuilder

QPerson person= QPerson.person;
BooleanBuilder builder = this.getBuilder(name, phone,page, pageSize, sortFlag, sortItem);
    PageRequest pg = getPRequest(page, pageSize);
    Page<Person> pages personRepo.findAll(builder,pg);
  return pages;

然后实现getBuilder方法,它是下面的

public BooleanBuilder getBuilder(String name, String phone, Integer page, Integer pageSize, String sortFlag, String sortItem) {

    QPerson person = QPerson.person;
    BooleanBuilder builder = new BooleanBuilder();
    builder.and(person.name.startsWith(name));
    return builder;
}

最后将getPRequest方法实现为以下

public PageRequest getPRequest(Integer page, Integer pageSize) {
    return new PageRequest(page, pageSize);
}

Oooooh Happy Days!