有时候我们的主键是自增的,但是我们想要在插入一条数据以后获取这条数据的主键值,而我们知道,mybatis执行完插入操作以后返回的是生效的记录数。那如何才能获取这个主键值呢。

1.在配置文件mapper.xml中加入如下语句。

     <insert id="insertSelectiveRePk" parameterType="com.xdx.entity.TMenu"
useGeneratedKeys="true" keyProperty="menuId" keyColumn="menu_id">
insert into t_menu
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="menuId != null">
menu_id,
</if>
<if test="menuName != null">
menu_name,
</if>
<if test="menuType != null">
menu_type,
</if>
<if test="menuLevel != null">
menu_level,
</if>
<if test="menuIcon != null">
menu_icon,
</if>
<if test="menuSrc != null">
menu_src,
</if>
<if test="rMenuId != null">
r_menu_id,
</if>
<if test="pMenuId != null">
p_menu_id,
</if>
<if test="menuIntro != null">
menu_intro,
</if>
<if test="priority1 != null">
priority1,
</if>
<if test="priority2 != null">
priority2,
</if>
<if test="priority3 != null">
priority3,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
<if test="isDel != null">
is_del,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="menuId != null">
#{menuId,jdbcType=INTEGER},
</if>
<if test="menuName != null">
#{menuName,jdbcType=VARCHAR},
</if>
<if test="menuType != null">
#{menuType,jdbcType=INTEGER},
</if>
<if test="menuLevel != null">
#{menuLevel,jdbcType=INTEGER},
</if>
<if test="menuIcon != null">
#{menuIcon,jdbcType=VARCHAR},
</if>
<if test="menuSrc != null">
#{menuSrc,jdbcType=VARCHAR},
</if>
<if test="rMenuId != null">
#{rMenuId,jdbcType=INTEGER},
</if>
<if test="pMenuId != null">
#{pMenuId,jdbcType=INTEGER},
</if>
<if test="menuIntro != null">
#{menuIntro,jdbcType=VARCHAR},
</if>
<if test="priority1 != null">
#{priority1,jdbcType=INTEGER},
</if>
<if test="priority2 != null">
#{priority2,jdbcType=INTEGER},
</if>
<if test="priority3 != null">
#{priority3,jdbcType=INTEGER},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
<if test="isDel != null">
#{isDel,jdbcType=INTEGER},
</if>
</trim>
</insert>

注意第二行的几句代码:useGeneratedKeys="true" keyProperty="menuId" keyColumn="menu_id"

useGeneratedKeys="true" :使用自增的主键

keyProperty="menuId"  :主键对应的java实体的成员

keyColumn="menu_id" :主键在数据库中的列名

2.在插入后就可以直接获取主键值了,代码如下

baseDao.addT("TMenuMapper.insertSelectiveRePk", menu);//插入
int key=menu.getMenuId();//直接获取主键

其中menu就是我们要保存的实体。

mybatis插入数据并获取主键值的更多相关文章

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

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

  2. MyBatis 插入记录同时获取主键

    MyBatis 插入记录同时获取主键 MyBatis 插入记录同时获取主键的系统界面 useGeneratedKeys 属性 keyProperty 属性 keyColumn 属性 selectKey ...

  3. SpringBoot整合MyBatis获得插入数据后获取主键,返回值总是1

    xml里的写法 <insert id="insertLogin" parameterType="com.xyt.p2p.pojo.LoginInfo" k ...

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

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

  5. mybatis插入的同时获取主键id

    <insert id="insertAndReturnId" parameterType="com.qianlong.cms.entity.AppCmsRole&q ...

  6. Mybatis插入数据后返回主键id

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

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

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

  8. Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)

    1.通过Oracle序列 -- Create sequence create sequence SEQ_DW_EWSYSTEM minvalue 1 maxvalue 9999999999999999 ...

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

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

随机推荐

  1. World Cup 996B(排队模拟)

    题意:有n个通道,按顺序每一次站一个通道,直到所站的通道没有人 分析:模拟这个过程 #include<cstdio> int main() { ]; while(~scanf(" ...

  2. js Json数组的增删改查

    <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...

  3. ASP.NET HTTP 协议

    http是无状态的,不会记得“上个请求***”,所以哪怕是同一个页面中的js.css.jpg也都要重复的提交Accept-Language.Accept-Encoding.Cookie等. 一般情况下 ...

  4. 如何將字串yyyyMMddHHmmss轉成Datetime呢?

    有朋友在FB上問到,他們將日期的分隔符號都置換成空字串後的字串,要如何將它再轉回成DateTime呢? 例如日期 2013/04/02 14:08:37 會轉成 20130402140837 . 要如 ...

  5. Delphi 透明窗体显示文字

    设置窗体属性:BorderStyle 属性设置为 bsNoneColor 属性设置为 clWhite(白色:窗体背景色)TransparentColor 属性设置为 trueTransparentCo ...

  6. 9.mysql-存储过程.md

    目录 创建 创建 -- 创建存储过程 DELIMITER $ -- 声明存储过程的结束符 CREATE PROCEDURE pro_test() --存储过程名称(参数列表) BEGIN -- 开始 ...

  7. css3选择器补充

    一.关系选择器 1.E+F   (E元素下一个满足条件的兄弟元素节点) <style> div + p{ background-color:red;// 第一个p元素变色 } </s ...

  8. div 光标处插入内容

    var Manager = { insertHtml: function(html, type) { var lastMemo = document.getElementById("memo ...

  9. redis 存储java对象 两种方式

    根据redis的存储原理,Redis的key和value都支持二进制安全的字符串 1.利用序列化和反序列化的方式存储java对象我们可以通过对象的序列化与反序列化完成存储于取出,这样就可以使用redi ...

  10. Java进阶 线程安全

    多线程编程中的三个核心概念 原子性 这一点,跟数据库事务的原子性概念差不多,即一个操作(有可能包含有多个子操作)要么全部执行(生效),要么全部都不执行(都不生效). 关于原子性,一个非常经典的例子就是 ...