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 ...
随机推荐
- Dart函数方法
/* 内置方法/函数: print(); 自定义方法: 自定义方法的基本格式: 返回类型 方法名称(参数1,参数2,...){ 方法体 return 返回值; } */ void printInfo( ...
- Python 线程,with的作用(自动获取和释放锁Lock)
Python 线程,with的作用(自动获取和释放锁Lock) import threading import time num= #全局变量多个线程可以读写,传递数据 mutex=threading ...
- Spring cloud微服务安全实战-3-13重构代码
让代码同时支持两种方式,登陆访问和带着请求头的token访问也可以. 首先做代码的重构 这里改成getSession() 改成这样以后会有一个问题,我用httpBasic登陆成功以后,我的用户信息放在 ...
- Qt编写气体安全管理系统5-数据监控
一.前言 本项目对设备的监控有四种视图模式,可以任意切换,数据监控.地图监控.设备监控.曲线监控,其中数据监控是最常用的,所以在主界面导航中也排在第一位,综合观察分析了很多气体安全或者组态监控软件,大 ...
- 利用SynchronizationContext.Current在线程间同步上下文(转)
https://blog.csdn.net/iloli/article/details/16859605 简而言之就是允许一个线程和另外一个线程进行通讯,SynchronizationContext在 ...
- LeetCode_257. Binary Tree Paths
257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...
- 【JS新手教程】JS中的split()方法,拆分字符串
该方法具体如代码和图中的注释,直接在语句后面加注释了.格式:要拆分的字符串.split(拆分依据的字符)例如该文中的例子,拆分人名,电话,地址.该文中用了个文本框,文本框中需要输入的格式是:人名,电话 ...
- 08点睛Spring MVC4.1-Spring MVC的配置(含自定义HttpMessageConverter)
8.1 配置 Spring MVC的配置是通过继承WebMvcConfigurerAdapter类并重载其方法实现的; 前几个教程已做了得配置包括 01点睛Spring MVC 4.1-搭建环境 配置 ...
- 【ARTS】01_40_左耳听风-201900812~201900818
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- 【OpenGL开发】GLUT/freeglut 是什么? OpenGL 和它们有什么关系?
GLUT/freeglut 是什么? OpenGL 和它们有什么关系? OpenGL只是一个标准,它的实现一般自带在操作系统里,只要确保显卡驱动足够新就可以使用.如果需要在程序里直接使用OpenGL, ...