这是我代码。
@Query(value = "SELECT * FROM T_PURCHASE_ORDER_ITEM",
countQuery = "SELECT count(*) FROM T_PURCHASE_ORDER_ITEM",
nativeQuery = true)
Page<PurchaseOrderItem> findByMateCodeNameLikeAndSuppCodeName(String supplierCode, String supplierName, String materialCode, String materialName, Pageable pageable);
debug之后报错,这是我出现的报错信息,大概意思就是如果是nativeQuery = true,就不能使用分页或者排序。
Caused by: org.springframework.data.jpa.repository.query.InvalidJpaQueryMethodException: Cannot use native queries with dynamic sorting and/or pagination in method public abstract org.springframework.data.domain.Page
找到官方栗子,发现好像自己写的没有什么问题。
但是注意栗子底下说了一句话,大概意思就是通过增加前缀可以是上面的查询方法工作。以下栗子可以参考以下:
- 这个是Oracle ,注意前缀 /#pageable/
@Query(value = "SELECT * FROM T_PURCHASE_ORDER_ITEM ORDER BY /*#pageable*/",
countQuery = "SELECT count(*) FROM T_PURCHASE_ORDER_ITEM",
nativeQuery = true)
Page<PurchaseOrderItem> findFf(String ff, Pageable pageable);
- 这个是mysql,注意前缀 #{#pageable}
@Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1 ORDER BY ?#{#pageable}",
countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
nativeQuery = true)
Page<User> findByLastname(String lastname, Pageable pageable);
这样写应该就不会出现我的那种报错。