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>

 

posted @ 2018-08-04 11:41  WhoKnows1  阅读(313)  评论(0编辑  收藏  举报