MyBatis框架

5.4 set

  • set 主要是用于解決修改操作中SQL語(yǔ)句中可能多出逗號(hào)的問(wèn)題

<update id="updateEmpByConditionSet">

?????????????????? update? tbl_employee?

?????????????????? <set>

??????????????????????????? <if test="lastName!=null &amp;&amp; lastName!=&quot;&quot;">

???????????????????????????????????? ?last_name = #{lastName},

??????????????????????????? </if>

??????????????????????????? <if test="email!=null and email.trim()!=''">

???????????????????????????????????? ?email = #{email} ,

??????????????????????????? </if>

??????????????????????????? <if test="&quot;m&quot;.equals(gender) or &quot;f&quot;.equals(gender)">

???????????????????????????????????? gender = #{gender}

??????????????????????????? </if>

?????????????????? </set>

?????????????????? ?where id =#{id}

???????? </update>

 

5.5 choose(when、otherwise)

  • choose 主要是用于分支判斷,類(lèi)似于java中的switch case,只會(huì)滿足所有分支中的一個(gè)

<select id="getEmpsByConditionChoose" resultType="com.atguigu.mybatis.beans.Employee">

????????????? select id ,last_name, email,gender from tbl_employee

????????????? <where>

???????????????????? <choose>

??????????????????????????? <when test="id!=null">

?????????????????????????????????? id = #{id}

??????????????????????????? </when>

??????????????????????????? <when test="lastName!=null">

?????????????????????????????????? last_name = #{lastName}

??????????????????????????? </when>

??????????????????????????? <when test="email!=null">

?????????????????????????????????? email = #{email}

??????????????????????????? </when>

??????????????????????????? <otherwise>

?????????????????????????????????? ?gender = 'm'

??????????????????????????? </otherwise>

???????????????????? </choose>

????????????? </where>

</select>