写一个需求,需要批量插入数据,并且主键自增长,开始用了这个语句,可是只能插入一条
<insert id="batchInsert" parameterType="java.util.List" keyProperty="id">
insert into score_manage
(
`exam_number`,
`password`,
`name`,
`class_and_grade`,
`semester`,
`subject`,
`grade`,
`score`,
`deleted`,
`deleted_time`,
`tenant_id`,
`create_user_id`,
`update_user_id`,
`create_time`,
`update_time`
)
<foreach collection="list" item="item" index="index" separator="union">
select
#{item.examNumber},
#{item.password},
#{item.name},
#{item.classAndGrade},
#{item.semester},
#{item.subject},
#{item.grade},
#{item.score},
#{item.deleted},
#{item.deletedTime},
#{item.tenantId},
#{item.createUserId},
#{item.updateUserId},
now(),
now()
from dual
</foreach>
</insert>
后来换了下边这个,成功了
<insert id="batchInsert" parameterType="java.util.List" keyProperty="id">
insert into score_manage
(
`exam_number`,
`password`,
`name`,
`class_and_grade`,
`semester`,
`subject`,
`grade`,
`score`,
`deleted`,
`deleted_time`,
`tenant_id`,
`create_user_id`,
`update_user_id`,
`create_time`,
`update_time`
)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.examNumber},
#{item.password},
#{item.name},
#{item.classAndGrade},
#{item.semester},
#{item.subject},
#{item.grade},
#{item.score},
#{item.deleted},
#{item.deletedTime},
#{item.tenantId},
#{item.createUserId},
#{item.updateUserId},
now(),
now()
)
</foreach>
</insert>
|