美文网首页
Jpa 分页处理(四)

Jpa 分页处理(四)

作者: L柠_檬 | 来源:发表于2017-09-21 11:08 被阅读38次

Model:不用处理

Dao:返回的一个Page,不再是List,根据Pageable查找。

public interface NewsRepository extends JpaRepository<NewsModel,Integer> {

    //public List<NewsModel> findBynativeId(String nativeID);

    public Page<NewsModel> findBynativeId(String nativeID, Pageable pageable);

}

Service:返回一个Page Content


@Service
public class NewsService {
    @Autowired
    private NewsRepository newsRespository;

    public List<NewsModel> findBynativeId(String nativeId, int page) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, 4, sort);
        Page<NewsModel> result = newsRespository.findBynativeId(nativeId, pageable);
        return result.getContent();
    }

}

Controller:findBynativeId 传入page

@RestController
public class NewsController {
//@RequestParam("name") String name

    @Autowired
    private NewsService newsService;
    //select * from a limit 0,10;select * from a limit 10,20
    @RequestMapping(value = "/news")

        public ResponseEntity news(@RequestParam("page") int page,@RequestParam("type") String type){
        
        List<NewsModel> result = newsService.findBynativeId("0",page);

        return new ResponseEntity(new BaseModel(0,"success",result),HttpStatus.OK);

       
    }
}

相关文章

网友评论

      本文标题:Jpa 分页处理(四)

      本文链接:https://www.haomeiwen.com/subject/nxdzsxtx.html