集群环境下mysql自增主键重复问题
<insert id="insert" parameterType="Student">
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
insert into student (id, stu_name)
values (#{id,jdbcType=BIGINT}, #{stu_name,jdbcType=VARCHAR})
</insert>
使用mybatis+mysql时,我们一般这样设置,获取自增主键id,这个是通过反射,将主键id设置到对应实体中,在单机版中没有任何问题,但是在集群环境会报 Duplicate entry '' for key 'PRIMARY'
解决办法
<insert id="insert" parameterType="Student">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
insert into student (id, stu_name)
values (#{id,jdbcType=BIGINT}, #{stu_name,jdbcType=VARCHAR})
</insert>
将order="BEFORE" 修改为 order="AFTER"