mybatis批量插入,用oracle序列做为主键的解决方案

在使用mybatis进行批量插入时,使用oracle的自动增长序列作为主键
mapper.xml内容

<!-- 批量插入 -->
<insert id="insertSalconfigAfter" parameterType="java.util.List">
	<![CDATA[
	   INSERT INTO TB_DEPARTMENT (id,s1,s2,s3,s4,s5,s6)
	]]>
	select TB_MY_SEQUENCE.NEXTVAL,m.* from (
<foreach collection="list" item="item" index="index" separator="union all">
	     select
		#{item.s1,jdbcType=VARCHAR},
		#{item.s2,jdbcType=VARCHAR},
		#{item.s3,jdbcType=VARCHAR},
		#{item.s4,jdbcType=VARCHAR},
		#{item.s5,jdbcType=VARCHAR},
		#{item.s6,jdbcType=VARCHAR}
              from dual
</foreach>
	) m
</insert>

java调用代码:
List<TbDepartment> list= new ArrayList<TbDepartment>();
sqlSessionTemplate.insert("insertSalconfigAfter", list);

猜你喜欢

转载自wchao226.iteye.com/blog/2251995