mybatis如何獲取剛剛新插入數(shù)據(jù)的主鍵值id
mybatis獲取剛剛新插入數(shù)據(jù)的主鍵值id
插入操作后,立馬返回主鍵Id。查閱了很多資料,用keyProperty和useGeneratedKeys屬性。
useGeneratedKeys 參數(shù)只針對(duì) insert 語句生效,默認(rèn)為 false。
當(dāng)設(shè)置為 true 時(shí),表示如果插入的表以自增列為主鍵,則允許 JDBC 支持自動(dòng)生成主鍵,并可將自動(dòng)生成的主鍵返回。
主要是以這樣的方式:
<insert id="insertFileRecord" useGeneratedKeys="true" keyProperty="id"> insert xxx </insert>
但是沒有生效,于是我找用了useGeneratedKeys="true"的寫法,發(fā)現(xiàn)我少寫了
<insert id="insertFileRecord" parameterType="FileRecord" useGeneratedKeys="true" keyProperty="id"> insert xxx </insert>
parameterType只能告訴它,在哪個(gè)實(shí)體類中,在Mybatis Mapper文件中添加屬性 “useGeneratedKeys”和“keyProperty”,其中 keyProperty 是 Java 對(duì)象的屬性名,而不是表的字段名。
但是還是沒生效,我開始懷疑是我自增id是long類型,但實(shí)際上,并不是的,是我在mapper接口類中,加了這樣一個(gè)注釋
int insertFileRecord(@Param("fileRecord") FileRecord fileRecord);
并且在insert語句中fileRecord.fileName,去掉@Param,終于看到id了,
最后附上完整的寫法:
mapper接口:
/** * @Description: 新增數(shù)據(jù) * @Author: wj * @param: cc.jz.work.entity.FileRecord * @Return: 影響行數(shù) * @Date: 2022-03-31 10:37:18 */ int insertFileRecord(FileRecord fileRecord);
xml:
<insert id="insertFileRecord" parameterType="cc.jz.work.entity.FileRecord" useGeneratedKeys="true" keyProperty="id"> insert into file_record <trim prefix="(" suffix=")" suffixOverrides=","> <if test="fileAddress != null and fileAddress != ''"> file_address, </if> <if test="fileName != null and fileName != ''"> file_name, </if> <if test="uploadUserId != null"> upload_user_id, </if> <if test="uploadTime != null"> upload_time, </if> <if test="status != null and status != ''"> status, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="fileAddress != null and fileAddress != ''"> #{fileAddress,jdbcType=VARCHAR}, </if> <if test="fileName != null and fileName != ''"> #{fileName,jdbcType=VARCHAR}, </if> <if test="uploadUserId != null"> #{uploadUserId,jdbcType=INTEGER}, </if> <if test="uploadTime != null"> #{uploadTime,jdbcType=TIMESTAMP}, </if> <if test="status != null and status != ''"> #{status,jdbcType=VARCHAR}, </if> </trim> </insert>
mybatis中插入記錄獲取主鍵值
在數(shù)據(jù)庫中主鍵的值是通過自動(dòng)遞增的方式創(chuàng)建的,然而在通過mybatis插入數(shù)據(jù)之后想獲取插入數(shù)據(jù)的主鍵值可以通過下面兩種方法:
方法一
在xml的配置文件中的insert標(biāo)簽中加入 <selectKey>
標(biāo)簽
<insert id="insert"> <selectKey keyProperty="id" resultType="int" order="AFTER"> select LAST_INSERT_ID() </selectKey> insert into t_student(uname,pass,stu_name,gender,birthdate,score) values(#{uname},#{pass},#{stu_name},#{gender},#{birthdate},#{score}) </insert>
public static void doInsert() { SqlSession sqlSession = MybatisUtil.getSqlSession(); Student s = new Student(); s.setBirthdate("2020-01-01"); s.setGender('1'); s.setPass("123"); s.setScore(69.3); s.setStu_name("張四"); s.setUname("jack"); int i = sqlSession.insert("student.insert",s); System.out.println("i="+i); System.out.println("新插入的學(xué)生的id為:"+s.getId()); sqlSession.commit(); sqlSession.close(); }
注意:
- selectKey標(biāo)簽中的 select LAST_INSERT_ID() 語句就能獲取生成的主鍵
- selectKey標(biāo)簽中的keyProperty屬性就是主鍵名,MyBatis會(huì)自動(dòng)將獲取的主鍵封裝給此屬性。
order的值有兩種:BEFORE、AFTER
- BEFORE:先獲取主鍵,然后執(zhí)行insert; 比如 Oracle數(shù)據(jù)庫。
- AFTER:先執(zhí)行insert,然后獲取主鍵; 比如 MySql數(shù)據(jù)庫。
方法二
在select標(biāo)簽中加入useGeneratedKeys=“true” keyProperty=“id”
<insert id="insert2" useGeneratedKeys="true" keyProperty="id"> insert into t_student(uname,pass,stu_name,gender,birthdate,score) values(#{uname},#{pass},#{stu_name},#{gender},#{birthdate},#{score}) </insert>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解@Autowired(required=false)注入注意的問題
這篇文章主要介紹了@Autowired(required=false)注入注意的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04單元測(cè)試 @mock與@SpringBootTest的使用
這篇文章主要介紹了單元測(cè)試 @mock與@SpringBootTest的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10解決SpringBoot中的Scheduled單線程執(zhí)行問題
在一次SpringBoot中使用Scheduled定時(shí)任務(wù)時(shí),發(fā)現(xiàn)某一個(gè)任務(wù)出現(xiàn)執(zhí)行占用大量資源,會(huì)導(dǎo)致其他任務(wù)也執(zhí)行失敗,這篇文章主要介紹了SpringBoot中的Scheduled單線程執(zhí)行問題及解決方法,需要的朋友可以參考下2022-06-06java實(shí)現(xiàn)的2048游戲完整實(shí)例
這篇文章主要介紹了java實(shí)現(xiàn)的2048游戲,結(jié)合完整實(shí)例形式分析了java實(shí)現(xiàn)2048游戲功能的相關(guān)數(shù)值運(yùn)算、swing組件布局、事件響應(yīng)等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01SpringBoot整合Swagger和Actuator的使用教程詳解
Swagger 是一套基于 OpenAPI 規(guī)范構(gòu)建的開源工具,可以幫助我們?cè)O(shè)計(jì)、構(gòu)建、記錄以及使用 Rest API。本篇文章主要介紹的是SpringBoot整合Swagger(API文檔生成框架)和SpringBoot整合Actuator(項(xiàng)目監(jiān)控)使用教程。感興趣的朋友一起看看吧2019-06-06java實(shí)現(xiàn)token無感刷新+處理并發(fā)的后端方案
在Web應(yīng)用中,Token用于身份驗(yàn)證和會(huì)話管理,但當(dāng)Token過期時(shí),可能會(huì)導(dǎo)致用戶在提交表單或進(jìn)行操作時(shí)突然被重定向到登錄頁面,本文就來介紹一下java實(shí)現(xiàn)token無感刷新+處理并發(fā)的后端方案,感興趣的可以了解一下2024-11-11一文帶你快速學(xué)會(huì)JDBC及獲取連接的五種方式
JDBC(Java Database Connectivity)是一個(gè)獨(dú)立于特定數(shù)據(jù)庫管理系統(tǒng)、通用的SQL數(shù)據(jù)庫存取和操作的公共接口,下面這篇文章主要給大家介紹了關(guān)于如何通過一文帶你快速學(xué)會(huì)JDBC及獲取連接的五種方式,需要的朋友可以參考下2022-09-09