欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate ent

程序员文章站 2022-04-05 07:50:23
...

错误原因:

代码中遍历插入数据,报错主键重复,mybatis在默认事务中,一个事物执行多条插入语句,mybatis没办法获取插入第一条的主键,我的代码中添加了selectKey标签,所以删除select标签即可解决不能批量插入问题
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate ent

<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>

转载请加链接,有不同意见请留言