如下情况适用支持自增的DB,如MySQL。其他情况参见:MyBatis魔法堂:Insert操作详解(返回主键、批量插入)

1.model

public class UserInfo {
private int id;//主键自增ID
private String userName;//姓名
private String account;//登陆账号
private String password;//密码
}

  

2.UserInfoMapper.java

public interface UserInfoMapper {
int addUser(UserInfo userInfo);
}

  

3.UserInfoMapper.xml

<insert id="addUser" parameterType="com.xxx.model.UserInfo" useGeneratedKeys="true" keyProperty="id">
INSERT INTO
user_info(user_name, account, password)
values
(#{userName},#{account},#{password})
</insert>

  

这样,在插入后,MySQL自增的id就会设置到原来的userInfo对象里。

其中 useGeneratedKeys="true" keyProperty="id" 是起作用的关键语句。

4.QA

4.1 报错:org.apache.ibatis.binding.BindingException: Parameter 'id' not found

完整错误如下:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [userInfo, param1]

这是因为在addUser()的对象参数前加@Param("userInfo"),而在mapper.xml里写成

<insert id="addUser" parameterType="com.xxx.model.UserInfo" useGeneratedKeys="true" keyProperty="id">
INSERT INTO
user_info(user_name,, account, password)
values
(#{userinfo.userName},#{userInfo.account},#{userInfo.password})
</insert>

这种情况在不返回自增值是没有问题的,但一旦设置了useGeneratedKeys就报错。所以养成良好的习惯,没事少加@Param!!!

end

MyBatis insert后返回自增字段的值的更多相关文章

  1. mybatis postgresql insert后返回自增id

    在使用mybatis + postgresql,我们有时在插入数据时需要返回自增id的值,此时在插入时,可以按照以下例子来编写mapper文件 <insert id="insertUs ...

  2. mybatis insert后返回主键ID

    需求: mybatis  在添加记录时需要获取到记录主键id id=0 无法获取主键id的值 在插入方法中添加如下属性和相应的值 <insert useGeneratedKeys="t ...

  3. Mybatis insert时返回自增id

    SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问题,他可以很随意的设置生成主键的方式. 详细看这里:http://www.cnblogs.com/SimonHu1 ...

  4. (转)Mybatis insert后返回主键给实体对象(Mysql数据库)

    <insert id="insert" parameterType="com.zqgame.game.website.models.Team"> & ...

  5. Spring中新建记录后返回自增主键的处理方法

    接手一个旧系统改造的过程,要插入后立即返回自增值,不能重构guid类型主键,Spring提供了很优美的机制. Spring利用GeneratedKeyHolder,提供了一个可以返回新增记录对应主键值 ...

  6. mybatis插入数据后返回自增主键ID详解

    1.场景介绍: ​ 开发过程中我们经常性的会用到许多的中间表,用于数据之间的对应和关联.这个时候我们关联最多的就是ID,我们在一张表中插入数据后级联增加到关联表中.我们熟知的mybatis在插入数据后 ...

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

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

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

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

  9. MyBatis返回主键,MyBatis Insert操作返回主键

    MyBatis返回主键,MyBatis Insert操作返回主键 >>>>>>>>>>>>>>>>> ...

随机推荐

  1. 注释PHP和html混合代码

    <?php if(false){ ?> any code <?php } ?>

  2. Lucene自定义扩展QueryParser

    Lucene版本:4.10.2 在使用lucene的时候,不可避免的需要扩展lucene的相关功能来实现业务的需要,比如搜索时,需要在满足一个特定范围内的document进行搜索,如年龄在20和30岁 ...

  3. [RxJS] Returning subscriptions from the subscribe function

    So far, when writing these subscribe functions, we haven't returned anything. It is possible return ...

  4. java图片处理工具类

    直接上代码: package com.zxd.tool; /** * Created by zhang on 14-3-1. * 图片的常用操作类 */ import java.awt.AlphaCo ...

  5. GCC编译选项

    一.看例子分析gcc 的编译选项 gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld 1.-I /home/h ...

  6. TreeSet与TreeMap

    TreeSet底层使用的存储容器为TreeMap TreeMap使用红黑树(一种自平衡的排序二叉树)实现,检索效率为O(logn) 红黑树的三个基本操作:左旋.右旋.着色 平衡二叉树:空树或左右子树高 ...

  7. Laravel 加载 js css image 文件

    写在前面的话: 1.前提是需要使用blade模板引擎 2.css js image 文件夹建在laravel 的 public 目录下面 3.生成的路径默认都是相对路径 A: 加载css文件 (用下面 ...

  8. EventBus 事件总线 案例

    简介 地址:https://github.com/greenrobot/EventBus EventBus是一个[发布 / 订阅]的事件总线.简单点说,就是两人[约定]好怎么通信,一人发布消息,另外一 ...

  9. web页面打印

    在使用的两种方式打印: 第一种:js如下 function doPrint() { allhtml = window.document.body.innerHTML; starstr = " ...

  10. Tomcat6.0数据源配置

    涉及context.xml和server.xml http://blog.csdn.net/onlymilan/article/details/5493485