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,优化