mybatis如何獲取剛剛新插入數(shù)據(jù)的主鍵值id
mybatis獲取剛剛新插入數(shù)據(jù)的主鍵值id
插入操作后,立馬返回主鍵Id。查閱了很多資料,用keyProperty和useGeneratedKeys屬性。
useGeneratedKeys 參數(shù)只針對 insert 語句生效,默認(rèn)為 false。
當(dāng)設(shè)置為 true 時,表示如果插入的表以自增列為主鍵,則允許 JDBC 支持自動生成主鍵,并可將自動生成的主鍵返回。
主要是以這樣的方式:
<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只能告訴它,在哪個實(shí)體類中,在Mybatis Mapper文件中添加屬性 “useGeneratedKeys”和“keyProperty”,其中 keyProperty 是 Java 對象的屬性名,而不是表的字段名。
但是還是沒生效,我開始懷疑是我自增id是long類型,但實(shí)際上,并不是的,是我在mapper接口類中,加了這樣一個注釋
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ù)庫中主鍵的值是通過自動遞增的方式創(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會自動將獲取的主鍵封裝給此屬性。
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é)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring遠(yuǎn)程加載配置的實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Spring遠(yuǎn)程加載配置的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-03-03
SpringBoot項(xiàng)目中使用Sharding-JDBC實(shí)現(xiàn)讀寫分離的詳細(xì)步驟
Sharding-JDBC是一個分布式數(shù)據(jù)庫中間件,它不僅支持?jǐn)?shù)據(jù)分片,還可以輕松實(shí)現(xiàn)數(shù)據(jù)庫的讀寫分離,本文介紹如何在Spring Boot項(xiàng)目中集成Sharding-JDBC并實(shí)現(xiàn)讀寫分離的詳細(xì)步驟,需要的朋友可以參考下2024-08-08
SpringMVC Mock測試實(shí)現(xiàn)原理及實(shí)現(xiàn)過程詳解
這篇文章主要介紹了SpringMVC Mock測試實(shí)現(xiàn)原理及實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10
java使用httpclient發(fā)送post請求示例
這篇文章主要介紹了java使用httpclient發(fā)送post請求示例,依賴JSON、HTTPClient等jar包,需要的朋友可以參考下2014-02-02
java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn)
這篇文章給大家分享java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn),需要獲取要賦值給二維碼的鏈接后綴,通過設(shè)置二維碼的訪問路徑等一系列操作,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧2021-06-06

