Jpa 分页处理(四)

Model:不用处理

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

public interface NewsRepository extends JpaRepository {

    //public List findBynativeId(String nativeID);

    public Page findBynativeId(String nativeID, Pageable pageable);

}

Service:返回一个Page Content


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

    public List findBynativeId(String nativeId, int page) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, 4, sort);
        Page 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 result = newsService.findBynativeId("0",page);

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

       
    }
}

你可能感兴趣的:(Jpa 分页处理(四))