Spring JPA-限制查询不适用于nativeQuery = true

发布时间:2020-07-07 15:49

我正在使用两个可选参数(列表)进行动态查询。

我想使用以下查询添加从数据库中检索的记录的限制数量:

@Query(value = "SELECT * FROM TM23WorkingBuilding WHERE (coalesce(?1, null) is null or buildingCode IN ?1) AND (coalesce(?2, null) is null OR buildingName IN ?2) AND isDel = ?3 "
       + "ORDER BY createdDate DESC LIMIT ?4", nativeQuery=true)
Page<TM23WorkingBuilding> getWorkingBuildingsByOptionalParams(List<String> buildingCodes, List<String> buildingNames, boolean isDel, Integer limit, Pageable pageable); 

但是当我调用该方法时,出现异常:

 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'null) AND (coalesce(null, null) is null OR buildingName IN null) AND isDel = 0 O' at line 1 

为什么会出现此错误?有人可以指出我在做什么错吗?!

当两个列表都为空时,将引发异常!

回答1