需求场景:
在一个表中有id,name,email我们查询的时候,希望name不为null的时候,作为查询条件,如果email不为null的时候,作为查询条件。
实现过程:
(1)name,email and 关系
如果name和email只是并且的关系的话,那么我们会这么写:
@Select("Select * from Demo where name =#{name} and email=#{email} ")
(2)if name !=null ,if email != null
现在我们希望的是如果name不为null的话,那么就当做条件,否则就不要当做条件;如果email不为null,那么就当做条件,否则不当做条件。
那么方案一:so eay,只需要在前面加入
@Select("<script> " +
"SELECT * " +
"from Demo " +
" <where> " +
" 1=1" +
" <if test=\"name != null\">and name=#{name}</if> " +
" <if test=\"email != null\"> and email=#{email}</if> " +
" </where> " +
" </script> ")
public List<Demo> select4(Demo demo);
链接:Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案一:<script>
Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案二:@Provider
自己所做的记录一下
userType 这个参数, app端可传可不传。