Mybatis?在?insert?插入操作后返回主鍵?id的操作方法
Mybatis 在 insert 插入操作后返回主鍵 id
前提條件
假設(shè)我們這里有一個 Student 表,結(jié)構(gòu)如下
| sid | name | age |
|---|---|---|
| 101 | Jone | 18 |
| 102 | Jack | 20 |
| 103 | Tom | 28 |
其中主鍵 sid 是自增的,那么我們插入數(shù)據(jù)時就不用插入 sid,它會生成一個自增的 sid。
問題提出
這里有一個問題,我們執(zhí)行插入語句之后,并不能獲取到生成的 sid。
StudentDao 接口中的 insert 方法
boolean insertStudent(Student student);
StudentDao.xml 中的 insert 標(biāo)簽
<insert id="insertStudent" parameterType="Student">
insert into student(name, age)
VALUES (#{name} , #{age})
</insert>單元測試類中的方法
@Test
public void insertUser() {
Student student = new Student(0,"xxx", 28);
boolean flag = studentDao.insertStudent(student);
System.out.println(flag);
System.out.println(student);
}執(zhí)行后的結(jié)果


這里并不能獲取到生成的 sid,如果要獲取這個 sid,還要根據(jù) name 來查詢數(shù)據(jù)庫,而且 name 也需要是 unique 唯一性的。
那么,有沒有辦法讓我們能夠執(zhí)行插入語句后,直接獲取到生成的 sid 呢,當(dāng)然是有的。
解決方法
方法一
修改 StudentDao.xml 中的 insert 標(biāo)簽,配置 useGeneratedKeys 和 keyProperty
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="sid">
insert into student(name, age)
VALUES (#{name} , #{age})
</insert>說明:
1、useGeneratedKeys=“true” 表示給主鍵設(shè)置自增長。
2、keyProperty=“sid” 表示將自增長后的 Id 賦值給實體類中的 sid 字段。
運(yùn)行結(jié)果:成功返回了主鍵 sid


方法二(推薦)
修改 StudentDao.xml 中的 insert 標(biāo)簽,在 insert 標(biāo)簽中編寫 selectKey 標(biāo)簽
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="sid">
insert into student(name, age)
VALUES (#{name} , #{age})
</insert>說明:
1、< insert> 標(biāo)簽中沒有 resultType 屬性,但是 < selectKey> 標(biāo)簽是有的。
2、order=“AFTER” 表示先執(zhí)行插入語句,之后再執(zhí)行查詢語句。
3、keyProperty=“sid” 表示將自增長后的 Id 賦值給實體類中的 sid 字段。
4、SELECT LAST_INSERT_ID() 表示 MySQL 語法中查詢出剛剛插入的記錄自增長 Id。
運(yùn)行結(jié)果:成功返回了主鍵 sid


方法三
這種方法需要在一定條件下才能使用,就是 name 需要是 unique,不可重復(fù)的。
這樣才能在插入后,根據(jù) name 來查詢出主鍵 sid。
同樣是修改 StudentDao.xml 中的 insert 標(biāo)簽,在 insert 標(biāo)簽中編寫 selectKey 標(biāo)簽
<insert id="insertStudent" parameterType="Student">
insert into student(name, age)
VALUES (#{name} , #{age})
<selectKey keyProperty="sid" order="AFTER" resultType="int">
select sid from student where name = #{name}
</selectKey>
</insert>原理上面也講了,就是在執(zhí)行插入語句之后,再執(zhí)行查詢語句,將 sid 查出來。
不過我這里 name 并未設(shè)置 unique,所以不在這里進(jìn)行測試,有興趣可以自行測試。
補(bǔ)充知識點:
mybatis中Insert語句如何返回插入的主鍵
方法一:
mapper.java
/**
* 添加部門
* @param department
* @return
*/
Integer insertDep(Department department);xml
<insert id="insertDep" parameterType="com.example.pojo.entity.Department">
<selectKey keyProperty="departmentId" order="AFTER" resultType="java.lang.Integer ">
select LAST_INSERT_ID()
</selectKey>
insert into sys_department(name,parentId,enabled,isParent)
VALUES(#{name},#{parentId},1,0)
</insert>其中:
selectKey標(biāo)簽:將插入到數(shù)據(jù)庫的某條記錄的主鍵,返回到指定對象(user)對應(yīng)屬性中。
keyProperty: 指定返回的主鍵,存儲在對象中(user)的哪個屬性
order:相對于insert語句,selectKey標(biāo)簽中的sql的執(zhí)行順序。由于mysql的自增原理,執(zhí)行完insert語句之后才將主鍵生成,所以這里selectKey的執(zhí)行順序為after。
resultType: 返回的主鍵對應(yīng)的JAVA類型
LAST_INSERT_ID(): 是mysql的函數(shù),返回auto_increment自增列新記錄id值。
方法二
使用:
useGeneratedKeys="true" keyProperty="departmentId"
<insert id="insertDep" parameterType="com.example.pojo.entity.Department" useGeneratedKeys="true" keyProperty="departmentId">
insert into sys_department(departmentId,name,parentId,enabled,isParent)
VALUES(uuid(),#{name},#{parentId},1,0)
</insert>到此這篇關(guān)于mybatis中Insert語句如何返回插入的主鍵的文章就介紹到這了,更多相關(guān)mybatis Insert返回插入的主鍵內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java使用POI批量導(dǎo)入excel數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了java使用POI批量導(dǎo)入excel數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
elasticsearch如何根據(jù)條件刪除數(shù)據(jù)
Elasticsearch是一個基于Apache Lucene?的開源搜索引擎,無論在開源還是專有領(lǐng)域,Lucene 可以被認(rèn)為是迄今為止最先進(jìn)、性能最好的、功能最全的搜索引擎庫,這篇文章主要介紹了elasticsearch如何根據(jù)條件刪除數(shù)據(jù),需要的朋友可以參考下2023-03-03
快速學(xué)會Dubbo的配置環(huán)境及相關(guān)配置
本文主要講解Dubbo的環(huán)境與配置,文中運(yùn)用大量代碼和圖片講解的非常詳細(xì),需要學(xué)習(xí)或用到相關(guān)知識的小伙伴可以參考這篇文章2021-09-09

