1. 程式人生 > 實用技巧 >Mybatis學習筆記一(簡介及動態sql)

Mybatis學習筆記一(簡介及動態sql)

Mybatis簡介

MyBatis是一款優秀的基於ORM的半自動輕量級持久層框架,它支援定製化SQL、儲存過程以及高階對映。MyBatis避免了幾乎所有的JDBC程式碼和手動設定引數以及獲取結果集。MyBatis可以使用簡單的XML或註解來配置和對映原生型別、介面和Java的POJO為資料庫中的記錄。對開發人員而言,核心sql還是需要自己優化,sql和java編碼分開,功能邊界清晰,一個專注業務,一個專注資料。

配置檔案分析

核心配置檔案層級關係

資料庫環境配置

動態sql標籤

where if 條件
<select id="findByCondition" parameterType="user" resultType="user"> 
select * from User
    <where>
        <if test="id!=0">
            and id=#{id}
        </if>
        <if test="username!=null">
            and username=#{username}
        </if>
    </where>
</select>
        
for each

foreach標籤主要有以下引數:
item:迴圈體中的具體物件。支援屬性的點路徑訪問,如item.age,item.info.details,在list和陣列中是其中的物件,在map中是value。
index:在list和陣列中,index是元素的序號,在map中,index是元素的key,該引數可選。
open:表示該語句以什麼開始
close:表示該語句以什麼結束
separator:表示元素之間的分隔符,例如在in()的時候,separator=","會自動在元素中間用“,“隔開,避免手動輸入逗號導致sql錯誤,如in(1,2,)這樣。該引數可選。

<select id="findByIds" parameterType="list" resultType="user"> 
    select * from User
    <where>
        <foreach collection="array" open="id in(" close=")" item="id" separator=",">
            #{id}
        </foreach>
    </where>
</select>
        

批量插入

<!--動態Sql: foreach標籤, 批量插入-->
        <insert id="dynamicSqlInsertList" useGeneratedKeys="true" keyProperty="id">
            insert into users (name, age, county, date)
            values
            <foreach collection="list" item="user" separator="," >
                (#{user.name}, #{user.age}, #{user.county}, #{user.date})
            </foreach>
        </insert>

choose、when、otherwise 標籤
 <!--動態Sql: choose、when、otherwise 標籤-->
    <select id="dynamicSql2" resultType="com.lks.domain.User">
        select * from users
        <where>
            <choose>
                <when test="name != null and name != ''">
                    AND name = #{name}
                </when>
                <when test="county != null and county != ''">
                    AND county = #{county}
                </when>
                <otherwise>
                    AND id = #{id}
                </otherwise>
            </choose>
        </where>
    </select>

set標籤

使用set標籤可以將動態的配置 SET 關鍵字,並剔除追加到條件末尾的任何不相關的逗號。使用 if+set 標籤修改後,在進行表單更新的操作中,哪個欄位中有值才去更新,如果某項為 null 則不進行更新,而是保持資料庫原值。

<!--動態Sql: set 標籤-->
    <update id="updateSet" parameterType="com.lks.domain.User">
        update users
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="county != null and county != ''">
                county = #{county},
            </if>
        </set>
        where id = #{id}
    </update>

trim標籤

trim 是一個格式化標籤,可以完成< set > 或者是 < where > 標記的功能。主要有4個引數:
① prefix:字首

② prefixOverrides:去掉第一個and或者是or

③ suffix:字尾

④ suffixOverrides:去掉最後一個逗號,也可以是其他的標記

<!--動態Sql: trim 標籤-->
    <select id="dynamicSqlTrim" resultType="com.lks.domain.User">
        select * from users
        <trim prefix="where" suffix="order by age" prefixOverrides="and | or" suffixOverrides=",">
            <if test="name != null and name != ''">
                AND name = #{name}
            </if>
            <if test="county != null and county != ''">
                AND county = #{county}
            </if>
        </trim>
    </select>