原文地址: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>

mybatis获取刚刚插入到数据库的数据的id(转载)的更多相关文章

  1. mybatis获取批量插入的主键自增id

    一.写一个实体类 public class UserInfo { private long userId; private String userAccount; private String use ...

  2. myBatis获取批量插入数据的主键id

    在myBatis中获取刚刚插入的数据的主键id是比较容易的 , 一般来说下面的一句话就可以搞定了 , 网上也有很多相关资料去查. @Options(useGeneratedKeys = true, k ...

  3. 获取刚刚插入表格的这条信息的自增ID

    获取刚刚插入表格的这条信息的自增ID var conn=getConnection(); var msql="INSERT INTO " + table +" (&quo ...

  4. Mybatis中文模糊查询,数据库中有数据,但无结果匹配

    1.Mybatis中文模糊查询,数据库中有数据,但无结果匹配 1.1 问题描述: Mybatis采用中文关键字进行模糊查询,sql语句配置无误,数据库有该数据,且无任何报错信息,但无查询结果 1.2 ...

  5. 数据库存入数据后id保持不变,或者直接报错

    数据库存入数据后id保持不变,且添加的数据一直在进行覆盖 或者直接报错 数据库存入数据后id保持不变,且添加的数据一直在进行覆盖 原因是: 之前注释掉了loadimage();在该函数中含有建立新的记 ...

  6. mybatis获取insert插入之后的id

    一.为什么要获取insert的id 写了测试类测试插入,插入之后用select查询出来进行Assert 插入成功后,不管Select对比的结果成功还是失败,都希望删除掉测试插入的结果 二.运行环境 m ...

  7. mybatis获得刚刚插入的自增的值

    转自这里 在http://blog.csdn.net/zhangwenan2010/article/details/7579191   介绍了MyBatis 3 的配置过程, 其中,Product 类 ...

  8. mysql获取刚插入(添加)记录的自动编号id

    我们在写数据库程序的时候,经常会需要获取某个表中的最大序号数, 一般情况下获取刚插入的数据的id,使用select max(id) from table 是可以的.但在多线程情况下,就不行了. 下面介 ...

  9. postgresql 获取刚刚插入的数据主键id

    postgresql不支持last_insert_id()方法,恶心到啦: 不过还好它有其他的解决方案: 创建一个测试数据表: CREATE TABLE test.test18 ( id serial ...

随机推荐

  1. Docs-.NET-C#-指南-语言参考-关键字-内置类型-值类型:整型数值类型

    ylbtech-Docs-.NET-C#-指南-语言参考-关键字-内置类型-值类型:整型数值类型 1.返回顶部 1. 整型数值类型(C# 参考) 2019/10/22 “整型数值类型”是“简单类型”的 ...

  2. OpenGL ES3使用MSAA(多重采样抗锯齿)的方法

    昨晚花费了我2个多小时的时间终于把OpenGL ES3.0中的MSAA给搞定了.在OpenGL ES2.0中,Khronos官方没有引入标准的MSAA全屏抗锯齿的方法,而Apple则采用了自己的GL_ ...

  3. ISO/IEC 9899:2011 条款6.5.17——逗号操作符

    6.5.17 逗号操作符 语法 1.expression: assignment-expression expression    ,    assignment-expression 语义 2.一个 ...

  4. ckpt pb

    (t20190518) luo@luo-All-Series:/dev/disk_2019/mask_rcnn_20190518/Mask_RCNN_20190902/models/research$ ...

  5. 查看所使用的Linux系统是32位还是64 位的方法

    方法一:getconf LONG_BIT # getconf LONG_BIT 1 1 我的Linux是32位!!! 方法二:arch # arch 1 1 显示 i686 就是32位,显示 x86_ ...

  6. 转 Java连接Oracle数据库的简单示例

    https://www.cnblogs.com/joyny/p/11176643.html https://community.oracle.com/thread/4096458 import jav ...

  7. [转]如何更换 Ubuntu 18.04 LTS 的 GDM 登录界面背景

    链接地址:https://www.linuxprobe.com/ubuntu-gdm-login.html

  8. DataTable.NET 使用server-side processing

    https://datatables.net/examples/server_side/simple.html 當頁面上要顯示的數據在10萬筆以上時,可以使用server-side processin ...

  9. 用anaconda保证64位和32位的python共存

    conda info # 查看当前工作平台 set CONDA_FORCE_32BIT=1 # 切换到32位 set CONDA_FORCE_32BIT=0 # 切换到64位 conda create ...

  10. 【Axure8】利用中继器(Repeater)实现表格数据的增删改

    利用Repeater实现对Table数据的增删改操作. 先拖入必需的控件:rectangle.text field.droplist.button.table.repeater.具体信息如图. 为方便 ...