MySQL和MSSQL返回主键方法

在personMap.xml中

<insert id="addPerson" parameterType="orm.Person" keyProperty="id" useGeneratedKeys="true">
insert into Person (name,age,gender) values (#{name},#{age},#{gender})
</insert>

调用插入方法,将mybatis返回的自动增长ID自动返回给id属性

SqlSession session =DBUtils.getSqlSession();
Person p=new Person();
p.setName("caoyc");
p.setAge((byte));
p.setGender("男");
System.out.println(session.insert("orm.personMapper.addPerson",p));;
session.commit();
session.close();
System.out.println(p.getId());

通过实体对象p.getId():就可以获取插入后该记录的id了

Oracle数据

<insert id="addPerson" parameterType="orm.Person" >
<selectKey resultType="java.long.Integer" keyProperty="id" order="BEFORE">
select idauto.nextval from dual
</selectKey>
insert into Person (id,name,age,gender) values (#{id},#{name},#{age},#{gender})
</insert>

Mybatis 插入后返回数据库自动增长ID的更多相关文章

  1. 数据库自动增长id下一次的值

    mysql SELECT auto_increment FROM information_schema.`TABLES` WHERE TABLE_SCHEMA='my_db_name' AND TAB ...

  2. mybatis 插入语句 返回自增长id方法

    背景:目前有个插入语句需要获取插入记录的id  因为id是自增长的,所以要在插入后返回这个id 错误1: mapper.xml: <!-- 新增 返回自增长id--> <insert ...

  3. Mybatis获取自动增长Id

    Mybatis获取自动增长Id MyBatis成功插入后获取自动增长的id 1.向xxMapping.xml配置中加上两个配置. <insert id="insertUser" ...

  4. MyBatis 插入时返回刚插入记录的主键值

    MyBatis 插入时返回刚插入记录的主键值 一.要求: 1.数据库表中的主键是自增长的,如:id: 2.获取刚刚插入的记录的id值: 二.源代码: 1.User.java package cn.co ...

  5. 插入Oracle数据库后返回当前主键id

    最近做一个spring版本3.0.4的老项目功能,应用场景要用到插入oracle表后返回主键ID拿来和其他表关联. 用oralce的可以一直用这种处理方式,高兼容低,搜索网上的资料都不能和这个Spri ...

  6. SQL获取刚插入的记录的自动增长列ID的值

    假设表结构如下: CREATE TABLE TestTable ( id int identity, CreatedDate datetime ) SQL2005获得新增行的自动增长列的语句如下: i ...

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

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

  8. 数据库插入数据返回当前主键ID值方法

    当我们插入一条数据的时候,我们很多时候都想立刻获取当前插入的主键值返回以做它用.我们通常的做法有如下几种: 1. 先 select max(id) +1 ,然后将+1后的值作为主键插入数据库: 2. ...

  9. mybatis插入是返回主键id

    <!-- 插入数据:返回记录的id值 --> <insert id="insertOneTest" parameterType="org.chench. ...

随机推荐

  1. 树形dp入门(poj 2342 Anniversary party)

    题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚 ...

  2. [Codeforces #190] Tutorial

    Link: Codeforces #190 传送门 A: 明显答案为$n+m-1$且能构造出来 #include <bits/stdc++.h> using namespace std; ...

  3. python基础之封装与绑定方法

    封装 1.什么是封装: 封:属性对外隐藏,但对内开放 装:申请一个名称空间,往里装入一系列名字/属性 2.为什么要封装: 封装数据属性:不让外部使用者直接使用数据,需要类内部开辟一个接口,让外部通过接 ...

  4. 浙南联合训练赛 H - The number of positions

    Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say ...

  5. Problem F: 程序填充(函数、指针):去数组负数

    #include <stdio.h> void f(int *a,int *m) { int i,j; ;i < *m;i++) ) { ;j++) a[j]=a[j+]; (*m) ...

  6. Elasticsearch 5.5.1的安装和入门教程(转)

    说明:转自老阮的文章,业界最简单的入门教程.一切的安装的运行建议不要用root权限,最好是当前用户下的权限. 作者: 阮一峰 日期: 2017年8月17日 全文搜索属于最常见的需求,开源的 Elast ...

  7. MongoDB 刷新几次就报错

    官方: MongoDB.Driver 在页面上 速度刷新几次,就会抛错? 为何? Service 层  Autofac

  8. Linux中的黑洞(black hole)-/dev/null

    http://blog.csdn.net/loongshawn/article/details/50514018

  9. 翻译:Spring-Framework-Reference Document:15.2-DispatcherServlet

    写在前面的话:   最近被项目的代码折腾的死去活来的,其实框架也没有那么难理解,只是自己的Web基础太差,被Request和Response这一对神雕侠侣坑到泪流满面!今天捣腾了一下Spring We ...

  10. iOS:KVC和KVO

    来源:  对月流 链接:http://www.jianshu.com/p/f1393d10109d 写在前面: 关于KVC和KVO各种博客多了去了,重新整理下,就当是温习一下吧,也还算是个新手,不对的 ...