Mybatis返回插入主键id的方法
程序员文章站
2024-02-29 19:15:04
在mapper的xml文件中配置 usegeneratedkeys
以及 keyproperty 返回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);
好了,到此结束,希望对大家有所帮助!
上一篇: 阿里云短信服务
推荐阅读