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 ...
随机推荐
- linux环境运行java项目并有外部引用jar
eclipse目录结构: linux目录结构: lib目录结构: 其中除了IMT_ENCODING_DSP.jar其余的都是外部引用的jar IMT_ENCODING_DSP.jar是java项目打包 ...
- DataGrip 连接数据库查询出来的结果乱码的问题
打开连接数据源选项 选择 Advanced----Charset 填入 GBK 应用即可 目前遇到的是连接 SYbase数据库
- postman 请求种添加用户权限
1. 打开postman, 在Tests输入以下内容: var jsonData =JSON.parse(responseBody);//获取body中返回的所有参数 postman.setGloba ...
- TCP连接异常断开检测(转)
TCP是一种面向连接的协议,连接的建立和断开需要通过收发相应的分节来实现.某些时候,由于网络的故障或是一方主机的突然崩溃而另一方无法检测到,以致始终保持着不存在的连接.下面介绍一种方法来检测这种异常断 ...
- 健康检测文件httpchk.jsp
静态显示: <html><body><center> Now time is: <%=new java.util.Date()%> </cente ...
- uml用例关系
关联关系 关联关系是指执行者与用例之间的关系,又称为通信关系,如果某个执行者可以对某个用例进行操作,它们之间就具有关联关系,如下图所示,“经理”有一个功能为“查看库存报表”,因此可以在执行者“经理”和 ...
- 创建一个Maven Web应用程序
1 在Eclipes创建maven,首先File new ,在other 中找到Maven,Maven Project,下一步选择WebApp,创建Maven工程名字,完成 2 在新建的Maven工程 ...
- python实现排序算法一:快速排序
##快速排序算法##基本思想:分治法,将数组分为大于和小于该值的两部分数据,然后在两部分数据中进行递归排序,直到只有一个数据结束## step1: 取数组第一个元素为key值,设置两个变量,i = 0 ...
- Java学习06 (第一遍) - JSP与Servlet
EL(Expression Language) <% User user=(User)session.getAttribute("user"); Dept dept=user ...
- http://wenku.baidu.com/view/26afdb8371fe910ef12df8ccRevit采用DWG和FBX两种格式导入3D max方法的总结
2.DWG 属性——导出为 ACIS 实体(如果选择导出为多边形网格,则将每个图元导出为 由多个多边形组成的对象,这些多边形相互连接或组成 “ 网格 ” .在导出到 DWG 文件以用 于 Ma ...