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
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 子元素设置它的值。默认: 不设置。
原文地址:https://www.cnblogs.com/dorothychai/p/3760031.html