Spring Boot中的Pageable

huangapple go评论76阅读模式
英文:

Pageable in Spring boot

问题

我想要将"ownDemand"作为需求的页面返回。我应该怎么做

@Override
public Page<Demand> getDemandbyId(Long id, Pageable pageable) {
    Iterable<Demand> alldemand = demandRepository.findAll();
    List<Demand> ownDemand = new ArrayList<Demand>();
    for (Demand demand : alldemand) {
        if (demand.getStore().getId() == id) {
            ownDemand.add(demand);
        }
    }
    return null;
}

需求仓库

@RestResource
public interface DemandRepository extends JpaRepository<Demand, Long> {
}
英文:

I want to return "ownDemand" as Page of demand. How can I do that

@Override
public Page&lt;Demand&gt; getDemandbyId(Long id, Pageable pageable) {
   Iterable&lt;Demand&gt; alldemand = demandRepository.findAll(); 
   List&lt;Demand&gt; ownDemand = new ArrayList&lt;Demand&gt;();
   for (Demand demand : alldemand) {
	   if(demand.getStore().getId()==id) {
		   ownDemand.add(demand);
	   }
  }
	return null;
 }
 }

Demand Repository

@RestResource
public interface DemandRepository extends JpaRepository&lt;Demand,Long&gt; {
}

答案1

得分: 0

为什么不像这样为您的DemandRepository添加一个方法:

Page&lt;Demand&gt; findAllByStore(Store store, Pageable page)

当然,这假设StoreEntity与Demand相关联。

英文:

Why not add a method to your DemandRepository like so

Page&lt;Demand&gt; findAllByStore(Store store, Pageable page)

This assumes that StoreEntity is related to Demand, of course.

答案2

得分: -1

返回的翻译部分如下:

根据签名,它返回 Page。根据您的需要更改 PageRequest。

英文:

Try below:

demandRepository.findAll(PageRequest.of(0, 2))

By signature it returns Page<Demand>. Change PageRequest according to your fit.

huangapple
  • 本文由 发表于 2020年8月10日 04:12:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63330874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定