【问题标题】:Trouble with spring jpa data native Sql queryspring jpa数据原生Sql查询出现问题
【发布时间】:2016-09-30 00:58:21
【问题描述】:

我所有的存储库都在扩展 commonService Repository,而后者又扩展了 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,不幸的是,我在文档中没有找到太多关于语法的信息。

我知道我是否可以在存储库中定义查询,然后我知道表名,但是否可以使其通用?

谢谢

【问题讨论】:

  • 也许如果您发布完整的堆栈跟踪,那么会有更多信息可以实际评论。更像是一个 MySQL 异常...

标签: mysql sql spring spring-boot spring-data-jpa


【解决方案1】:

在 Spring 数据中定义的 Base Repository Section 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&lt;?&gt; customFindQuery(String tableName,int offset,int limit);

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

Page<T> findAll(Pageable pageable)

例如:

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

其中 offest = 30 (3 x 10),limit = 10

【讨论】:

    猜你喜欢
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多