Mybatis返回插入主鍵id的方法
在mapper的xml文件中配置 useGeneratedKeys
以及 keyProperty 返回Id即可
<insert id="insertObject" useGeneratedKeys="true" keyProperty="id" parameterType="www.change.tm.model.Orders" > insert into orders <trim prefix="(" suffix=")" suffixOverrides=","> <if test="number!=null"> OrderNumber, </if> <if test="orderTime!=null"> orderTime, </if> </trim> values <trim prefix="(" suffix=")" suffixOverrides=","> <if test="number!=null"> #{number}, </if> <if test="orderTime!=null"> #{orderTime}, </if> </trim> </insert>
PS:Mybatis中insert中返回主鍵ID的方法
1、XyzMapper.xml
<insertid=“doSomething"parameterType="map"useGeneratedKeys="true"keyProperty=“yourId"> ... </insert>
或
<insert id=“doSomething" parameterType=“com.xx.yy.zz.YourClass" useGeneratedKeys="true" keyProperty=“yourId"> ... </insert>
2、XyzMapper.java
public int doSomething(Map<String, Object> parameters); or public int doSomething(YourClass c);
3、要在map或c中有一個字段名為yourId,Mybatis會自動把主鍵值賦給這個字段。
Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put(“yourId”, 1234); ... mapper.doSomething(parameters); System.out.println(“id of the field that is primary key” + parameters.get(“yourId"));
或
YourClass c = new YourClass(); ... mapper.doSomething(c); System.out.println(“id of the field that is primary key” + c.yourId);
好了,到此結(jié)束,希望對大家有所幫助!
相關(guān)文章
spring mvc4的日期/數(shù)字格式化、枚舉轉(zhuǎn)換示例
本篇文章主要介紹了spring mvc4的日期/數(shù)字格式化、枚舉轉(zhuǎn)換示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01spring boot中xalan引入報錯系統(tǒng)找不到指定的文件原因分析
這篇文章主要介紹了spring boot中xalan引入報錯系統(tǒng)找不到指定的文件,主要原因是內(nèi)嵌的tomcat9.0.36,本文給大家分享最新解決方法,需要的朋友可以參考下2023-08-08Java異常處理UncaughtExceptionHandler使用實例代碼詳解
當一個線程由于未捕獲異常即將終止時,Java虛擬機將使用thread . getuncaughtexceptionhandler()查詢線程的uncaughtException處理程序,并調(diào)用處理程序的uncaughtException方法,將線程和異常作為參數(shù)傳遞2023-03-03基于Log4j2阻塞業(yè)務(wù)線程引發(fā)的思考
這篇文章主要介紹了基于Log4j2阻塞業(yè)務(wù)線程引發(fā)的思考,基于很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12mybatis resultType自帶數(shù)據(jù)類型別名解讀
MyBatis為了簡化開發(fā),通過org.apache.ibatis.type.TypeAliasRegistry為常見類定義了別名,這些別名包括基本數(shù)據(jù)類型及其數(shù)組、集合類型等,如string對應(yīng)java.lang.String,int對應(yīng)java.lang.Integer等,此外,還有特殊前綴的別名如_int對應(yīng)int類型2024-10-10