Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate ent
程序员文章站
2022-04-05 07:50:23
...
错误原因:
代码中遍历插入数据,报错主键重复,mybatis在默认事务中,一个事物执行多条插入语句,mybatis没办法获取插入第一条的主键,我的代码中添加了selectKey标签,所以删除select标签即可解决不能批量插入问题
<insert id="insertSelective" parameterType="com.reada.pojo.ReadaUserTag" >
<selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into RDA_UC_TAG
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="userId != null" >
user_id,
</if>
<if test="tagId != null" >
tag_id,
</if>
<if test="tagName != null" >
tag_name,
</if>
<if test="countNum != null" >
count_num,
</if>
<if test="createtime != null" >
createtime,
</if>
<if test="updatetime != null" >
updatetime,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="userId != null" >
#{userId,jdbcType=INTEGER},
</if>
<if test="tagId != null" >
#{tagId,jdbcType=INTEGER},
</if>
<if test="tagName != null" >
#{tagName,jdbcType=VARCHAR},
</if>
<if test="countNum != null" >
#{countNum,jdbcType=INTEGER},
</if>
<if test="createtime != null" >
#{createtime,jdbcType=TIMESTAMP},
</if>
<if test="updatetime != null" >
#{updatetime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
转载请加链接,有不同意见请留言
下一篇: 记录:Vue移动端项目优化过程