1. 程式人生 > >MyBatis的入門筆記(二)

MyBatis的入門筆記(二)

MyBatis的入門筆記(二)

                                       - 2018年11月14日

動態SQL:

如資料庫中有這樣一張學生表,下面將從這張表的增刪改查說起動態SQL

1.動態SQL:如果+其中語句

根據姓名和年齡來查詢資料,如果名稱為空,那麼將只根據年齡來查詢,反之根據名稱來查詢。

如果不使用動態SQL我們查詢所有資料,或者根據姓名和年齡來查詢,如果名稱為空,查詢的結果也就為空。

這樣用到動態SQL來查詢的話,在SQL中判斷是否傳入,如果標籤來判斷要比JDBC寫SQL拼接要簡單很多。

<select id="selectgetif" resultType="Student">
        select * from student
        <!--<where>
            <if test="name != null">
                name=#{name}
            </if>
            <if test="age != 0">
                and age=#{age}
            </if>
        </where>-->
      <!--動態sql trim語句-->
        <trim prefix="where" prefixOverrides="and | or">
            <!--<if test="name != null">
                and name=#{name}
            </if>
            <if test="age != 0">
                and age=#{age}
            </if>-->
            <!--引入sql片段,如果refid 指定的不在本檔案中,那麼需要在前面加上 namespace-->
            <include refid="studentifsql"></include>
        </trim>
    </select>

這個”這樣如果名稱不傳的話,SQL就是這樣的:

反正也一樣,值得注意的地方就是”這個地方標籤,標籤會知道如果它包含的標籤中有返回值得話,就插入那裡,如果標籤的返回內容以與和或開頭,MyBatis的會自動剔除掉。

2.動態SQL:如果+設定語句

同理,上面的對於查詢SQL語句包含其中關鍵字,如果在進行更新操作的時候,含有設定關鍵字,含有設定關鍵字,我們該怎麼處理。

現在這裡根據ID更新表中資料:

<!--動態sql if+set判斷更新語句-->
    <update id="updateif">
          update student
          <!--<set>-->
              <!--<if test="name !=''">-->
                  <!--name = #{name},-->
              <!--</if>-->
              <!--<if test="age !=0">-->
                   <!--age = #{age}-->
              <!--</if>-->
          <!--</set>-->
        <trim prefix="set" prefixOverrides=",">
            <if test="name !=''">
            name = #{name},
            </if>
            <if test="age !=0">
            age = #{age}
            </if>
        </trim>
        where id=#{id}
    </update>

如果名稱為空,也就是沒有傳值,

sql就是這樣的:更新學生SET年齡=?id =?“

3.動態SQL:選擇(當,否則)語句

有時候我們我們不想用到所有的查詢條件,只想選擇其中一個,查詢條件有一個滿足即可,這個時候就可以使用選擇標籤可以解決此類問題,類似於Java的的開關語句

<!--動態sql:choose(when,otherwise)語句意思就是隻能以一種作為查詢條件-->
    <select id="selectgetchoose" resultType="Student">
          select * from student
          <where>
              <choose>
                  <when test="id != 0">
                      id=#{id}
                  </when>
                  <when test="name != null">
                      and name=#{name}
                  </when>
                  <!--相當於else-->
                  <otherwise>
                      and age=#{age}
                  </otherwise>
              </choose>
          </where>
    </select>

這裡如果只傳入了名

那麼sql語句就是這種:select * from student WHERE name =?

這裡的XML中選擇查詢條件,也就是這裡總共寫了三個條件,有ID,姓名,年齡

如果ID為空,那麼看名字是否為空,如果不為空,那麼語句為上面那種,

如果名稱為空,sql語句就是:select * from student WHERE age =?

4.動態SQL:修剪語句

修剪標記是一個格式化的標記,可以以完成設定或者是其中標記的功能

a.prefix:字首:其中或者集......

b.prefxoverride:去掉第一個和或者或(組就是去掉後面的)

鍵入程式碼:

5.動態SQL:SQL片段

有時候某些sql語句是一樣的,用的特別多,就比如查詢時用的select * from等等,為了增加程式碼的重用性,簡化程式碼,我們需要將這些程式碼抽取出來,然後使用時直接呼叫。

比如:上面的地方語句判斷就可以將程式碼抽取出來,

<!--定義sql片段-->
    <sql id="studentifsql">
        <if test="name != null">
           and name=#{name}
        </if>
        <if test="age != 0">
            and age=#{age}
        </if>
    </sql>

在引入SQL

<!--引入sql片段,如果refid 指定的不在本檔案中,那麼需要在前面加上 namespace-->
            <include refid="studentifsql"></include>

注意:在SQL片段中不要包含其中

6.動態SQL:的foreach語句

需求:我們需要查詢表中的ID分別為1,2,3的學生

Sql語句包括:select * from student,其中id = 1或id = 2或id = 3

或者:從學生中選擇*,其中id為(1,2,3)

①。首先需要新建一個StudentVo類,裡面封裝一個列表<Integer> id的屬性

然後用來自學生的foreach來改寫select *,其中id = 1或id = 2或id = 3

<!--動態sql之foreach,查詢多個id的Student-->
    <select id="selectids" resultType="Student">
          select * from student
          <where>
              <!--
                collection:指定輸入物件中的集合屬性
                item:每次遍歷生成物件
                open:開始遍歷時的拼接字串
                close:結束時拼接的字串
                separator:遍歷物件之間需要拼接的字串
                 select * from student where id=1 or id=2 or id=3
              -->
              <foreach collection="id" item="id" open="(" close=")" separator="or">
                  id=#{id}
              </foreach>
          </where>
    </select>

測試類:

控制檯列印:

Log4j中的日誌輸出SQL為:

同理:用的foreach來改寫

select * from student where where in(1,2,3)也是一樣的