1. 程式人生 > >JPA原生SQL(自定義SQL)分頁查詢邏輯

JPA原生SQL(自定義SQL)分頁查詢邏輯

JPA自己寫SQL查詢的話,分頁還稍微麻煩,經過測試下面方法可以:

@Query(nativeQuery = true, value = "select * from goods where " +
            " codeName like CONCAT('%',?1,'%') and order by  ?w#{#pageable}")
    Page<Goods> findByName(String name, Pageable pageable);

pageable物件裡定義排序的欄位:

Pageable pageable = new PageRequest(pageNumber, pageSize,Sort.Direction.DESC, "id");