jpa 条件不为空加 条件查询

在使用spring boot JPA写repository的时候参数可能为空不传

@Query(value = "select * from mw_user where is_identification = 1 and is_inter_identification = 1  and if(?1 !='',mobile=?1,1=1) and if(?2 !='',nick=?2,1=1)",nativeQuery = true)
List findBySearch(String mobile,String nick);

利用原生sql 加 if的方式实现参数为空不作为查询条件

 

@Query(value = "select * from mw_user where is_identification = 1 and is_inter_identification = 1  and if(?1 !='',mobile=?1,1=1) and if(?2 !='',nick  LIKE CONCAT('%',?2,'%'),1=1)",nativeQuery = true)
List findBySearchTotal(String mobile,String nick);
@Query(value = "select count(1) from mw_user where mw_user.is_identification='1' and mw_user.is_inter_identification='1' and if(?1 !='',mobile=1?,1=1) and if(?2 !='',nick LIKE CONCAT('%',?2,'%'),1=1)",nativeQuery = true)
Long findBySearchTotalCount(String mobile,String nick);

加like查询

你可能感兴趣的:(问题方案,jpa,条件不为空加,条件查询,sql条件不为空加,条件查询)