博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mybatis Generator insert useGeneratedKeys keyProperty
阅读量:6984 次
发布时间:2019-06-27

本文共 3063 字,大约阅读时间需要 10 分钟。

Mybatis自动生成代码,需要用到mybatis Generator,详见http://mybatis.github.io/generator/configreference/generatedKey.html

insert语句如果要返回自动生成的key值,一般会在insert里加入useGeneratedKeys属性,例如

insert into subtasks (SID, TID, RID, START, INTERVALL, SCHEDULE, RESULTS, STYLE) values (#{sid,jdbcType=INTEGER}, #{tid,jdbcType=INTEGER}, #{rid,jdbcType=INTEGER}, #{start,jdbcType=TIMESTAMP}, #{interval,jdbcType=INTEGER}, #{schedule,jdbcType=INTEGER}, #{results,jdbcType=VARCHAR}, #{style,jdbcType=INTEGER})

如果要让generator自动添加该功能,可以如下配置:

则通过generator自动生成的Mapper文件里的insert语句则类似如下:

SELECT LAST_INSERT_ID()
insert into test (TASKID) values (#{taskid,jdbcType=INTEGER})

 

注意:最近才发现一个问题,通过这样generator出的id,如果在插入时已经写了id,则会出错,把已经自己设定的id,更新成上次插入的id。所以这种方法不适合于id不是自动生成而是手动添加的情况!!!

其中generateKey的sqlStatement属性见下表:

 

Attribute Description
column The column name of the generated column.
sqlStatement The SQL statement that will return the new value. If this is an identity column, then you can use one of the predefined special values, or substitute the proper statement for your database. The predefined special values are as follows:
Cloudscape This will translate to: VALUES IDENTITY_VAL_LOCAL()
DB2 This will translate to: VALUES IDENTITY_VAL_LOCAL()
DB2_MF This will translate to: 
SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1

Use this value for DB2 on zOS (Main Frames) and, in some cases, iSeries (AS/400)

Derby This will translate to: VALUES IDENTITY_VAL_LOCAL()
HSQLDB This will translate to: CALL IDENTITY()
Informix This will translate to: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1
MySql This will translate to: SELECT LAST_INSERT_ID()
SqlServer This will translate to: SELECT SCOPE_IDENTITY()
SYBASE This will translate to: SELECT @@IDENTITY
JDBC This will configure MBG to generate code for MyBatis3 suport of JDBC standard generated keys. This is a database independent method of obtaining the value from identity columns.

Important: This value will only produce valid code when the target runtime is MyBatis3. If used with iBATIS2 target runtimes it will produce code with runtime errors.

关于mybatis的mapper文件的xml学习可以参考:http://my.oschina.net/zplswf/blog/63981

其中关于useGeneratedKeys和keyProperty参考下表:

属性 描述
id 在命名空间中唯一的标识符,可以被用来引用这条语句。
parameterType 将会传入这条语句的参数类的完全限定名或别名。
parameterMap 这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。
flushCache 将其设置为 true,不论语句什么时候被带哦用,都会导致缓存被清空。默认值:false。
timeout 这个设置驱动程序等待数据库返回请求结果, 并抛出异常时间的最大等待值。默认不设置(驱动自行处理)。
statementType STA TEMENT,PREPARED 或 CALLABLE 的一种。这会让 MyBatis 使用选择使用 Statement,PreparedStatement 或 CallableStatement。默认值:PREPARED。
useGeneratedKeys ( 仅 对 insert 有 用 ) 这 会 告 诉 MyBatis 使 用 JDBC 的 getGeneratedKeys 方法来取出由数据(比如:像 MySQL 和 SQL Server 这样的数据库管理系统的自动递增字段)内部生成的主键。默认值:false。
keyProperty (仅对 insert 有用) 标记一个属性, MyBatis 会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。默认: 不设置。
keyColumn (仅对 insert 有用) 标记一个属性, MyBatis 会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。默认: 不设置。

转载地址:http://yxvpl.baihongyu.com/

你可能感兴趣的文章
Linux之Ansible入门用法(实验解析)
查看>>
Linux系统如何在开机时修改root密码
查看>>
Anychat的绝对路径与相对路径
查看>>
我的友情链接
查看>>
如何使用网络库实现应用级消息收发
查看>>
Single Area OSPF
查看>>
rhel6之yum
查看>>
selenium+ant+testng测试框架简单介绍
查看>>
自己写的DBUtil数据库连接工具类
查看>>
登录多实例MySQL失败,修改密码临时解决,原因不明
查看>>
SCCM 2007 R2部署、操作详解系列之部署篇
查看>>
hystrix thread pool Metrics
查看>>
MDT2012部署问题,MDT中的驱动是如何工作的
查看>>
Selenium 使用要点记录<二>
查看>>
Windows与Linux系统拷贝文件之pscp的使用
查看>>
_xmlXPathNewContext", referenced from
查看>>
Netty3之ServerBootstrap分析
查看>>
小木木的Python学习笔记
查看>>
用SQL语句添加删除修改字段
查看>>
运维少年系列 python and cisco (1)
查看>>