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屬性見下表:

 

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 子元素設置它的值。默認: 不設置。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM