如何在本机查询中动态传递限制和取消设置值

时间:2019-02-21 13:45:08

标签: java postgresql spring-data-jpa eclipselink nativequery

我正在使用本机查询从PostgreSQL获取分页结果,并且使用了此查询,并且遇到了以下异常:

SELECT a.*
  FROM table1 a LEFT OUTER JOIN table2 b ON a.clmn1 = b.clmn1
  WHERE (a.clmn3 = ?3 OR a.clmn4 ISNULL)
  ORDER BY a.clmn1 DESC offset = ?1 limit = ?2

查询:

@Query(nativeQuery = true, value="select a.* from table1 a left outer join table2 b ON a.clmn1 = b.clmn1 where (a.clmn3= ?3 OR a.clmn4 isnull) order by a.clmn1 desc offset = ?1 limit = ?2")
public List<Result> getResults(int offset, int limit, int value);

例外:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "="

请提出建议。

2 个答案:

答案 0 :(得分:0)

查询中存在语法错误。删除=并使用命名参数,例如:

@Query(nativeQuery = true, value = "SELECT a.* FROM table1 a "
    + " LEFT OUTER JOIN table2 b"
    + " ON a.clmn1 = b.clmn1 "
    + " WHERE (a.clmn3= ?3 OR a.clmn4 IS NULL) "
    + " ORDER BY a.clmn1 DESC OFFSET :offset LIMIT :limit ")
public List<Result> getResults(@Param("offset")int offset, 
                               @Param("limit")int limit, int value);

免责声明:未测试参数注入的工作方式,但语法应如下所示

答案 1 :(得分:0)

我相信错误是

offset = ?1 limit = ?2

https://www.postgresql.org/docs/8.0/queries-limit.html

偏移量和限制上不需要'='