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);

你可能感兴趣的:(java,jpa)