zoukankan      html  css  js  c++  java
  • mybatis获取刚刚插入到数据库的数据的id(转载)

    原文地址:https://blog.csdn.net/hehuihh/article/details/82800739

    我用的是第一种写法,直接把代码copy到insert标签里(id要是自增id)

    写法如下:

    第一种写法:

    第二种写法:

    第一种写法详解:

    keyProperty属性表示要查询的主键的名字,就是主键字段对应实体类的属性。

    order属性有两个值,即after,before;after表示在insert之后执行SELECT LAST_INSERT_ID(),一般用于主键自增时,得到的就是自增长生成的id,而before表示在insert之前执行SELECT LAST_INSERT_ID(),一般用于非自增主键,得到的是传入的对象中主键的值,一般是用户生成的uuid。

    resultType属性表示主键的类型,一般要么是Integer,要么是String

    将该selectKey标签的内容放入insert标签语句中就ok了,例如:

    <insert id="insertSelective" parameterType="com.rkxch.studypaper.po.StoryComments">
              
            <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
              SELECT LAST_INSERT_ID()
            </selectKey>
            
            insert into story_comments
            <trim prefix="(" suffix=")" suffixOverrides=",">
              <if test="storyId != null">
                story_id,
              </if>
              <if test="userId != null">
                user_id,
              </if>
              <if test="isDisplayName != null">
                is_display_name,
              </if>
              <if test="isSupport != null">
                is_support,
              </if>
              <if test="likeCount != null">
                like_count,
              </if>
              <if test="auditUserId != null">
                audit_user_id,
              </if>
              <if test="auditStatus != null">
                audit_status,
              </if>
              <if test="auditTime != null">
                audit_time,
              </if>
              <if test="dislikeCount != null">
                dislike_count,
              </if>
              <if test="createTime != null">
                create_time,
              </if>
              <if test="updateTime != null">
                update_time,
              </if>
              <if test="commentsContent != null">
                comments_content,
              </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
              <if test="storyId != null">
                #{storyId,jdbcType=INTEGER},
              </if>
              <if test="userId != null">
                #{userId,jdbcType=INTEGER},
              </if>
              <if test="isDisplayName != null">
                #{isDisplayName,jdbcType=INTEGER},
              </if>
              <if test="isSupport != null">
                #{isSupport,jdbcType=INTEGER},
              </if>
              <if test="likeCount != null">
                #{likeCount,jdbcType=INTEGER},
              </if>
              <if test="auditUserId != null">
                #{auditUserId,jdbcType=INTEGER},
              </if>
              <if test="auditStatus != null">
                #{auditStatus,jdbcType=INTEGER},
              </if>
              <if test="auditTime != null">
                #{auditTime,jdbcType=TIMESTAMP},
              </if>
              <if test="dislikeCount != null">
                #{dislikeCount,jdbcType=INTEGER},
              </if>
              <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
              </if>
              <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
              </if>
              <if test="commentsContent != null">
                #{commentsContent,jdbcType=LONGVARCHAR},
              </if>
            </trim>
          </insert>

    运行效果:

     

    笔者的这个项目,数据库的表结构主键都是自增长,所以selectKey标签中order属性必须是AFTER,而且是大写。

    如果,将order属性改成BEFORE会怎样呢?

    运行效果如下:

    这里查询的主键id是对象的id的值,而不是自增长生成的id的值,对象的属性未赋值,自动初始化的值是0,所以此处主键的值为0,改成BEFORE取的就是对象主键的值。

    笔者习惯在写insert语句时将selectKey标签带上,方便使用。

    另外:再强调一下,AFTER     BEFORE必须大写

    否则,运行如下:

    第二种写法详解:

    在insert标签中加入useGeneratedKeys和keyProperty属性即可,即:useGeneratedKeys="true" keyProperty="id"

    例如:

        <insert id="insertSelective" parameterType="com.rkxch.studypaper.po.StoryComments" useGeneratedKeys="true" keyProperty="id">
            insert into story_comments
            <trim prefix="(" suffix=")" suffixOverrides=",">
              <if test="storyId != null">
                story_id,
              </if>
              <if test="userId != null">
                user_id,
              </if>
              <if test="isDisplayName != null">
                is_display_name,
              </if>
              <if test="isSupport != null">
                is_support,
              </if>
              <if test="likeCount != null">
                like_count,
              </if>
              <if test="auditUserId != null">
                audit_user_id,
              </if>
              <if test="auditStatus != null">
                audit_status,
              </if>
              <if test="auditTime != null">
                audit_time,
              </if>
              <if test="dislikeCount != null">
                dislike_count,
              </if>
              <if test="createTime != null">
                create_time,
              </if>
              <if test="updateTime != null">
                update_time,
              </if>
              <if test="commentsContent != null">
                comments_content,
              </if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
              <if test="storyId != null">
                #{storyId,jdbcType=INTEGER},
              </if>
              <if test="userId != null">
                #{userId,jdbcType=INTEGER},
              </if>
              <if test="isDisplayName != null">
                #{isDisplayName,jdbcType=INTEGER},
              </if>
              <if test="isSupport != null">
                #{isSupport,jdbcType=INTEGER},
              </if>
              <if test="likeCount != null">
                #{likeCount,jdbcType=INTEGER},
              </if>
              <if test="auditUserId != null">
                #{auditUserId,jdbcType=INTEGER},
              </if>
              <if test="auditStatus != null">
                #{auditStatus,jdbcType=INTEGER},
              </if>
              <if test="auditTime != null">
                #{auditTime,jdbcType=TIMESTAMP},
              </if>
              <if test="dislikeCount != null">
                #{dislikeCount,jdbcType=INTEGER},
              </if>
              <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
              </if>
              <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
              </if>
              <if test="commentsContent != null">
                #{commentsContent,jdbcType=LONGVARCHAR},
              </if>
            </trim>
          </insert>

     



  • 相关阅读:
    Dumps for Dummies Dump Analysis Tutorial
    WCF 学习资料
    winform 跨线程设置或读取控件的属性
    反射通过属性名得到属性的值
    C# 不用循环填充数组
    反射字符串调用方法
    使用反射打开窗体,并控制一个窗体只能打开一个
    绘制圆角窗体和圆角panel
    WinForm使用反射通过控件的name得到该控件
    winfrom 绘制圆形按钮
  • 原文地址:https://www.cnblogs.com/shuaimeng/p/11800429.html
Copyright © 2011-2022 走看看