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 ...
随机推荐
- Python 基础补充(一) 列表、元组、集合、字典的区别和相互转换
一.列表.元组.集合.字典的区别 列表 元组 集合 字典 英文 list tuple set dict 可否读写 读写 只读 读写 读写 可否重复 是 是 否 是 存储方式 值 值 键(不能重复) ...
- cvc-complex-type.2.4.a: Invalid content was found starting with element 'async-supported'. One of '{"http://java.sun.com/xml/ns/javaee":init-param}' is expected.
第一种方案: 将 "http://java.sun.com/xml/ns/javaee" 换为 "http://java.sun.com/xml/ns/j2ee& ...
- C#使用MonoPInvokeCallback,让C直接回调C#函数
Test.mm char* TestMakeCString(NSString *str) { const char* string = [str UTF8String]; if (string == ...
- Java设置运行时环境参数
一.代码中,如下: System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", " ...
- Cookie-base 认证实现(学习笔记)
第一步 新建一个ASP.NET core 默认项目 新建 AdminController public class AdminController : Controller { [Authorize] ...
- Android通过adb命令Debug调试
Android Debug后IDE执行的命令: / ::: Launching module_app $ adb push C:\fastwork\Projects\project\CJPT\modu ...
- 【原】wow64 x86/x64 代码切换过程分析
下面以ntdll32!ZwQueryInformationProcess API为例分析 x86代码与x64代码之间的切换过程, 32bit的test程序: step1: ntdll32!ZwQuer ...
- 【转】Unity网格合并_材质合并
原帖请戳:Unity网格合并_材质合并 写在前面: 从优化角度,Mesh需要合并. 从换装的角度(这里指的是换形状.换组成部件的换装,而不是挂点型的换装),都需要网格合并.材质合并.如果是人物的换装, ...
- python 如何注释
一.单行注释 单行注释以#开头,例如: print 6 #输出6 二.多行注释 (Python的注释只有针对于单行的注释(用#),这是一种变通的方法) 多行注释用三引号' ...
- 修改maven项目的编译版本
在pom.xml中添加如下代码 <build> <!-- 配置了很多插件 --> <plugins> <plugin> <groupId>o ...