异常现象

insert 通过 mybatis 以下语法给领域类 赋予的 id 值为0. 后续根据主键的update操作失效。且无异常抛出

<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      SELECT LAST_INSERT_ID()
    </selectKey>
  • 1
  • 2
  • 3

产生原因

因为 执行插入语句 是在 写库 , 而SELECT LAST_INSERT_ID() 是在读库执行。

解决方案 ##

  • 添加 /FORCE_MASTER/ 注解 使该sql 走 master 节点,代码如下
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
      /*FORCE_MASTER*/
      SELECT LAST_INSERT_ID()
    </selectKey>
  • 1
  • 2
  • 3
  • 4
  • 添加事物

可以参考 :https://help.aliyun.com/document_detail/51073.html

相关文章:

  • 2021-07-30
  • 2022-12-23
  • 2022-12-23
  • 2021-10-30
  • 2021-07-26
  • 2021-12-24
  • 2022-12-23
猜你喜欢
  • 2021-07-02
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2021-07-23
相关资源
相似解决方案