有时候使用mybatis插入数据后,需要用到记录在数据库中的自增id,可以利用keyProperty来返回,赋值给实体类中的指定字段。

单条记录插入并返回

First, if your database supports auto-generated key fields (e.g. MySQL and SQL Server), then you can simply set useGeneratedKeys="true" and set the keyProperty to the target property and you're done. For example, if the Authortable above had used an auto-generated column type for the id, the statement would be modified as follows。

<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
</insert>

  

添加useGeneratedKeys="true" 以及keyProperty="id"即可。id为实体类中的字段名称

多条记录插入并返回

If your database also supports multi-row insert, you can pass a list or an array of Authors and retrieve the auto-generated keys.

<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
insert into Author (username, password, email, bio) values
<foreach item="item" collection="list" separator=",">
(#{item.username}, #{item.password}, #{item.email}, #{item.bio})
</foreach>
</insert>

  

坑:要注意,多条记录时,有可能会出现id没有返回的情况。检查以下几点:

1、Mybatis版本3.3.1及其以上。

2、在Dao中不能使用@param注解。

3、Mapper.xml中使用list变量接受Dao中的集合。

Mybatis插入数据后返回主键id的更多相关文章

  1. Laravel 5 插入数据后返回主键ID

    方法一: $info = DB::table('表名')->insertGetId(['imgName' => $fileName]);//图片名入库后返回添加数据行的主键ID 方法二:( ...

  2. mybatis 插入数据时返回主键

    在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:显然,假如主键是你生成后插入的,自然你已经有主键了,显然不需要我们再去获得,所以我们这里处理的是当主键 ...

  3. MyBatis插入记录时返回主键id的方法

    有时候插入记录之后需要使用到插入记录的主键,通常是再查询一次来获取主键,但是MyBatis插入记录时可以设置成返回主键id,简化操作,方法大致有两种. 对应实体类: public class User ...

  4. mybatis 插入数据并返回主键值

    <insert id="insert" parameterType="com.pojo.TSubject" useGeneratedKeys=" ...

  5. mybatis插入数据并返回主键(oracle)

    通常我们执行一个inser语句,即使有返回,也只是会返回影响了多少条数据 @insert("insert into t_user (id,name) values (suser.nextva ...

  6. mysql数据库使用mybatis 插入数据时返回主键

    为了体现题目,特指的是mysql,先贴上代码: <insert id="saveBizProdOrderDetail" useGeneratedKeys="true ...

  7. mybatis的插入数据后的主键获取

    为什么要在插入数据后获取主键:当有一个订单表和订单详情表,当插入订单表的数据后,需要在订单详情表插入该订单的具体购物情况,订单详情表需要的一个列是订单表的主键或者订单ID.(通俗讲:A表的主键是B表的 ...

  8. mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey

    mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...

  9. mybatis添加记录时返回主键id

    参考:mybatis添加记录时返回主键id 场景 有些时候我们在添加记录成功后希望能直接获取到该记录的主键id值,而不需要再执行一次查询操作.在使用mybatis作为ORM组件时,可以很方便地达到这个 ...

随机推荐

  1. 【Android】ProgressBar

    http://www.cnblogs.com/wangying222/p/5304990.html http://www.cnblogs.com/plokmju/p/android_ProgressB ...

  2. ASPX代码加固小结

    1.replace替换 <%@Page Language="C#"%> <% string strID=Request["id"]; strI ...

  3. Android开发-- The content of the adapter has changed but ListView did not receive a notification - With AsyncTask

    最近在联系开发DaysMatter时遇到一个问题: app中使用ListView来展示所有事件,每次添加完事件后使用下面代码来更新ListView. toDoListView.refreshDrawa ...

  4. Unity弹出MessageBox

    [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = Char ...

  5. Unity Shader 设置纹理采样tex2D过滤方式

    双击红色框区域

  6. mybatis 之 resultType="HashMap" parameterType="list"

    public ServiceMessage<List<Map<String, Object>>> queryGoodsStockInfo(List<Long& ...

  7. mysql check约束无效

    转自http://blog.csdn.net/maxint64/article/details/8643288 今天在mysql中尝试使用check约束时,才知道在MySQL中CHECK约束是无效的, ...

  8. Java面试题全集

    Java面试题全集(上) Java面试题全集(中) Java面试题全集(下) http://www.importnew.com/21445.html

  9. Linux设备驱动剖析之SPI(一)

    写在前面 初次接触SPI是因为几年前玩单片机的时候,由于普通的51单片机没有SPI控制器,所以只好用IO口去模拟.最近一次接触SPI是大三时参加的校内选拔赛,当时需要用2440去控制nrf24L01, ...

  10. LeetCode 10 Regular Expression Matching(字符串匹配)

    题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description   '.' Matches any si ...