对JPA不是很熟悉,对其一些原生sql的操作支持方面查了很多,都没一个可参考的,所以记录下。
@Query(value = "select * from table where if(?1 !='',a=?1,1=1) and (coalesce (?2 , null) is null or b IN ( ?2 ))",nativeQuery = true)
Page<TableBean> findByaAndb(String str, List<String> list, Pageable pageable);
其中需要说明的是list不能判断size=0,所以在执行查询语句前需要判断size为0的情况,将其设置为null,类似于这样:
List<String> list1 = list.size() == 0 ? null : list;