myBatis 批量操作
2013-12-25 13:07
411 查看
在批量操作数量级比较高时,尽量不要在service 层循环访问数据库,这会占用数据库的连接数
mybatis 中提供的批量操作函数 foreach ,通过生成批量处理sql 语句来实现批量操作
一 批量插入: (适用oracle)
<insert id="plInsert">
insert into plstu (id,name,sex)
<foreach collection="stus" item="stu" index="index" separator="union">
select #{stu.id},#{stu.name},#{stu.sex} from dual
</foreach>
</insert>
<!-- 批量删除 -->
<delete id="pldelete">
delete from plstu where name in
<foreach collection="stus" item="stu" index="index" separator="," open="(" close=")" >
#{stu.name}
</foreach>
</delete>
或者不要 open close 直接将foreach 标签() 起来
<!-- 批量更新 -->
<update id="plupdate">
<foreach collection="stus" item="stu" index="index" separator=";" open="begin" close=";end;" >
update plstu set name = #{stu.sex},sex = #{stu.name} where id = #{stu.id}
</foreach>
</update>
批量的本质。。。还是生成批量的sql处理语句交给数据库操作,只是减少了程序与数据库的互动次数
----------------------------oracle 批量操作--------------------------------------------------
1 批量插入
mybatis 中提供的批量操作函数 foreach ,通过生成批量处理sql 语句来实现批量操作
一 批量插入: (适用oracle)
<insert id="plInsert">
insert into plstu (id,name,sex)
<foreach collection="stus" item="stu" index="index" separator="union">
select #{stu.id},#{stu.name},#{stu.sex} from dual
</foreach>
</insert>
<!-- 批量删除 -->
<delete id="pldelete">
delete from plstu where name in
<foreach collection="stus" item="stu" index="index" separator="," open="(" close=")" >
#{stu.name}
</foreach>
</delete>
或者不要 open close 直接将foreach 标签() 起来
<!-- 批量更新 -->
<update id="plupdate">
<foreach collection="stus" item="stu" index="index" separator=";" open="begin" close=";end;" >
update plstu set name = #{stu.sex},sex = #{stu.name} where id = #{stu.id}
</foreach>
</update>
批量的本质。。。还是生成批量的sql处理语句交给数据库操作,只是减少了程序与数据库的互动次数
----------------------------oracle 批量操作--------------------------------------------------
1 批量插入
<insert id="saveEconomicByRows" parameterType="List"> insert into KJ_ECONOMIC_AWARD ( id, proid, nd, xzlr, xzss, cswh, jzze) select seq_kj_other_id.nextval,A.* from ( <foreach collection="list" item="row" index="index" separator="union"> select #{row.proid,jdbcType=NUMERIC} as proid, #{row.nd} as nd, #{row.xzlr} as xzlr, #{row.xzss} as xzss, #{row.cswh} as cswh, #{row.jzze} as jzze from dual </foreach> )A </insert>
相关文章推荐
- mybatis批量操作oracle
- MyBatis魔法堂:Insert操作详解(返回主键、批量插入)
- MyBatis批量操作(mysql)
- mybatis 批量 操作数据
- MyBatis批量操作报错:Parameter 'xxxList' not found. Avail
- Mybatis之批量更新操作
- MyBatis的几种批量操作
- Mybatis实现批量删除操作
- MyBatis的几种批量操作
- mybatis 传入map 进行批量更新操作示例
- Mybatis 批量操作
- Mybatis之批量更新操作
- MyBatis批量操作报错:Parameter 'xxxList' not found. Available parameters are [list]
- mybatis增删改查与批量操作
- mybatis批量操作
- MyBatis魔法堂:Insert操作详解(返回主键、批量插入)
- mybatis的批量操作
- mybatis以map的方式批量插入list集合数据操作步骤
- MyBatis的几种批量操作
- mybatis批量操作问题总结