JPA 使用page分页,不使用@Query时不报错,可以正常使用。
加入@Query之后就报错:
我的代码如下:
@Query(value = "select id,author,create_time as createTime,img_list as imgList,title,visits FROM news1 where tag like ? order BY create_time desc",nativeQuery=true)
Page<Map<String,Object>> findByTagLike(String tag,Pageable pageRequest);
报错如下:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where) FROM news1 where tag like '%推荐%'' at line 1
还以为是sql写的有问题,复制到navicat里面测试,发现sql写的没问题,后面加上了countQuery参数解决问题:
@Query(value = "select id,author,create_time as createTime,img_list as imgList,title,visits FROM news1 where tag like ? order BY create_time desc",
countQuery = "select count(*) FROM news1 where tag like ?",nativeQuery=true)
Page<Map<String,Object>> findByTagLike(String tag,Pageable pageRequest);
至于为啥必须要加上countQuery还没搞清楚,搞清楚之后再回来写,有知道的或者遇到过类似问题的同学也可以留言