mybatis3 :insert返回插入的主键(selectKey)
Mysql:
主键自增长。
加上:keyProperty="id"就可以获得了。
<insert id="insert" parameterType="entity" keyProperty="id" useGeneratedKeys="true">
insert into <include refid="t_user"/>
(name, code, version)
values (#{name}, #{code}, #{version})
</insert>
=========================================以下的方式也可以========================
*_mapper.xml:
<insert id="insert" parameterType="entity" useGeneratedKeys="true">
insert into <include refid="t_user"/>
(name, code, version)
values (#{name}, #{code}, #{version})
<selectKey resultType="long" keyProperty="id">
SELECT IF(row_count() > 0, last_insert_id(), 0) AS id FROM dual
</selectKey>
</insert>
就实现了插入之后获得主键的功能。
mybatis3 :insert返回插入的主键(selectKey)的更多相关文章
- (转)MyBatis+MySQL 返回插入的主键ID
MyBatis+MySQL 返回插入的主键ID 需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值. 方法:在mapper中指定keyProperty属性,示例如 ...
- mybatis+mysql返回插入的主键,参数只是提供部分参数
mybatis+mysql返回插入的主键,参数只是提供部分参数 <insert id="insertByChannelIdOpenid" useGeneratedKeys=& ...
- MyBatis+MySQL 返回插入的主键ID
需求:使用MyBatis往MySQL数据库中插入一条记录后,需要返回该条记录的自增主键值. 方法:在mapper中指定keyProperty属性,示例如下: <insert id="i ...
- Mybatis返回插入的主键
在使用MyBatis做持久层时,insert语句默认是不返回记录的主键值,而是返回插入的记录条数:如果业务层需要得到记录的主键时,可以通过配置的方式来完成这个功能 情景一:针对自增主键的表,在插入时不 ...
- Mybatis 返回插入的主键
业务中,会遇到这样的问题,就是感觉返回的主键,插入作为其他表的外键. 那么问题来了,如何去实现,其实方法比较简单,这里就是重点记录下,会出现的误区. 用自动生成sql工具的话,加上下面这句话 < ...
- MyBatis返回插入的主键ID(Mysql数据库)
1.Java代码: 1.1 entity类: User.java public class User { private int userId; private String userName; pr ...
- mybatis MySQL返回插入的主键ID,oracle不行
<insertid=“doSomething"parameterType="map"useGeneratedKeys="true"keyProp ...
- MyBatis + MySQL返回插入的主键id
这是最近在实现perfect-ssm中的一个功能时碰到的一个小问题,觉得需要记录一下,向MySQL数据库中插入一条记录后,需要获取此条记录的id值,以生成对应的key值存入到redis中,id为自增i ...
- mybatis oracle insert 返回新增的主键值
<insert id="insertVmsTemplatePlayItem" parameterType="VmsTemplatePlayItem"> ...
随机推荐
- TRUNC函数,ORA-01898 精度说明符过多
TRUNC(SYSDATE)即可默认当前日期(年月日),TRUNC(SYSDATE,'yyyy-mm-dd'),精度说明符过多
- Java_HttpURLConnection使用
包括使用HttpURLConnection执行get/post请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- 【CodeForces 604B】F - 一般水的题1-More Cowbe
Description Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter ...
- 【Matplotlib】 刻度设置(2)
Tick locating and formatting 该模块包括许多类以支持完整的刻度位置和格式的配置.尽管 locators 与主刻度或小刻度没有关系,他们经由 Axis 类使用来支持主刻度和小 ...
- Android 服务类Service 的详细学习
http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建 目录(?)[+] 什么是服务 服务有 ...
- Maven学习笔记-02-Maven项目打包配置与测试
一 Maven项目打包配置 1 为整个项目统一指定字符集 <properties> <project.build.sourceEncoding>UTF-</project ...
- 【转】KMP算法
转载请注明来源,并包含相关链接.http://www.cnblogs.com/yjiyjige/p/3263858.html 网上有很多讲解KMP算法的博客,我就不浪费时间再写一份了.直接推荐一个当初 ...
- C# ServiceStack.Redis 操作对象List
class Car { public Int32 Id { get; set; } public String Name { get; set; } static void Main(string[] ...
- mybatis使用小记
参考资料:http://blog.csdn.net/hupanfeng/article/details/9098453 1.设置不缓存每次查询的结果: 如题,通过设置 flushCache=" ...
- MySQL中varchar类型在5.0.3后的变化
1.mysql varchar类型变化:mysql 5.0.3 之前: 0--255字节 varchar(20)中的20表示字节数,如果存放urf8编码的话只能放6个汉字. MySQL 5.0.3 之 ...