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);
}
}