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 標籤的使用
- <select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap">
- SELECT * from STUDENT_TBL ST
- <where>
- <if test="studentName!=null and studentName!='' ">
- ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')
- </if>
- <if test="studentSex!= null and studentSex!= '' ">
- AND ST.STUDENT_SEX = #{studentSex}
- </if>
- <if test="studentBirthday!=null">
- AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
- </if>
- <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">
- AND ST.CLASS_ID = #{classEntity.classID}
- </if>
- </where>
- </select>
set 標籤類似 where 用在 update xxx set 中
- update id="updateStudent" parameterType="StudentEntity">
- UPDATE STUDENT_TBL
- <set>
- <if test="studentName!=null and studentName!='' ">
- STUDENT_TBL.STUDENT_NAME = #{studentName},
- </if>
- <if test="studentSex!=null and studentSex!='' ">
- STUDENT_TBL.STUDENT_SEX = #{studentSex},
- </if>
- <if test="studentBirthday!=null ">
- STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
- </if>
- <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">
- STUDENT_TBL.CLASS_ID = #{classEntity.classID}
- </if>
- </set>
- WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
- </update>
還有 choose 標籤 相當於 拼接語句的時候用 switch case
- update id="updateStudent" parameterType="StudentEntity">
- UPDATE STUDENT_TBL
- <set>
- <if test="studentName!=null and studentName!='' ">
- STUDENT_TBL.STUDENT_NAME = #{studentName},
- </if>
- <if test="studentSex!=null and studentSex!='' ">
- STUDENT_TBL.STUDENT_SEX = #{studentSex},
- </if>
- <if test="studentBirthday!=null ">
- STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
- </if>
- <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' ">
- STUDENT_TBL.CLASS_ID = #{classEntity.classID}
- </if>
- </set>
- WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
- </update>
上面代碼都是有問題的, XML中的字符串最好都寫![CDATA[裏面]];不然><#%都可能報錯
3,優化