• 背景

mybatis没有关联保存的功能,所以主从表需要分开保存,这就涉及到主表保存后要再次获取主表ID的环节,以下介绍mybatis插入数据后返回其自增ID的两种方式

  • 方案

  1、sql获取

<insert id="insert" parameterType="com.erp.oa.entity.MessageCommentDO" >
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
      select LAST_INSERT_ID()
    </selectKey>
    insert into message_comment (ID, comment_man, comment_detail, 
      insert_time)
    values (#{id,jdbcType=INTEGER}, #{commentMan,jdbcType=INTEGER}, #{commentDetail,jdbcType=VARCHAR}, 
      #{insertTime,jdbcType=TIMESTAMP})
  </insert>
keyProperty="id"中的id对应实体中的主键

  2、mybatis标签属性获取

<insert id="insert" parameterType="com.erp.oa.entity.MessageCommentDO" useGeneratedKeys="true" keyProperty="id">
    insert into message_comment (ID, comment_man, comment_detail,
      insert_time)
    values (#{id,jdbcType=INTEGER}, #{commentMan,jdbcType=INTEGER}, #{commentDetail,jdbcType=VARCHAR}, 
      #{insertTime,jdbcType=TIMESTAMP})
  </insert>
keyProperty="id"中的id对应实体中的主键

相关文章:

  • 2021-10-03
  • 2021-06-02
  • 2021-10-30
  • 2022-12-23
  • 2021-05-18
  • 2021-08-14
  • 2021-11-20
  • 2022-12-23
猜你喜欢
  • 2021-08-17
  • 2022-12-23
  • 2021-11-02
  • 2021-12-26
  • 2021-04-09
  • 2022-12-23
相关资源
相似解决方案