1. 程式人生 > >Jpa帶下拉框的多條件查詢

Jpa帶下拉框的多條件查詢

如下示例:

//查詢商品資訊
@Query(value = "select * from tb_product " +
        "where del_flag=?1 and name like ?4 and " +
        "case ?2 when 'all' then state is not null else state=?2 end and " +
        "case ?3 when 'all' then type_id is not null else type_id=?3 end",nativeQuery = true)
Page<Product> findByDelFlagAndStateAndTypeIdAndNameLike(String delFlag,String state, String typeId, String nameLike,Pageable pageable);
當下拉框選的是"選擇狀態"和"選擇型別"時傳的值未all,
此時可把條件設為 is not null,不能為!=null,否則會出錯