mybtais的mapper中使用@Select注解使用if

一、场景描述

在springboot项目中使用mybatis+mybatis-plus,为满足业务需要,在mapper中定义的方法中使用@Select映射SQL语句  注意事项 :  1.需要使用标签 script标签包裹  2.注意大于小于符号的使用时的转义  3.mybatis常用转义如下

&lt;          < 
    &gt;          >  
    &lt;&gt;     <>
    &amp;        & 
    &apos;       
    &quot;       "
@Select({
          
   "<script>"+
" select t.* from track_name t"+
" <where>"+
" <if test=username!= null and username !="" >"+
" and t.username=#{username}"+
" </if>"+
" <if test=gmtCreateTime!=null and gmtCreateTime !="" >"+
" and t.gmtCreateTime &gt; #{gmtCreateTime} "+
" </if>"+
" </where>"+
"</script>"
})
IPage<TrackName> queryCondition(Page<TrackName> page,@Param("username")String username,@Param("gmtCreateTime")String gmtCreateTime)
经验分享 程序员 微信小程序 职场和发展