简体   繁体   English

带有可选参数的 Springboot 动态查询

[英]Springboot dynamic query with optional parameters

In the below I can have the modifedDate as null some times.在下面,我modifedDate可以将modifedDate设为 null。 at that time it is throwing error as "could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"当时它抛出错误,因为“无法提取 ResultSet;SQL [n/a];嵌套异常是 org.hibernate.exception.SQLGrammarException:无法提取 ResultSet”

How can I get data even the modifiedDate is null.即使modifiedDate为空,我如何获取数据。 Some time the date will be coming.某个时候日期将到来。 How do this ?这怎么办?

I want the below Query method return data always我希望下面的查询方法总是返回数据

  1. return based on adminStatus if modifiedDate is null.如果 modifiedDate 为空,则基于 adminStatus 返回。
  2. return based on adminStatus and modifiedDate.根据 adminStatus 和 modifiedDate 返回。 if modifiedDate is not null.如果 modifiedDate 不为空。

we can do by building a dynamic query and executing with entity Manager.我们可以通过构建动态查询并使用实体管理器执行来实现。 but I want with repository.但我想要存储库。

@Repository
public interface AdminRepository extends JpaRepository<AdminEntity, Long> {

@Query(
     value="SELECT * from admin_entity where status=:adminStatus and modified_tsz>=:modifedDate:", 
    nativeQuery=true)
  List<AdminEntity> getAdmins(String adminStatus, Date modifedDate)
}


Try this尝试这个

@Repository
public interface AdminRepository extends JpaRepository<AdminEntity, Long> {

@Query(
     value="SELECT * from admin_entity where status=:adminStatus
           AND  (modified_tsz>=:modifedDate OR :modifedDate is null)", 
    nativeQuery=true)
  List<AdminEntity> getAdmins(String adminStatus, Date modifedDate)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM