分享牛,分享牛原创。ssm整合的时候,我们操作mybatis insert 的时候,需要返回插入的主键,因为主键是自增的,这个时候怎么办呢?很简单看一下下面的代码示例:

1.1.1. 代码定义

public interface IBaseService<M,QM extends BaseModel> {
public void create(M m);
public int createAndRetuenId(M m);
public void update(M m);
public void delete(int uuid);
public M getByUuid(int uuid);
public Page<M> getByConditionPage(QM qm);
}

1.1.2. xml定义

  <insert id="createAndRetuenId"  useGeneratedKeys="true" keyProperty="uuid" parameterType="com.shareniu.car.vo.CartModel">
     	insert into tbl_cart (customerUuid,goodsUuid,buyNum)
     	 values(#{customerUuid},#{goodsUuid},#{buyNum})
     </insert>

keyProperty 注意这里就是传入的对象中的属性,最终的返回主键的值封装在这个属性中。

1.1.3. java代码

CartModel m=new CartModel();
m.setBuyNum(5);
m.setGoodsUuid(2);
m.setCustomerUuid(3);
int createAndRetuenId = getiCartService.createAndRetuenId(m);
System.out.println(m.getUuid());

1.1.4. 注意

1.返回值int只是 标示sql是否执行成功。

2.keyProperty 返回的值才是真正的主键的值,直接从插入的对象中取值即可这里我们m.getUuid()。

分享牛原创(尊重原创 转载对的时候第一行请注明,转载出处来自分享牛http://blog.csdn.net/qq_30739519)

mybatis insert 返回主键的更多相关文章

  1. MyBatis insert返回主键(sqlserver2008)

    mybatis insert返回主键(sqlserver2008)   MyBatisXML配置,下面两种方式都行 方式1: <insert id="insert" para ...

  2. MyBatis insert 返回主键的方法

    数据库:SqlServer2005 表结构: /*==============================================================*//* Table: D ...

  3. Mybatis insert返回主键ID

    Mybatis insert语句书写 <insert id="insertSelective" useGeneratedKeys="true" keyPr ...

  4. Mybatis Insert 返回主键ID

    <insert id="insert" useGeneratedKeys="true" keyProperty="u_Id" para ...

  5. Mybatis批量insert 返回主键值和foreach标签详解

    Mybatis批量insert 返回主键 Mybatis从3.3.1版本开始,支持批量插入后返回主键ID.首先对于支持自增主键的数据库使用useGenerateKeys和keyProperty,对于不 ...

  6. mybatis添加数据时返回主键 insert 返回主键值

    insert 返回主键值 useGeneratedKeys=“true” parameterType=“USer” keyProperty=“id”, <insert id="inse ...

  7. mybatis mysql 批量insert 返回主键

    Mybatis在插入单条数据的时候有两种方式返回自增主键:    mybatis3.3.1支持批量插入后返回主键ID, 首先对于支持自增主键的数据库:useGenerateKeys和keyProper ...

  8. mybatis 08: 返回主键值的insert操作 + 利用UUID获取字符串(了解)

    返回主键值的insert操作 应用背景 图示说明 在上述业务背景下,涉及两张数据表的关联操作:用户表 + 用户积分表 传统操作:在对用户表执行完插入语句后,再次查询该用户的uid,将该uid作为外键, ...

  9. Mybatis中insert返回主键ID

    记录解决的过程,这里就不搬砖了. 1.获取insert后的主键id 原文链接:http://www.cnblogs.com/fsjohnhuang/p/4078659.html 2.insert后返回 ...

随机推荐

  1. php Redis函数使用总结(string,hash,list, set , sort set )

    对于:string, set , sort set , hash 的增,改操作,是同一个命令,但是把它当改操作时,及时成功返回值依旧为0 对于:list结构来说,增删改查自有一套方法.   <? ...

  2. js将一个数组插入另一个数组

    var cont =[1,2,3,4]; var res =[4,5,6] for(var i=0;i<res;i++){ cont.push( res.data.list[i]); } con ...

  3. [LeetCode] Zuma Game 祖玛游戏

    Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), gre ...

  4. [LeetCode] Diagonal Traverse 对角线遍历

    Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal ...

  5. Python系列之 - python循环语句

    前两篇说的是数据类型和数据运算,本篇来讲讲条件语句和循环语句. 1. 条件语句 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执 ...

  6. 机器学习基石:10 Logistic Regression

    线性分类中的是非题------>概率题, 设置概率阈值后,大于等于该值的为O,小于改值的为X.------>逻辑回归. O为1,X为0: 逻辑回归假设: 逻辑函数/S型函数:光滑,单调, ...

  7. swoole_event_add实现异步

    swoole提供了swoole_event_add函数,可以实现异步.此函数可以用在Server或Client模式下. 实现异步tcp客户端 示例: <?php $start_time = mi ...

  8. Redis常用命令--SortedSet

    SortedSet是一个类似于Set的集合数据类型,里面的每个字符串元素都关联到一个score(整数或浮点数),并且总是通过score来进行排序着. 并且可以取得一定范围内的元素. 在Redis中大概 ...

  9. [Codeforces 606C]Sorting Railway Cars

    Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...

  10. bzoj 2004: [Hnoi2010]Bus 公交线路

    Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距 离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决 ...