Mybatis返回插入主鍵id的方法
更新時間:2017年04月13日 08:51:59 作者:Mr_YarNell
這篇文章主要介紹了 Mybatis返回插入主鍵id的方法,在文章底部給大家補充了Mybatis中insert中返回主鍵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);
好了,到此結束,希望對大家有所幫助!
相關文章
spring boot中xalan引入報錯系統(tǒng)找不到指定的文件原因分析
這篇文章主要介紹了spring boot中xalan引入報錯系統(tǒng)找不到指定的文件,主要原因是內嵌的tomcat9.0.36,本文給大家分享最新解決方法,需要的朋友可以參考下2023-08-08Java異常處理UncaughtExceptionHandler使用實例代碼詳解
當一個線程由于未捕獲異常即將終止時,Java虛擬機將使用thread . getuncaughtexceptionhandler()查詢線程的uncaughtException處理程序,并調用處理程序的uncaughtException方法,將線程和異常作為參數傳遞2023-03-03