@Query(value ="select table from Table table "+"where ( ?1 is null or table.a = ?1 ) "+"and ( ?2 is null or table.b like %?2% ) "+"and ( ?3 is null or table.c = ?3) "+"order by ?#{#pageable} ", countQuery ="select table from Table table "+"where ( ?1 is null or table.a = ?1 ) "+"and ( ?2 is null or table.b like %?2% ) "+"and ( ?3 is null or table.c = ?3) ")
List<Table>findTableList(String a, String b, String c, Pageable pageable);
① 需要注意的是,这里用到了模糊查询,在参数两侧根据需要加上“%”;
② a,b,c参数需要在方法中进行一次判断,不能传一个空值;
2、sql形式
@Query(value ="select * from table where "+"if(?1 !='',x1=?1,1=1) "+"and if(?2 !='',x2=?2,1=1) "+"and if(?3 !='',x3=?3,1=1) ", nativeQuery =true)
List<Table>findTableList(String a, String b, String c);