MySql+MyBatis放回插入的主键
2016-07-11 17:42
645 查看
数据库: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中的方法。
表: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中的方法。
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- Android之获取手机上的图片和视频缩略图thumbnails
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- 数据库链接字符串查询网站
- MySQL 备份和恢复策略
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)
- MySQL数据的导出、导入(mysql内部命令:mysqldump、mysql)
- mysql数据行转列
- Linux下修改MySQL编码的方法