Mybatis返回插入主键id Mybatis返回插入主键id的方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Mybatis返回插入主键id Mybatis返回插入主键id的方法

Mr_YarNell   2021-03-24 我要评论
想了解Mybatis返回插入主键id的方法的相关内容吗,Mr_YarNell在本文为您仔细讲解 Mybatis返回插入主键id的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:mybatis,返回主键id,mybatis,返回主键,下面大家一起来学习吧。

在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);

好了,到此结束,希望对大家有所帮助!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们