Mybatis Generator insert useGeneratedKeys keyProperty

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

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


<insert id="insert" parameterType="cn.ac.iscas.pebble.ufe.bean.Subtask"
useGeneratedKeys="true" keyProperty="sid">
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})
</insert>

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

    <table schema="cc" tableName="test" domainObjectName="TT"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<generatedKey column="taskid" sqlStatement="SELECT LAST_INSERT_ID()"/>
</table>

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


<insert id="insert" parameterType="cn.ac.iscas.pebble.ufe.bean.TT" >
<selectKey resultType="java.lang.Integer" keyProperty="taskid" order="BEFORE" >
SELECT LAST_INSERT_ID()
</selectKey>
insert into test (TASKID)
values (#{taskid,jdbcType=INTEGER})
</insert>

其中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 子元素设置它的值。默认: 不设置。

Mybatis Generator insert useGeneratedKeys keyProperty,布布扣,bubuko.com

时间: 2024-08-08 05:38:48

Mybatis Generator insert useGeneratedKeys keyProperty的相关文章

Mybatis Generator最完整配置详解

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <

MyBatis Generator XML 配置参考

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <

Mybatis Generator.xml最完整配置详解

作者:小码哥Java学院 链接:http://www.jianshu.com/p/e09d2370b796 來源:简书 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://my

最完整的Mybatis Generator(简称MBG)的最完整配置文件,带详解,再也不用去看EN的User Guide了;

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <

mybatis generator使用总结

一.mybatis项目的体系结构 百度mybaits,可以进入mybatis的github:https://github.com/mybatis. mybatis是一个大大的体系,它不是孤立的,它可以和许多其他框架或技术配合使用. mybatis下的子项目有如下几类: *与IOC框架之间的配合:spring和guice,guice跟spring是同类产品,但是它使用注解方式完全替代spring,比spring更轻量,来自谷歌. *与缓存之间的配合:各种cache都有,有memcached,caf

mybatis generator的generatorConfig.xml配置详解

generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1

Maven插件方式使用Mybatis Generator

Mybatis Generator Mybatis Generator简称MBG,可以根据数据库自动生成实体类.单表查询接口及其映射xml文件(也可以选择以注解方式生成). 下面介绍一下以maven插件方式使用MBG. 首先在pom中添加依赖: <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-cor

MyBatis - Generator(MBG)

前言 MyBatis Generator简称MBG,是一个专门为MyBatis框架使用者定制的代码生成器, 可以快速的根据数据表自动生成Bean对象.Java接口及SqlMapper.xml配置文件. 支持基本的增删改查,以及QBC风格的条件查询,这样能够大大减少我们平时开发的工作量. 但是表连接.存储过程等这些复杂sql的定义需要我们手工编写. 传送门:官方文档 下载 Mybatis Generator最完整配置详解 1 <?xml version="1.0" encoding

Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解

生成器设计思路: 连接数据库 -> 获取表结构 -> 生成文件 1 下载与安装 官网文档入口 最方便的 maven 插件使用方式 贴至pom 文件 2 新建配置文件 填充配置信息(官网示例) 项目实例 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Genera