spring jpa 数据本机 Sql 查询有问题

Trouble with spring jpa data native Sql query

我所有的存储库都在扩展 commonService Repository,在 return 中扩展了 JpaRepository 和 JpaSpecification

public interface CommonReadRepository<T>
        extends JpaRepository<T, Long>, JpaSpecificationExecutor<T> {

我想在 CommonReadRepository 中定义本机 Sql 查询,例如: Mysql:select * 来自 table 限制 1,20;

@Query(value = "select * from ?1 limit ?2,?3", nativeQuery = true)

 List<?> customFindQuery(String tableName,int offset,int limit);

但我收到 SqlGrammerException,不幸的是,我没有在文档中找到太多关于语法的信息。

我知道如果我可以在存储库中定义查询,那么我知道 table 名称但是是否可以将其设为通用名称?

谢谢

Spring 数据 reference

中定义的基础存储库部分

遵循参考中定义的指南,如下所示:

@NoRepositoryBean
interface CommonReadRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {

  List<T> custonFindQuery();
}
@Repository
interface UserRepository extends CommonReadRepository<User, Long> {
  User findByEmailAddress(EmailAddress emailAddress);
}

针对您的具体查询List<?> customFindQuery(String tableName,int offset,int limit);

它已经在 JpaRepository 中通过调用方法支持:

Page<T> findAll(Pageable pageable)

例如:

Page<User> all = userRepository .findAll(new PageRequest(3, 10));

其中偏移 = 30 (3 x 10),并且限制 = 10