MyBatis框架

5.4 set

  • set 主要是用于解决修改操作中SQL语句中可能多出逗号的问题

<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 主要是用于分支判断,类似于java中的switch case,只会满足所有分支中的一个

<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>