Jpa 페이지 나누기 (4)

1474 단어
모델:처리 안 함
Dao: 더 이상 List가 아닌 Pageable에서 찾을 수 있는 페이지가 반환됩니다.
public interface NewsRepository extends JpaRepository {

    //public List findBynativeId(String nativeID);

    public Page findBynativeId(String nativeID, Pageable pageable);

}

서비스: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 전송 페이지
@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);

       
    }
}

좋은 웹페이지 즐겨찾기