JPA多条件动态查询

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);
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开始摆烂ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值