mybatis

1,翻頁與集合

2,批量

foreach元素的屬性主要有 item,index,collection,open,separator,close。

item表示集合中每一個元素進行迭代時的別名.

index指 定一個名字,用於表示在迭代過程中,每次迭代到的位置.

open表示該語句以什麼開始,separator表示在每次進行迭代之間以什麼符號作爲分隔 符.

close表示以什麼結束

<select id="selectTestForEach" parameterType="News" resultMap="NewsResultMapper">
  select * from t_news n where 
  <foreach collection="listTag" index="index" item="tag" open="("
    separator="," close=")">
   #{tag} in n.tags
  </foreach>

==select * from t_news n where ( ? in n.tags , ? in n.tags )


批量插入

mysql 

<insert id="batchInsertStudent" parameterType="java.util.List">
    INSERT INTO STUDENT (id,name,sex,tel,address)
    VALUES 
    <foreach collection="list" item="item" index="index" separator="," >
        (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})
    </foreach>
</insert>

oracle是這樣的

<insert id="add" parameterType="EStudent">
  <selectKey keyProperty="id" resultType="_long" order="BEFORE">
    select CAST(RANDOM * 100000 as INTEGER) a FROM SYSTEM.SYSDUMMY1
  </selectKey>
  insert into TStudent(id, name, age) values(#{id}, #{name}, #{age}) //注意這個id是上面selectkey出來的, BEFORE表示是在insert語句前還是後執行selectKey操作,type表示類型 id表示這個數據會放到EStudent這個類的id屬性下.
</insert>

或者下面這樣的

<select id="batchSave" parameterType="java.util.List">
                INSERT INTO TABLE_NAME(ID,NAME) 
                <foreach collection="list"  item="itm" separator="union all">
                        (SELECT #{itm.id},#{itm.name} FROM DUAL) //就是這裏不一樣.
                </foreach>
        </select>
如果上面mysql的insert標籤報錯.用下面的肯定不會報錯
<select id="batchSave" parameterType="java.util.List">
                INSERT INTO TABLE_NAME(ID,NAME) VALUES
                <foreach collection="list"  item="itm" separator=",">
                        (#{itm.id},#{itm.name})
                </foreach>
        </select>
上面標籤這麼多,如果在最外層寫了![CDATA[<foreach> ]],那foreach不會執行.xml解析器直接把它當做字符串了.

過程;

SessionFactoryUtil.getSqlSessionFactory().openSession()
-->session.insert --> session.commit --> close session

1,上面的如果一條記錄報錯,會回滾麼?

2,返回值設多少?

where 和 if 標籤

抄的 where 標籤,防止出現 select * from 什麼條件都不符合也有一個 where. 順便有一個if 標籤的使用

  1. <select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">     
  2.     SELECT * from STUDENT_TBL ST      
  3.     <where>     
  4.         <if test="studentName!=null and studentName!='' ">     
  5.             ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
  6.         </if>     
  7.         <if test="studentSex!= null and studentSex!= '' ">     
  8.             AND ST.STUDENT_SEX = #{studentSex}      
  9.         </if>     
  10.         <if test="studentBirthday!=null">     
  11.             AND ST.STUDENT_BIRTHDAY = #{studentBirthday}      
  12.         </if>     
  13.         <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">     
  14.             AND ST.CLASS_ID = #{classEntity.classID}      
  15.         </if>     
  16.     </where>     
  17. </select>     

set 標籤類似 where 用在 update xxx set 中

  1. update id="updateStudent" parameterType="StudentEntity">     
  2.     UPDATE STUDENT_TBL      
  3.     <set>     
  4.         <if test="studentName!=null and studentName!='' ">     
  5.             STUDENT_TBL.STUDENT_NAME = #{studentName},      
  6.         </if>     
  7.         <if test="studentSex!=null and studentSex!='' ">     
  8.             STUDENT_TBL.STUDENT_SEX = #{studentSex},      
  9.         </if>     
  10.         <if test="studentBirthday!=null ">     
  11.             STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
  12.         </if>     
  13.         <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
  14.             STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
  15.         </if>     
  16.     </set>     
  17.     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
  18. </update>     

還有 choose 標籤 相當於 拼接語句的時候用 switch case

  1. update id="updateStudent" parameterType="StudentEntity">     
  2.     UPDATE STUDENT_TBL      
  3.     <set>     
  4.         <if test="studentName!=null and studentName!='' ">     
  5.             STUDENT_TBL.STUDENT_NAME = #{studentName},      
  6.         </if>     
  7.         <if test="studentSex!=null and studentSex!='' ">     
  8.             STUDENT_TBL.STUDENT_SEX = #{studentSex},      
  9.         </if>     
  10.         <if test="studentBirthday!=null ">     
  11.             STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},      
  12.         </if>     
  13.         <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">     
  14.             STUDENT_TBL.CLASS_ID = #{classEntity.classID}      
  15.         </if>     
  16.     </set>     
  17.     WHERE STUDENT_TBL.STUDENT_ID = #{studentID};      
  18. </update>     

上面代碼都是有問題的, XML中的字符串最好都寫![CDATA[裏面]];不然><#%都可能報錯


3,優化

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章