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>


mybatis获取刚刚插入到数据库的数据的id(转载)的更多相关文章
- mybatis获取批量插入的主键自增id
一.写一个实体类 public class UserInfo { private long userId; private String userAccount; private String use ...
- myBatis获取批量插入数据的主键id
在myBatis中获取刚刚插入的数据的主键id是比较容易的 , 一般来说下面的一句话就可以搞定了 , 网上也有很多相关资料去查. @Options(useGeneratedKeys = true, k ...
- 获取刚刚插入表格的这条信息的自增ID
获取刚刚插入表格的这条信息的自增ID var conn=getConnection(); var msql="INSERT INTO " + table +" (&quo ...
- Mybatis中文模糊查询,数据库中有数据,但无结果匹配
1.Mybatis中文模糊查询,数据库中有数据,但无结果匹配 1.1 问题描述: Mybatis采用中文关键字进行模糊查询,sql语句配置无误,数据库有该数据,且无任何报错信息,但无查询结果 1.2 ...
- 数据库存入数据后id保持不变,或者直接报错
数据库存入数据后id保持不变,且添加的数据一直在进行覆盖 或者直接报错 数据库存入数据后id保持不变,且添加的数据一直在进行覆盖 原因是: 之前注释掉了loadimage();在该函数中含有建立新的记 ...
- mybatis获取insert插入之后的id
一.为什么要获取insert的id 写了测试类测试插入,插入之后用select查询出来进行Assert 插入成功后,不管Select对比的结果成功还是失败,都希望删除掉测试插入的结果 二.运行环境 m ...
- mybatis获得刚刚插入的自增的值
转自这里 在http://blog.csdn.net/zhangwenan2010/article/details/7579191 介绍了MyBatis 3 的配置过程, 其中,Product 类 ...
- mysql获取刚插入(添加)记录的自动编号id
我们在写数据库程序的时候,经常会需要获取某个表中的最大序号数, 一般情况下获取刚插入的数据的id,使用select max(id) from table 是可以的.但在多线程情况下,就不行了. 下面介 ...
- postgresql 获取刚刚插入的数据主键id
postgresql不支持last_insert_id()方法,恶心到啦: 不过还好它有其他的解决方案: 创建一个测试数据表: CREATE TABLE test.test18 ( id serial ...
随机推荐
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战
笔记 4.Redis工具类封装讲解和实战 简介:高效开发方式 Redis工具类封装讲解和实战 1.常用客户端 https://redisdesktop.com/download ...
- Android开发--IntentService的用法,你错过了什么
Android开发--IntentService的用法,你错过了什么 . 本文链接:https://blog.csdn.net/smbroe/article/details/45009721 Inte ...
- flutter 页面布局 Paddiing Row Column Expanded 组件
Flutter Paddiing 组件 在 html 中常见的布局标签都有 padding 属性,但是 Flutter 中很多 Widget 是没有 padding 属 性.这个时候我们可以用 Pad ...
- openresty开发系列27--openresty中封装redis操作
openresty开发系列27--openresty中封装redis操作 在关于web+lua+openresty开发中,项目中会大量操作redis, 重复创建连接-->数据操作-->关闭 ...
- 泡泡一分钟:Fast and Robust Initialization for Visual-Inertial SLAM
张宁 Fast and Robust Initialization for Visual-Inertial SLAM链接:https://pan.baidu.com/s/1cdkuHdkSi9x7l ...
- bladex调用网关使用oauth2统一获取授权
一:启动网关服务(需要启动其它服务,如:AuthApplication .UserApplication.BladeLogApplication,及自己添加的代码服务) 二:http://localh ...
- springboot中使用mybatis的分页插件pageHelper
首先在pom.xml中配置 <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot ...
- hadoop(四)MapReduce
如果将 Hadoop 比做一头大象,那么 MapReduce 就是那头大象的电脑.MapReduce 是 Hadoop 核心编程模型.在 Hadoop 中,数据处理核心就是 MapReduce 程序设 ...
- js禁止退出当前页面
禁止用户退出网页,就一个添加窗体历史状态方法,代码很简单,但是建议大家不要去使用,会给用户带来不友好的体验,我这里只是学习这种方法,看到自己代码上有也知道在哪里改变这些代码,所以分享一下: 实现原理: ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...