今天试了一下springdatajpa 的带查询参数的分页功能,如果像官网给出的例子,使用本地sql 就报错,官方的例子如下
Note, that we currently don’t support execution of dynamic sorting for native queries as we’d have to manipulate the actual query declared and we cannot do this reliably for native SQL. You can however use native queries for pagination by specifying the count query yourself:
Example 50. Declare native count queries for pagination at the query method using @Query
public interface UserRepository extends JpaRepository<User, Long> {
@Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
nativeQuery = true)
Page<User> findByLastname(String lastname, Pageable pageable);
}
This also works with named native queries by adding the suffix .count to a copy of your query. Be aware that you probably must register a result set mapping for your count query, though.
我本地是这样写的。
@Query(value = "select u from UserInfo u where u.name=?1",
countQuery = "select count(*) from UserInfo u where u.name=?1",
nativeQuery = true)
Page<UserInfo> findByNameLike(String name,Pageable pageable);
运行就出错了。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appliation': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.it.core.repository.UserRepository com.it.core.web.Appliation.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is 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 com.it.core.repository.UserRepository.findByNameLike(java.lang.String,org.springframework.data.domain.Pageable)