SpringBoot开发日记(五)——如何在@Query中动态的判断参数是否为空

 直接上代码:

@Query(value = "select * from table_name where if(?1!=null and ?1!='',key=?1,1=1) and if(?2!=null and ?2!='',key2=?2,1=1),nativeQuery = true)
public List queryPage(String key,String key2);

使用条件:当有多个查询条参数且有时参数会为空时,为了避免写多个参数不同的查询函数,可以使用该方法。

理解:if(?1!=null and ?1!='',key=?1,1=1)

如果?1!=null and ?1 !=''成立,则返回key=?1,否则返回1=1

你可能感兴趣的:(SpringBoot,SpringBoot)