myBatis mySql 批量插入
<insert id="batchInsert" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List" >
INSERT INTO `oauth_client_method`
(appkey,method)
VALUES
<foreach collection="list" item="item" index="index" separator="," >
(#{item.appkey},#{item.method})
</foreach>
</insert>
---------------------------------------------------------------------------------------------------------------------------------------------------
mybatis ,mysql
插入的时候,主键需要特殊处理
主键 且 自增长
上面的代码
useGeneratedKeys="true" keyProperty="id"
在单条插入时OK,但是批量插入时应该去掉,否则会报错,因为只能找到list找不到id
正确如下:
<insert id="saveBatch" parameterType="java.util.List">
INSERT INTO cerins_calendar_manager
(user_id,user_name,phase,grade,subject,publisherVer,ctime,utime)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.user_id},#{item.user_name},
#{item.phase},#{item.grade},#{item.subject},#{item.publisherVer},
now(),now())
</foreach>
</insert>