Mybatis where 1=1 和 <where>标签

来源:互联网 发布:modo软件多少钱 编辑:程序博客网 时间:2024/04/29 10:43
   <select id="selSampleListByIDX4" resultMap="BaseResultMap" parameterType="cn.com.git.cbs.datamodel.TBL_Sample">    select     <include refid="Base_Column_List" />    from SAMPLE    where 1=1         <if test="samplenumber != null" >        AND SAMPLENUMBER = #{samplenumber,jdbcType=DECIMAL}      </if>   </select> 


Mybatis  之前拼条件的时候 写法  where 1=1,也可以使用<where>标签

<select id="findActiveBlogLike"     resultType="Blog">  SELECT * FROM BLOG   <where>     <if test="state != null">         state = #{state}    </if>     <if test="title != null">        AND title like #{title}    </if>    <if test="author != null and author.name != null">        AND author_name like #{author.name}    </if>  </where></select>

where 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且,若最后的内容是“AND”或“OR”开头的,where 元素也知道如何将他们去除。

如果 where 元素没有按正常套路出牌,我们还是可以通过自定义 trim 元素来定制我们想要的功能。比如,和 where 元素等价的自定义 trim 元素为:


<trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>




0 0
原创粉丝点击