mybatis+oracle,mysql,sql server批处理
目录
3.4 通过调用存储过程批量处理 proc_user为存储过程
1 oracle 批处理
1.1 批量插入
<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="false">
insert into user
(id, name )
<foreach collection="list" item="item" index="index" separator="union all" >
(select #{item.id,jdbcType=DECIMAL},
#{item.name,jdbcType=VARCHAR}
from dual )
</foreach>
</insert>
1.2 批量更新
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
update user
<set>
<if test="item.name != null">
name = #{item.name,jdbcType=VARCHAR},
</if>
</set>
where id = #{item.id,jdbcType=DECIMAL}
</foreach>
</update>
2 sql server
2.1批量插入
<insert id="batchInsert" parameterType="java.util.List">
insert into user
(id,name)
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.id},#{item.name})
</foreach>
</insert>
2.2 批量更新
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
UPDATE user
<set>
name=#{item.name}
</set>
where id=#{item.id}
</foreach>
</update>
3 mybatis+mysql批处理
注意:连接数据库url 后面添加allowMultiQueries=true 如jdbc:mysql://localhost:3306/test?allowMultiQueries=true
3.1 批量插入
<insert id="batchInsert" parameterType="java.util.List">
insert into user
(id,name)
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.id},#{item.name})
</foreach>
</insert>
3.2 批量更新
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update user
<set>
name=#{item.name}
</set>
where id=#{item.id}
</foreach>
</update>
3.3 通过List<Integer> ids 将表中某一列修改为固定值
<update id="batchUpdate" parameterType="java.util.List">
update middle_process_user_closed
set collected=1
where
id
<foreach collection="list" item="item" index="index" open="in (" close=")" separator=",">
#{item,jdbcType=INTEGER}
</foreach>
</update>
3.4 通过调用存储过程批量处理 proc_user为存储过程
<select id="callPro" statementType="CALLABLE" resultType="java.util.Map">
CALL proc_user()
</select>

本文详细介绍使用MyBatis框架进行Oracle、MySQL和SQLServer数据库的批量插入、更新操作,包括通过存储过程处理和修改表中列的固定值。文章提供具体XML映射文件示例,涵盖不同数据库特性和参数配置。
336

被折叠的 条评论
为什么被折叠?



