JPA多条件动态查询
  iCAEEniAA7wo 2023年12月22日 46 0



JPA多条件动态查询



如果每个参数都是必填项,则


@Query(value = "select * from edge_alarm_notify_record a left join edge_alarm_notify_config b on a.notify_config_id = b.notify_config_id where 1 = 1" +
           "b.notify_name LIKE CONCAT('%',?1,'%') and" +
           "a.addressee = ?2 and" +
           "b.notify_type = ?3 and" +
           "a.state = ?4 and" +
           "a.send_date BETWEEN ?5 AND ?6 and" +
           "a.notify_config_id = ?7",nativeQuery = true)
   Page<AlarmNotifyRecord> pageQueryNotifyRecord(@Param("addressee") String addressee,
                                                 @Param("state") String state,
                                                 @Param("startDate") Date startDate,
                                                 @Param("endDate") Date endDate,
                                                 @Param("configids") List<String> configids,
                                                 Pageable pageable);

多条件动态查询

@Query(value = "select * from edge_alarm_notify_record a where" +
           "(:addressee is null or a.addressee = :addressee) and" +
           "(:state is null or a.state = :state) and" +
           "(:startDate is null or a.send_date >= :startDate) and" +
           "(:endDate is null or a.send_date <= :endDate) and" +
           "('-1' in :configids or a.notify_config_id in :configids)" , nativeQuery = true)
   Page<AlarmNotifyRecord> pageQueryNotifyRecord(@Param("addressee") String addressee,
                                                 @Param("state") String state,
                                                 @Param("startDate") Date startDate,
                                                 @Param("endDate") Date endDate,
                                                 @Param("configids") List<String> configids,
                                                 Pageable pageable);

或者

@Query(value = "select * from edge_alarm_notify_record a left join edge_alarm_notify_config b on a.notify_config_id = b.notify_config_id where " +
            "b.notify_name LIKE CASE WHEN :#{#criteria.notifyName} IS NULL THEN b.notify_name ELSE CONCAT('%', :#{#criteria.notifyName}, '%') END," +
            "a.addressee = CASE WHEN :#{#criteria.addressee} IS NULL THEN a.addressee ELSE :#{#criteria.addressee} END," +
            "b.notify_type = CASE WHEN :#{#criteria.notifyType} IS NULL THEN b.notify_type ELSE :#{#criteria.notifyType} END," +
            "a.state = CASE WHEN :#{#criteria.state} IS NULL THEN a.state ELSE :#{#criteria.state} END," +
            "a.send_date >= CASE WHEN :#{#criteria.startDate} IS NULL THEN a.send_date ELSE :#{#criteria.startDate} END," +
            "a.send_date <= CASE WHEN :#{#criteria.endDate} IS NULL THEN a.send_date ELSE :#{#criteria.endDate} END," +
            "a.notify_config_id = CASE WHEN :#{#criteria.notifyConfigId} IS NULL THEN a.notify_config_id ELSE :#{#criteria.notifyConfigId} END ", nativeQuery = true)
    Page<AlarmNotifyRecord> pageQueryNotifyRecord(@Param("criteria")AlarmNotifyRecordCriteria criteria,
                                                  Pageable pageable);


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月22日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   55   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   110   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
iCAEEniAA7wo