1. 程式人生 > >Mybatis對映檔案(3)

Mybatis對映檔案(3)

動態SQL

1、if/where
<!--
    test:判斷表示式(OGNL自己去查怎麼用)
    test="id != null":從引數中取值進行判斷
    拼裝的時候有的條件沒帶可能導致sql拼裝會有問題
    1、給where後面加上1=1,以後的條件都and
    2、mybatis使用<where>標籤來將所有查詢條件包括,
    mybatis就會將where標籤中拼裝的sql多出來的and或者or去掉
    where只會去掉第一個多出來的and或者or
-->
<select id="getEmpsByConditionIfAndWhere" 
resultType="bean.Employee"> SELECT * FROM t_employee <where> <if test="id != null"> id=#{id} </if> <if test="lastName != null and lastName != ''"> AND last_name=#{lastName} </if> <if test="email != null and email.trim() != ''"
> AND email=#{email} </if> <if test="gender==0 or gender==1"> AND gender=#{gender} </if> </where> </select>
2、trim自定義字串擷取規則
<!--
若將and放在每個條件之後,<where>標籤不能解決拼接的問題
trim:自定義字串擷取規則
prefix="":字首:trim標籤中是整個字串拼串後的結果
            prefix給拼串後的整個字串加一個字首
prefixOverrides="":字首覆蓋,去掉整個字串前面多餘的字元 suffix="":字尾: suffix給拼串後的字串加一個字尾 suffixOverrides="":字尾覆蓋,去掉整個字串後面多餘的 --> <select id="getEmpsByConditionTrim" resultType="bean.Employee"> SELECT * FROM t_employee <trim prefix="where" suffixOverrides="and"> <if test="id != null"> id=#{id} AND </if> <if test="lastName != null and lastName != ''"> last_name=#{lastName} AND </if> <if test="email != null and email.trim() != ''"> email=#{email} AND </if> <if test="gender==0 or gender==1"> gender=#{gender} </if> </trim> </select>
3、choose (when, otherwise):分支選擇
<!--
如果帶了id就用id查,如果帶了lastName就用lastName查,只會進入其中一個
-->
<select id="getEmpsByConditionChoose" resultType="bean.Employee">
SELECT * FROM t_employee
    <where>
        <choose>
            <when test="id!=null">
id=#{id}
            </when>
            <when test="lastName!=null and lastName!=''">
last_name LIKE #{lastName}
            </when>
            <when test="email!=null and email!=''">
email=#{email}
            </when>
            <otherwise>
1=1
            </otherwise>
        </choose>
    </where>
</select>
4、where-封裝查詢條件, set-封裝修改的條件
<update id="updateEmp">
UPDATE t_employee
    <set>
        <if test="lastName!=null and lastName!=''">
last_name=#{lastName},
        </if>
        <if test="email!=null and email!=''">
email=#{email},
        </if>
        <if test="gender!=null">
gender=#{gender}
        </if>
    </set>
    <where>
id=#{id}
    </where>
<!-- UPDATE t_employee
    <trim prefix="set" suffixOverrides=",">
        <if test="lastName!=null and lastName!=''">
            last_name=#{lastName},
        </if>
        <if test="email!=null and email!=''">
            email=#{email},
        </if>
        <if test="gender!=null">
            gender=#{gender}
        </if>
    </trim>
    <where>
        id=#{id}
    </where>-->
</update>
5、foreach標籤——批量處理
<!--
collection:指定要遍歷的集合
    list型別的引數會特殊處理封裝在map中,map的key就是list
item:將當前遍歷出的元素賦值給指定的變數
#{變數名}:就能取出變數的值,也就是當前遍歷出的元素
separator:每個元素之間的分隔符
open/close:遍歷出所有結果拼接一個開始和結束的字元(整個語句以什麼開始和什麼結束)
index:索引:遍歷list的時候就是索引,item就是當前值
            遍歷map的時候就是map的key,item就是map的value
-->
<select id="getEmpsByConditionForeach" resultType="bean.Employee">
SELECT * FROM t_employee WHERE id IN
        <foreach collection="list" item="item_id" separator="," open="(" close=")">
#{item_id}
        </foreach>
</select>
<!--批量儲存-->
<!--MySQL下批量儲存,可以foreach遍歷values-->
<!--<insert id="addEmps">
    INSERT INTO t_employee(id,last_name,gender,email) VALUES
    <foreach collection="emps" item="emp" separator=",">
        (#{emp.id},#{emp.lastName},#{emp.gender},#{emp.email})
    </foreach>
</insert>-->
<!--該種方法會有語法異常,需要開啟sql語句間用“;”分隔的許可權—allowMultiQueries=true-->
<insert id="addEmps">
    <foreach collection="emps" item="emp" separator=";">
INSERT INTO t_employee(id,last_name,gender,email) VALUES
        (#{emp.id},#{emp.lastName},#{emp.gender},#{emp.email})
    </foreach>
</insert>
6、內建引數和bind
<!--
兩個內建引數:
不只是方法傳遞過來的引數可以被用來判斷、取值
mybatis預設還有兩個內建引數:
_parameter:代表整個引數
    單個引數:_parameter就是這個引數
    多個引數:引數會被封裝為一個map,_parameter就是代表這個map
_databaseId:如果配置了DatabaseIdProvider標籤
    _databaseId就是代表當前資料庫的別名
bind:可以將OGNL表示式的值繫結到一個變數中,方便後來引用這個變數的值
 <bind name="_lastName" value="'%'lastName'%'"></bind>這樣寫了就可以不用在傳入引數的時候寫%號
-->
<select id="getEmpsTestInnerParameter" resultType="bean.Employee">
    <bind name="_lastName" value="'%'lastName'%'"></bind>
    <if test="_databaseId=='mysql'">
SELECT * FROM t_employee
        <if test="_parameter!=null">
WHERE last_name LIKE #{_lastName}
        </if>
    </if>
    <if test="_databaseId=='oracle'">
SELECT * FROM employee
        <if test="_parameter!=null">
WHERE last_name=#{lastName}
        </if>
    </if>
</select>
7、可重用SQL——經常將要查詢的列名或插入用的列名抽取出來方便引用
<!--
1、sql標籤抽取可重用的sql片段,方便後面引用
    裡面還可以做動態判斷,經常將查詢和插入的列名抽取出來
2、include標籤就是引用從外部定義的sql
3、include還可以自定義有些property,sql標籤內部就能使用自定義的屬性,取值的方式${pro},#{}不可以
-->
<sql id="insertColumn">
    <if test="_databaseId=='mysql'">
id,last_name,gender,email
    </if>
    <if test="_databaseId=='oracle'">
--------
    </if>
</sql>
<insert id="addEmps">
    <foreach collection="emps" item="emp" separator=";">
INSERT INTO t_employee(
        <include refid="insertColumn"></include>
) VALUES
        (#{emp.id},#{emp.lastName},#{emp.gender},#{emp.email})
    </foreach>
</insert>