MyBatis使用MySQL数据库如何在执行insert操作后返回自增的主键
来源:互联网 发布:飞龙签名设计软件 编辑:程序博客网 时间:2024/06/10 05:34
数据库:MySQL5
表:create table play(iid int(10) not null primary key auto_increment,typeId int(3));
Play.java:
public class Play{
private Integer iID;
private Integer typeID;
...setter and getter省略...
}
play-mapper.xml:
<mapper namespace="PlayDAO">
<resultMap id="BaseResultMap" type="Play">
<id column="iid" property="iID" jdbcType="INTEGER" />
<result column="typeId" property="typeID" jdbcType="INTEGER" />
</resultMap>
<insert id="insert" parameterType="Play">
insert into play(typeId) values (#{typeID,jdbcType=INTEGER})
<selectKey keyProperty="iID" resultType="int" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
</insert>
</mapper>
说明:
1、 order="AFTER" 表示selectKey的动作在insert into...执行之后执行。
2、为了说明问题,本例特别让java类中的属性名与xml配置文件中的column名不同,需要特别注意 selectKey的keyProperty属性必须是java类中的属性名。
补充:
在Oracle中的用法:先为主键创建一个序列 create sequence Play_Sequence increment by 1 start with 1 nomaxvalue;
<insert id="insert" parameterType="Play">
<selectKey keyProperty="iID" resultType="int" order="BEFORE" >
select Play_Sequence.nextval from dual
</selectKey>
insert into play( iid ,typeId) values ( #{ iID ,jdbcType=INTEGER} ,#{typeID,jdbcType=INTEGER})
</insert>
说明:
1、无需为序列创建触发器(trigger),order="BEFORE"确定了在插入数据之前取得主键,再将主键随其他字段一起插入即可。
2、做了一个试验,在创建触发器的情况下,使selectKey的order="AFTER",再使selectKey选择序列的currval,发现currval还未加1。所以只能用1中的方法。
参考:http://tx2099.iteye.com/blog/1413878
表:create table play(iid int(10) not null primary key auto_increment,typeId int(3));
Play.java:
public class Play{
private Integer iID;
private Integer typeID;
...setter and getter省略...
}
play-mapper.xml:
<mapper namespace="PlayDAO">
<resultMap id="BaseResultMap" type="Play">
<id column="iid" property="iID" jdbcType="INTEGER" />
<result column="typeId" property="typeID" jdbcType="INTEGER" />
</resultMap>
<insert id="insert" parameterType="Play">
insert into play(typeId) values (#{typeID,jdbcType=INTEGER})
<selectKey keyProperty="iID" resultType="int" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
</insert>
</mapper>
说明:
1、 order="AFTER" 表示selectKey的动作在insert into...执行之后执行。
2、为了说明问题,本例特别让java类中的属性名与xml配置文件中的column名不同,需要特别注意 selectKey的keyProperty属性必须是java类中的属性名。
补充:
在Oracle中的用法:先为主键创建一个序列 create sequence Play_Sequence increment by 1 start with 1 nomaxvalue;
<insert id="insert" parameterType="Play">
<selectKey keyProperty="iID" resultType="int" order="BEFORE" >
select Play_Sequence.nextval from dual
</selectKey>
insert into play( iid ,typeId) values ( #{ iID ,jdbcType=INTEGER} ,#{typeID,jdbcType=INTEGER})
</insert>
说明:
1、无需为序列创建触发器(trigger),order="BEFORE"确定了在插入数据之前取得主键,再将主键随其他字段一起插入即可。
2、做了一个试验,在创建触发器的情况下,使selectKey的order="AFTER",再使selectKey选择序列的currval,发现currval还未加1。所以只能用1中的方法。
参考:http://tx2099.iteye.com/blog/1413878
0 0
- MyBatis使用MySQL数据库如何在执行insert操作后返回自增的主键
- Mybatis在执行insert语句返回自增主键
- 使用mybatis进行insert操作时返回自增的主键id
- mybatis执行insert实体后返回主键
- jdbc连接mysql数据库执行insert语句后获取自增长的主键值
- mybatis如何实现mysql数据库的主键返回主键返回
- Mybatis+MySQL insert 后取出插入数据的自增主键值
- MyBatis 获取insert操作返回的主键
- Mybatis+MySql 插入数据后返回自增主键值
- MyBatis insert操作返回主键
- MyBatis insert操作返回主键
- MyBatis insert操作返回主键
- MyBatis insert操作返回主键
- MyBatis insert操作返回主键
- MyBatis insert操作返回主键
- MyBatis insert操作返回主键
- MyBatis insert操作返回主键
- MyBatis insert操作返回主键
- Android 编程下 Touch 事件的分发和消费机制
- JDBC封装
- memset, memcpy, strcpy
- 作业P51-4
- 顺序表应用之多项式求和
- MyBatis使用MySQL数据库如何在执行insert操作后返回自增的主键
- 关于梦的解释
- 机器学习演算法 第五讲 Training versus Testing——学习笔记
- sublime编辑器实用插件
- 设计模式GOF23之单例模式理解
- document和window的区别
- 解决Vmware 克隆虚拟机 网卡不能启动的问题
- mmap函数用法及示例程序
- 孙子定理(中国余数定理)