MyBatis 常用寫法

MyBatis 常用寫法

1、forEach 循環

  forEach 元素的屬性主要有 item, idnex, collection, open, separator, close。

  1. collection:傳入的 List 或 Array 或自己封裝的 Map。
  2. item:集合中元素迭代時的別名。
  3. idnex:集合中元素迭代是的索引。
  4. open:where 後面表示以什麼開始,如以‘(’開始。
  5. separator:表示在每次進行迭代是的分隔符。
  6. close:where後面表示以什麼結束,如以‘)’結束。
//mapper中需要傳遞一個容器
public List<User> queryByIdList(List<Integer> userIdList);

<select id="queryByIdList" resultMap="BaseResultMap" parameterType="map">
    SELECT * FROM user
    WHERE userId IN
    <foreach collection="userIdList" item="id" index="index" open="(" close=")" separator=",">
        #{id}
    </foreach>
</select>

2、concat 模糊查詢

//模糊查詢使用concat拼接sql
<select id="queryByName" resultMap="BaseResultMap" paramterType"string">
    SELECT * FROM user
    <where>
        <if test="name != null">
            name like concat('%', concat(#{name}, '%'))
        </if>
    </where>
</select>

3、if + where 標籤

  用 if 標籤判斷參數是否有效來進行條件查詢。

<select id="getUserList" resultMap="BaseResultMap" paramterType="com.demo.User">
    SELECT * FROM user
    <where>
        <if test="userId !=null and userId!= ''">
            userId= #{userId}
        </if>
        <if test="name !=null and name!= ''">
            AND name= #{name}
        </if>
        <if phone="userId !=null and phone!= ''">
            AND phone= #{phone}
        </if>
    </where>
</select>

where 動態語句中,where 標籤會自動去掉 AND 或 OR。防止 WHERE AND 錯誤。

4、if + set

  使用 set 標籤可以動態的配置 SET 關鍵字,使用 if + set 標籤,如果某項爲 null 則不進行更新。

<update id="updateUser" paramterType="com.demo.User">
    UPDATE user
    <set>
        <if test=" name != null and name != ''">
            name = #{name},
        </if>
        <if test=" phone != null and phone != ''">
            phone = #{phone},
        </if>
    </set>
    WHERE userId = #{userId}
</update>

5、if + trim 代替 where/set 標籤

  trim 可以更靈活的去處多餘關鍵字的標籤,可以實現 where 和 set 的效果。

<select id="getUserList" resultMap="BaseResultMap" paramterType="com.demo.User">
    SELECT * FROM user
    <trim prefix="WHERE" prefixOverrides="AND|OR">
        <if test="userId !=null and userId!= ''">
            userId= #{userId}
        </if>
        <if test="name !=null and name!= ''">
            AND name= #{name}
        </if>
        <if phone="userId !=null and phone!= ''">
            AND phone= #{phone}
        </if>
    </trim>
</select>

<update id="updateUser" paramterType="com.demo.User">
    UPDATE user
    <trim prefix="SET" suffixOverrides=",">
        <if test=" name != null and name != ''">
            name = #{name},
        </if>
        <if test=" phone != null and phone != ''">
            phone = #{phone},
        </if>
    </trim>
    WHERE userId = #{userId}
</update>

5、choose(when, otherwise)標籤

  choose 標籤是按順序判斷其內部 when 標籤中的 test 條件是否成立,如果有一個成立,則 choose 結束。當 choose 中所有 when 的條件都不滿足,則執行 otherwise 中的 sql。類似 java 中的 switch 語句,choose 爲 switch,when 爲 case,otherwise 則爲 default。

<select id="selectCustomerByCustNameAndType" parameterType="map" resultMap="BaseResultMap">
    SELECT * FROM user
    <choose>
        <when test="Utype == 'name'">
            WHERE name = #{name} 
        </when>
        <when test="Utype == 'phone'">
            WHERE phone= #{phone}
        </when>
        <when test="Utype == 'email'">
            WHERE email= #{email}
        </when>
        <otherwise>
            WHERE name = #{name}
        </otherwise>
    </choose>
</select>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章