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>

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

其中generateKey的sqlStatement属性见下表:

 

AttributeDescription
columnThe column name of the generated column.
sqlStatementThe 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:
CloudscapeThis will translate to: VALUES IDENTITY_VAL_LOCAL()
DB2This will translate to: VALUES IDENTITY_VAL_LOCAL()
DB2_MFThis 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)

DerbyThis will translate to: VALUES IDENTITY_VAL_LOCAL()
HSQLDBThis will translate to: CALL IDENTITY()
InformixThis will translate to: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1
MySqlThis will translate to: SELECT LAST_INSERT_ID()
SqlServerThis will translate to: SELECT SCOPE_IDENTITY()
SYBASEThis will translate to: SELECT @@IDENTITY
JDBCThis 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这个设置驱动程序等待数据库返回请求结果, 并抛出异常时间的最大等待值。默认不设置(驱动自行处理)。
statementTypeSTA 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 子元素设置它的值。默认: 不设置。
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值