MySql 使用foreach标签 批量插入list中数据,主键自增,插入

来源:互联网 发布:网络投票 编辑:程序博客网 时间:2024/06/14 06:40

方法一

<insert id="add" parameterType="EStudent">

  <foreach collection="list" item="item" index="index" separator=";" >
    INSERT INTO TStudent(name,age) VALUES(#{item.name}, #{item.age})

  </foreach>

</insert>


方法二

<insert id="add" parameterType="EStudent">

   INSERT INTO TStudent(name,age)VALUES

  <foreach collection="list" item="item" index="index" separator=";" >
  (#{item.name}, #{item.age})

  </foreach>


</insert>


使用方法二时:

1.item为每一个对象

2.  <foreach>标签中不可使用   keyProperty="id"   否则sql语句 或报错,找不到 id





0 0
原创粉丝点击