溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MyBatis常用寫法有哪些

發(fā)布時間:2021-08-13 17:20:25 來源:億速云 閱讀:168 作者:小新 欄目:編程語言

小編給大家分享一下MyBatis常用寫法有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

什么是 MyBatis ?

MyBatis 是一款優(yōu)秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數(shù)以及獲取結(jié)果集。MyBatis 可以使用簡單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數(shù)據(jù)庫中的記錄。

1、forEach 循環(huán)

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

1.collection:傳入的 List 或 Array 或自己封裝的 Map。
2.item:集合中元素迭代時的別名。
3.idnex:集合中元素迭代是的索引。
4.open:where 后面表示以什么開始,如以‘('開始。
5.separator:表示在每次進行迭代是的分隔符。
6.close:where后面表示以什么結(jié)束,如以‘)'結(jié)束。

//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 標簽判斷參數(shù)是否有效來進行條件查詢。

<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 動態(tài)語句中,where 標簽會自動去掉 AND 或 OR。防止 WHERE AND 錯誤。

4、if + set

使用 set 標簽可以動態(tài)的配置 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 可以更靈活的去處多余關鍵字的標簽,可以實現(xiàn) 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 標簽是按順序判斷其內(nèi)部 when 標簽中的 test 條件是否成立,如果有一個成立,則 choose 結(jié)束。當 choose 中所有 when 的條件都不滿足,則執(zhí)行 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>

以上是“MyBatis常用寫法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI