1. 程式人生 > >Mybatis自動生成key值(selectKey和useGeneratedKeys)

Mybatis自動生成key值(selectKey和useGeneratedKeys)

insert和update操作中會常常用到自動生成主鍵的問題。

  1. selectKey和useGeneratedKeys屬性
    useGeneratedKeys (insert and update only) This tells MyBatis to use the JDBC getGeneratedKeys method to retrieve keys generated internally by the database (e.g.auto increment fields in RDBMS like MySQL or SQL Server). Default: false
    (( 僅 對 insert 和update有 用 ) 這 會 告 訴 MyBatis 使 用 JDBC 的 getGeneratedKeys 方法來取出由資料(比如:像 MySQL 和 SQL Server 這樣的資料庫管理系統的自動遞增欄位)內部生成的主鍵。預設值:false。)
    keyProperty

    (insert and update only) Identifies a property into which MyBatis will set the key value returned by
    getGeneratedKeys , or by a selectKey child element of the insert statement. Default: unset .
    Can be a comma separated list of property names if multiple generated columns are expected.
    ((僅對 insert和update有用) 標記一個屬性, MyBatis會通過 getGeneratedKeys 或者通過 insert 語句的 selectKey 子元素設定它的值。預設: 不設定。)
    keyColumn

    (insert and update only) Sets the name of the column in the table with a generated key. This is only required
    in certain databases (like PostgreSQL) when the key column is not the first column in the table. Can be a
    comma separated list of columns names if multiple generated columns are expected.
  2. selectKey和useGeneratedKeys使用
<insert id="insert">
 <selectKey keyProperty="id" resultType="int" order="BEFORE">
  <if test="_databaseId == 'oracle'">
   select seq_users.nextval from dual
  </if>
  <if test="_databaseId == 'db2'">
   select nextval for seq_users from sysibm.sysdummy1"
  </if>
 </selectKey>
 insert into users values (#{id}, #{name})
</insert>

通過selectKey在插入操作前或者操作後獲取key值,做為欄位插入或返回欄位。(此段程式碼獲取的序列值id作為欄位值插入到users表中)

<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
    insert into Author (username,password,email,bio)
    values (#{username},#{password},#{email},#{bio})
</insert>

如果資料庫支援自增長主鍵欄位(比如mysql、sql server)設定useGeneratedKeys=”true”和keyProperty,這樣就可以插入主鍵id值
oracle則不支援自增長id,設定useGeneratedKey=”false”,如果設定true則會有報錯資訊。通過nextval函式,如SEQ_table.Nextval生成id

3.插入更新一條資料時,可以使用selectKey獲取id操作。當做多條資料插入更新時,而selectKey只能使用一次,此時應該使用useGeneratedKeys操作。

相關推薦

Mybatis自動生成keyselectKeyuseGeneratedKeys

insert和update操作中會常常用到自動生成主鍵的問題。 selectKey和useGeneratedKeys屬性 useGeneratedKeys (insert and update only) This tells

Mybatis插入時返回自增主鍵selectKeyuseGeneratedKeys

通過selectKey在插入操作前或者操作後獲取key值,做為欄位插入或返回欄位。(此段程式碼獲取的序列值id作為欄位值插入到實體類中返回)  <insert id="insert"> <selectKey keyProperty="id" result

MySQL KeyPRI, UNI, MUL的含義

key值 target .cn 唯一約束 AR ica logs htm hive PRI主鍵約束; UNI唯一約束; MUL可以重復。 參考:http://www.cnblogs.com/licheng/archive/2010/10/16/1852938.htmlMyS

Qt:計算CRC校驗CRC16CRC32

CRC有非常多的模式,我沒有全部都做,目前支援 CRC16-Modbus CRC16-X25 CRC32 使用方法 auto data = QByteArray::fromHex( "01

Ajax中的async的取非同步同步

之前去面試廣州科騰公司時,面試官問了一個問題—ajax的async取值的理解。那時候的我可以說是個死菜鳥(現在可能是個半死不活的菜鳥),只是理解取值的字面意思,最後還要面試官給我解釋。雖然面試肯定是掛了,但是至少面試官教了我點東西,也不虧了對吧。言歸正傳了。

mybatis之map傳參List物件

map傳參在我們實際開發會經常用到,也是一種特別方便的傳參方式。 話不多說上程式碼 service層:、 Map<String, Object> params = new HashMap<String, Object>(); Lis

MyBatis自動生成實體類、DAO介面Mapping對映檔案的程式碼逆向工程

MyBatis屬於一種半自動的ORM框架,它需要程式設計師自己編寫sql語句和對映檔案,但是編寫對映檔案和sql語句很容易出錯,所以mybatis官方提供了Generator生成器,自動生成DAO介面。實體類和Mapping。這個生成器是根據單表自動生成myba

Mybatis自動生成配置實體類內附jar包資源

相對於Hibernate而言,mybatis是一個“實體類↔sql語句”的對映框架,所以說它是一個半自動化的對映框架。 開發的時候需要我們自己配置對映檔案、自己編寫SQL語句,這和容易導致書

Mybatis自動生成實體類實體映射工具

ase 忽略 路徑 package mapping decimal rri upd 需要 Mybatis Mysql生成實體類 用到的Lib包: mybatis-generator-core-1.3.2.jarmysql-connector-java-5.1.30.jar

Mybatis Generator自動生成JavaEntity帶中文註釋註解[Maven]工程

mybatis-generator是一個很好的資料庫逆向生成Java實體的外掛,但不能新增中文註釋和註解,如果我們想生成下面這樣一個UserEntity,那麼就只能自己動手擴充套件一下mybatis-generator。 package com.domain.entity; import com

idea + groovy + mybatis 自動生成 Dao、mappings 實體類

背景 在 windows 系統中,idea 在 C:\Users\使用者名稱\.IntelliJIdea2018.2\config\extensions\com.intellij.database\schema 目錄下預設存在如下 Groovy 檔案:Generate POJOs.g

IntelliJ IDEA相關小技巧外掛 | Mybatis自動生成工具

本文簡單介紹一些idea開發常用外掛和工具,以及一些小技巧,不足之處希望大家指出,我改正。不喜勿噴! 一、IDEA開發小技巧 在使用idea開發過程中我們會對介面有一些潔癖(簡稱強迫症),下面簡單介紹一下個人的習慣: 1.idea如何檢視檔案或專案的歷史提交記錄 在

MyBatis的sql對映檔案CRUD主鍵id生成

一個好的講解mybatis的部落格地址http://www.jianshu.com/nb/5226994 官方文件:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html Mapper XML 檔案 MyBatis 的真正強大在於它的對映語

Spring boot入門:SpringBoot整合結合AdminLTE(Freemarker),利用generate自動生成程式碼,利用DataTablePageHelper進行分頁顯示

  關於SpringBoot和PageHelper,前篇部落格已經介紹過Spring boot入門(二):Spring boot整合MySql,Mybatis和PageHelper外掛,前篇部落格大致講述了SpringBoot如何整合Mybatis和Pagehelper,但是沒有做出實際的範例,本篇部落格是連

Eclipse+MyBatis自動生成Mapper實體還有對應sqlxml

1.安裝外掛(eclipse的marketplace中搜索mybatis就行了) 2.建立generatorConfig.xml檔案(配置資料庫等資訊,讀懂這個檔案的內容就行了) generatorConfig.xml檔案 <?x

使用mavenmybatis自動生成程式碼配置

使用自動生成真的很方便,之前試過手寫,但是無奈太慢而且容易出錯,一急之下馬上去 研究了下,沒想到2個小時不到就研究出來了,廢話不多說,我們正式開始, 我自己的個人網站就用到了這種技術 如果需要生成資料庫欄位的註釋,請下載:點我開啟 連結內的檔案,裡面有詳細教程    

mybatis自動生成對應表的實體類、介面對映檔案

使用maven構建的專案,其中整合了mybatis,每次使用時都需要建立model實體類,dao介面和mapper對映檔案,為了方便開發,maven中有一種外掛可以直接生成資料庫表對應的實體類,介面和對映檔案 1.首先要在pom.xml檔案中加入外掛依賴,注意直接放在<

mybatis自動生成entity層dao層中Mapper介面中的各個方法的意義及example實體類的用法

package cn.lichenyang.emall.dao; import cn.lichenyang.emall.entity.TbContent; import cn.lichenyang.emall.entity.TbContentExample; import

利用mybatis-generator自動生成JAVA的實體類mapping等

一:引入mybatis-generator-core jar包 我的測試專案是使用的maven,pom.xml對應如下: <!-- Mybatis Generator --><dep

Mybatis自動生成對映檔案PO物件呼叫示例程式碼工

 src/generatorConfig.properties suppressAllComments=false driverClass=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@localhost:152