mybatis如何獲取剛剛新插入數(shù)據(jù)的主鍵值id
mybatis獲取剛剛新插入數(shù)據(jù)的主鍵值id
插入操作后,立馬返回主鍵Id。查閱了很多資料,用keyProperty和useGeneratedKeys屬性。
useGeneratedKeys 參數(shù)只針對 insert 語句生效,默認為 false。
當設置為 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只能告訴它,在哪個實體類中,在Mybatis Mapper文件中添加屬性 “useGeneratedKeys”和“keyProperty”,其中 keyProperty 是 Java 對象的屬性名,而不是表的字段名。
但是還是沒生效,我開始懷疑是我自增id是long類型,但實際上,并不是的,是我在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標簽中加入 <selectKey> 標簽
<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("新插入的學生的id為:"+s.getId());
sqlSession.commit();
sqlSession.close();
}注意:
- selectKey標簽中的 select LAST_INSERT_ID() 語句就能獲取生成的主鍵
- selectKey標簽中的keyProperty屬性就是主鍵名,MyBatis會自動將獲取的主鍵封裝給此屬性。
order的值有兩種:BEFORE、AFTER
- BEFORE:先獲取主鍵,然后執(zhí)行insert; 比如 Oracle數(shù)據(jù)庫。
- AFTER:先執(zhí)行insert,然后獲取主鍵; 比如 MySql數(shù)據(jù)庫。
方法二
在select標簽中加入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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解@Autowired(required=false)注入注意的問題
這篇文章主要介紹了@Autowired(required=false)注入注意的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
解決SpringBoot中的Scheduled單線程執(zhí)行問題
在一次SpringBoot中使用Scheduled定時任務時,發(fā)現(xiàn)某一個任務出現(xiàn)執(zhí)行占用大量資源,會導致其他任務也執(zhí)行失敗,這篇文章主要介紹了SpringBoot中的Scheduled單線程執(zhí)行問題及解決方法,需要的朋友可以參考下2022-06-06
SpringBoot整合Swagger和Actuator的使用教程詳解
Swagger 是一套基于 OpenAPI 規(guī)范構(gòu)建的開源工具,可以幫助我們設計、構(gòu)建、記錄以及使用 Rest API。本篇文章主要介紹的是SpringBoot整合Swagger(API文檔生成框架)和SpringBoot整合Actuator(項目監(jiān)控)使用教程。感興趣的朋友一起看看吧2019-06-06
java實現(xiàn)token無感刷新+處理并發(fā)的后端方案
在Web應用中,Token用于身份驗證和會話管理,但當Token過期時,可能會導致用戶在提交表單或進行操作時突然被重定向到登錄頁面,本文就來介紹一下java實現(xiàn)token無感刷新+處理并發(fā)的后端方案,感興趣的可以了解一下2024-11-11

