提问者:小点点

spring jpa数据本机Sql查询出现问题


我的所有存储库都在扩展commonService存储库,而commonService存储库反过来也在扩展JpaRepository和JpaSpecification

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

我想在CommonReadRepository中定义原生Sql查询,如:mysql:select*from table limit 1,20;

@Query(value=“Select*from?1 limit?2,?3”,nativeQuery=true)

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

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

我知道如果我可以定义存储库中的查询,那么我知道表名,但是否可能使它通用?

谢谢


共1个答案

匿名用户

在spring数据参考中定义的基本存储库部分

遵循参考文献中定义的指导原则,如下所示:

@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);
}

对于特定查询列表<?>customFindQuery(字符串表名、int偏移量、int限制);

jparepository中已经通过调用以下方法支持它:

Page<T> findAll(Pageable pageable)

例如:

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

其中offest=30(3 x 10),极限=10