Mybatis的Mapper對映檔案中常用標籤及作用

快救救猫猫發表於2024-10-14

Mybatis的Mapper對映檔案中常用標籤及作用


1.<mapper>標籤

主要用於定義 Mapper 介面的對映檔案。通常包含名稱空間(namespace),該名稱空間通常是介面的全限定類名。

 <mapper namespace="com.example.demo.mapper.UserMapper">

2.<insert>標籤

用於定義插入語句,對應於 Mapper 介面中帶有 @Insert 註解的方法。

   <insert id="insertUser" parameterType="com.example.demo.entity.User">
     INSERT INTO user (name, age) VALUES (#{name}, #{age})
   </insert>

其中id對應mapper層的方法名,parameterType對應傳入的引數型別

3.<update>標籤

用於定義更新語句,對應於 Mapper 介面中帶有 @Update 註解的方法。

   <update id="updateUser" parameterType="com.example.demo.entity.User">
     UPDATE user SET name=#{name}, age=#{age} WHERE id=#{id}
   </update>

4.<delete>標籤

用於定義刪除語句,對應於 Mapper 介面中帶有 @Delete 註解的方法。

   <delete id="deleteUserById" parameterType="int">
     DELETE FROM user WHERE id = #{id}
   </delete>

注意在使用<delete>和<updatev標籤是需要使用where關鍵字或者<where>標籤加入篩選條件,否則會影響整張表的欄位

5.<select>標籤

用於定義查詢語句,對應於 Mapper 介面中帶有 @Select 註解的方法。

   <select id="selectUserById" resultType="com.example.demo.entity.User">
     SELECT * FROM user WHERE id = #{id}
   </select>

其中resultType對應返回值的接收引數的型別。
注意:如果返回多個資料,需要使用List集合接收,否則會報錯

6.<resultMap>標籤

對於複雜的對映關係,尤其是資料庫表和物件模型不完全一致時。可以使用<resultMap>定義一對一或一對多的關係。效果和在查詢時給資料庫中的欄位起別名的效果是一樣的,主要是用於將資料庫欄位與物件屬性進行一對一對映的

<resultMap id="userResultMap" type="com.example.demo.entity.User">
     <id property="id" column="user_id"/>
     <result property="name" column="username"/>
     <result property="age" column="user_age"/>
   </resultMap>

7.<if>標籤

主要用於增刪改查四大標籤中指定條件使用,主要用於解決同一條查詢中可以存在為空的欄位查詢導致查詢結果異常的問題。

<if test="productKey != null and productKey != ''">
    and product_id = #{productKey}
</if>
<if test="functionName != null and functionName != ''">
    and function_name like concat('%',#{functionName},'%')
</if>
<if test="alertRuleName != null and !alertRuleName.isBlanck()">
    and alert_rule_name like concat('%',#{alertRuleName},'%')
</if>

8.<where>標籤

主要用於解決在使用<if>標籤是引數全為空導致使用where關鍵字後面需要拼接 1=1 條件的情況。在使用<where>標籤後,會在標籤內為空的情況下自動刪除該標籤,可以自動最佳化多餘的and連線詞。

<where>
    <if test="productKey != null and productKey != ''">
       and product_id = #{productKey}
    </if>
    <if test="functionName != null and functionName != ''">
       and function_name like concat('%',#{functionName},'%')
    </if>
    <if test="alertRuleName != null and alertRuleName != ''">
       and alert_rule_name like concat('%',#{alertRuleName},'%')
     </if>
</where>

9.<set>標籤

與<where>標籤同理,主要用於解決<update>標籤出現的可能修改引數全為空的情況,,可以自動最佳化多餘的,連線符

 <update id="updateUser" parameterType="com.example.demo.entity.User">
     <set>
       <if test="name != null">name=#{name},</if>
       <if test="age != null">age=#{age},</if>
     </set>
     WHERE id=#{id}
   </update>

10.<parameter>

用於定義引數型別和引數對映。雖然 <insert>、<update> 和 <delete> 標籤本身可以定義引數型別,但<parameter> 標籤提供了更細粒度的控制。

   <update id="updateUser" parameterType="com.example.demo.entity.User">
     <set>
       <if test="name != null">name=#{name},</if>
       <if test="age != null">age=#{age},</if>
     </set>
     WHERE id=#{id}
   </update>

11.<sql>和<include>標籤

主要用於引用其他 SQL 片段,避免重複編寫相同的 SQL 程式碼。

   <sql id="userColumns">name, age</sql>

   <select id="selectUserById" resultType="com.example.demo.entity.User">
     SELECT <include refid="userColumns"/> FROM user WHERE id = #{id}
   </select>

12.<choose>、<when>、<otherwise>標籤

用於條件判斷,使得 SQL 更加靈活。

  <select id="selectUsersByCondition" resultType="com.example.demo.entity.User">
    SELECT * FROM user WHERE
    <choose>
      <when test="name != null">
        name LIKE '%${name}%'
      </when>
      <when test="age != null">
        age = #{age}
      </when>
      <otherwise>
        1=1
      </otherwise>
    </choose>
  </select>

13.<foreach>標籤

用於遍歷集合或陣列,常用於批次操作或多條件查詢。

    <select id="selectUsersByIds" resultType="com.example.demo.entity.User">
      SELECT * FROM user WHERE id IN
      <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item}
      </foreach>
    </select>

主要item的值就是mapper層方法對於的屬性名或者使用@Param註解指定的值

14.<cache>標籤

用於定義快取策略,可以顯著提高查詢效能。透過使用快取,可以避免頻繁地執行相同的查詢操作,從而減少資料庫的負擔。

<mapper namespace="com.example.demo.mapper.UserMapper">
  <cache/>
  
  <!-- 其他 SQL 對映 -->
</mapper>

type:指定快取實現的型別,預設為 PERPETUAL(永久快取)。常見的型別有:
PERPETUAL:永久快取。
LRU:最近最少使用的快取。
FIFO:先進先出快取。
SOFT:軟引用快取。
WEAK:弱引用快取。
eviction:快取驅逐策略,預設為 LRU。常見的驅逐策略有:
LRU:最近最少使用的快取。
FIFO:先進先出快取。
flushInterval:快取重新整理間隔,單位為毫秒,預設為 0(不自動重新整理)。
size:引用表的大小,預設為 1024。
readOnly:是否只讀,預設為 false。設定為 true 表示快取的資料被認為是隻讀的,因此不會被更新。
blocking:是否阻塞,預設為 false。設定為 true 表示等待快取載入完成再返回結果。

15.<bind>標籤

用於定義變數繫結,可以在 SQL 中使用變數。

    <select id="selectUserById" resultType="com.example.demo.entity.User">
      <bind name="userId" value="'U_' + id"/>
      SELECT * FROM user WHERE id = #{userId}
    </select>

相關文章