mybatis插入数据并获取主键值
有时候我们的主键是自增的,但是我们想要在插入一条数据以后获取这条数据的主键值,而我们知道,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插入数据并获取主键值的更多相关文章
- mybatis 插入数据并返回主键值
<insert id="insert" parameterType="com.pojo.TSubject" useGeneratedKeys=" ...
- MyBatis 插入记录同时获取主键
MyBatis 插入记录同时获取主键 MyBatis 插入记录同时获取主键的系统界面 useGeneratedKeys 属性 keyProperty 属性 keyColumn 属性 selectKey ...
- SpringBoot整合MyBatis获得插入数据后获取主键,返回值总是1
xml里的写法 <insert id="insertLogin" parameterType="com.xyt.p2p.pojo.LoginInfo" k ...
- mybatis 插入数据时返回主键
在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:显然,假如主键是你生成后插入的,自然你已经有主键了,显然不需要我们再去获得,所以我们这里处理的是当主键 ...
- mybatis插入的同时获取主键id
<insert id="insertAndReturnId" parameterType="com.qianlong.cms.entity.AppCmsRole&q ...
- Mybatis插入数据后返回主键id
有时候使用mybatis插入数据后,需要用到记录在数据库中的自增id,可以利用keyProperty来返回,赋值给实体类中的指定字段. 单条记录插入并返回 First, if your databas ...
- mybatis插入数据并返回主键(oracle)
通常我们执行一个inser语句,即使有返回,也只是会返回影响了多少条数据 @insert("insert into t_user (id,name) values (suser.nextva ...
- Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)
1.通过Oracle序列 -- Create sequence create sequence SEQ_DW_EWSYSTEM minvalue 1 maxvalue 9999999999999999 ...
- mysql数据库使用mybatis 插入数据时返回主键
为了体现题目,特指的是mysql,先贴上代码: <insert id="saveBizProdOrderDetail" useGeneratedKeys="true ...
随机推荐
- js弹出div层内容(按回退键关闭div层及遮罩)
<!--弹出的div列表对应的详情--> <div id="newhtml" class="white_content"> <di ...
- ncnn 源码学习-Mat.h Mat.c
纯小白记录下腾讯的ncnn框架源码的学习.纯粹写给自己看的,不保证正确性. Mat 类似于 caffe中的blob,是一个张量的存储结构体. 一.数据成员: 1.void * data 多维数据按一位 ...
- while 循环居然可以用else
while 循环居然可以用else python 3 while a<50: print a a=a+1 else: print"over."
- 创建一个Maven Web应用程序
1 在Eclipes创建maven,首先File new ,在other 中找到Maven,Maven Project,下一步选择WebApp,创建Maven工程名字,完成 2 在新建的Maven工程 ...
- 使用arguments对象验证函数的参数是否合法
<script>function sum(arg1,arg2) //加法函数{ var realArgCount = arguments.length; //调用函数时传递的实参个数 va ...
- 修饰词public、private、protected、默认、四者之间的区别
在Java语法中,对于类与类中的成员变量和成员方法是通过访问控制符来区分控制的. 下面来看看这四种访问控制符的区别:(public.protected.private.default) 1.publi ...
- 【deep learning】斯坦福CS231n—深度学习与计算机视觉(资料汇总)
官网 链接:CS231n: Convolutional Neural Networks for Visual Recognition Notes: 链接:http://cs231n.github.io ...
- Python之路 - Socketserver实现多并发
Python之路 - Socketserver实现多并发 阅读指引
- ubuntu16 安装openssh-server 一直安装不上Unable to correct problems, you have held broken packages
zengqi@zengqi:~$ sudo apt-get install openssh-server Reading package lists... DoneBuilding dependenc ...
- webui-popover 一个轻量级的jquery弹出层插件
该提示框插件可以和Bootstrap完美结合,但是并不一定需要和Bootstrap一起使用.它支持IE7以上的浏览器. 首先要引入需要的css js 文件 <link rel="s ...