当前位置: 首页 > 知识库问答 >
问题:

如何以页面的形式获取JPA自定义查询的结果

于鸿博
2023-03-14

我有一个存储库,它返回一个页面

public interface MindRepository extends PagingAndSortingRepository<Mind, Integer> {
    Page<Mind> findByCountry(String country, Pageable pageable);
}

以及使用它的控制器:

private MindRepository mindRepository;

@GetMapping(path = "/minds", produces = "application/json")
public Page<Mind> getMinds(String country, Integer page, Integer size) {
    Pageable pageable = PageRequest.of(page,size);
    return mindRepository.findByCountry(country,pageable);        
}

一切都很好。控制器返回页面

但是现在我必须使查询更加复杂,有几个过滤器,动态变化。我想像这样使用createQuery

public interface CustomizedMindRepository<T> {
    Page<T> findByCountry(String country, Pageable pageable);
}

public interface MindRepository extends PagingAndSortingRepository<Mind, Integer>,CustomizedMindRepository {
    Page<Mind> findByCountry(String country, Pageable pageable);
}

public class CustomizedMindRepositoryImpl implements CustomizedMindRepository {
    @PersistenceContext
    private EntityManager em;

    @Override
    public Page<Mind> findByCountry(String country, Pageable pageable) {
        return em.createQuery("from minds where <dynamical filter> AND <another dynamical filter> AND <...etc>", Mind.class)
                .getResultList();
    }
}

但是getResultList()返回列表,而不是页面:(

解决这个问题的最好方法是什么?


共有2个答案

孔海超
2023-03-14

如果要使用EntityManager。createQuery,您可以使用setFirstResultsetMaxResults方法来获得相同的结果。

@Override
public List<Mind> findByCountry(String country, Pageable pageable) {
  return em.createQuery("from minds where <dynamical filter> AND <another dynamical filter> AND <...etc>", Mind.class)
           .setFirstResult(startPosition)
           .setMaxResults(size)
           .getResultList();
}

在本例中,大小的含义与您的情况相同,但起始位置不是一个页面,而是计算为:

startPosition = page * size

但是,如果您需要建立DyMic查询-考虑使用规范或JPA标准API。

童铭晨
2023-03-14
  • 页面存储库调用执行两个调用,一个用于获取结果,另一个用于获取总大小。因此,您必须通过执行计数查询来复制这种行为
    @Override
    public Page<Mind> findByCountry(String country, Pageable pageable) {
        long offset = pageable.getPageNumber() * pageable.getPageSize();
        long limit = pageable.getPageSize();
        
        List<Item> itemsInNextPage = em.createQuery(query)
                .setFirstResult(offset)
                .setMaxResults(limit)
                .getResultList();
        
        long total = // Another query to get the total count
                
        List<Mind>  results = em.createQuery("from minds ...", Mind.class)
                             .getResultList();
        return new PageImpl(results, pageable, total); 
                            
    }
 类似资料:
  • 我想在不使用元组的情况下,从这个查询中得到DTO的结果。 @Repository公共界面CustomMapRepository扩展了JpaRepository{ } 公共类CustomMapDTO实现可序列化{ //..接受者和接受者}

  • 我想从JSON列中获取值,并在spring JPA中返回自定义DTO。 表结构 列包含年龄,例如 我想获取具有、和的用户列表。由于数据量可能很大,我创建了一个自定义DTO 下面是一个同样的例子: 实体: 结构: 结构: 启动Spring启动应用程序 警告|上下文初始化期间遇到异常-取消刷新尝试:org。springframework。豆。工厂UnsatifiedPendencyException:

  • 我的存储库如下所示: 如何将分页添加到不使用JPA默认“findAll”查询的自定义查询? 如何让Spring将其转换为页面或可分页对象? 因此,在我的AppController中,我可以这样使用它: Page=heroService。findAllHeroByGroupName(pageNum); 请注意,返回一个列表,而不是页面对象。

  • 我需要创建一个jpa自定义查询,使用几个表上的联接来获取记录。 以下是我想要达到的目标: 对很少的参数进行数据排序(在运行时决定) 使用where子句进行筛选(在运行时决定) 示例: @query(value=“从用户a中选择a.name,b.city,c.reason在a.id=b.id上连接地址b在a.id=c.id上连接测试c 我无法为其创建常规查询。 任何其他的方法对我来说也是可以接受的来

  • 我尝试过用Spring Boot实现JPA存储库,它工作得很好。现在,如果我尝试在使用@query注释扩展JpaRepository的接口中实现自定义查询,它可以很好地返回bean列表(使用NamedQuery)。现在,当我尝试为自定义方法/查询使用分页时,它不起作用。 代码: 控制器: 服务 异常:java.lang.IllegalArgumentException:为TypedQuery[ja

  • 我有一个实体 及其存储库 注意:如您所见,intValue不是entity类的成员变量。