1. 程式人生 > >Mybatis+Mysql返回主鍵

Mybatis+Mysql返回主鍵

使用MyBatis往MySQL資料庫中插入一條記錄後,成功則返回1,即成功的條數。如果要返回該條記錄的自增主鍵值,在mapper中指定keyProperty屬性,例如:
(1)
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.demo.User">
    insert into user(userName,password,desc) values(#{userName},#{password},#{desc})
</insert>


(2)
<insert id="insert" parameterType="com.demo.User">
<selectKey keyProperty="id" resultType="Long" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
    insert into user(userName,password,desc) values(#{userName},#{password},#{desc})
</insert>

useGeneratedKeys:
取值範圍true|false。預設值是:false。
含義:設定是否使用JDBC的getGenereatedKeys方法獲取主鍵並賦值到keyProperty設定的屬性中。MySQL和SQLServer執行auto-generated key field,因此當資料庫設定好自增長主鍵後,可通過JDBC的getGeneratedKeys方法獲取。但像Oralce等不支援auto-generated key field的資料庫就不能用這種方法獲取主鍵了。

keyProperty:
含義:被設定的目標屬性, MyBatis 會通過 getGeneratedKeys 或者selectKey 子元素設定它的值。預設: 不設定。我們在insert中指定了keyProperty="id",其中id代表插入的User物件的主鍵屬性。

order:
含義:可以被設定為 BEFORE 或 AFTER。如果設定為 BEFORE,那麼它會首先選擇主鍵,設定 keyProperty 然後執行插入語句。如果設定為 AFTER,那麼先執行插入語句,然後是 selectKey 元素,要想取得正確的key值,應設為AFTER。

public class User {
    private Long id; //主鍵
    private String userName;
    private String password;
    private String desc;
    
    //setter and getter
}

public interface UserService {
    public int insert(User user);
}
 
User user = new User();
user.setUserName("test");
user.setPassword("123456");
user.setDesc("測試插入返回主鍵");
userService.insert(user);
Long id = user.getId();

相關推薦

Mybatis+Mysql返回

使用MyBatis往MySQL資料庫中插入一條記錄後,成功則返回1,即成功的條數。如果要返回該條記錄的自增主鍵值,在mapper中指定keyProperty屬性,例如: (1) <insert id="insert" useGeneratedKeys="true" k

mybatis+MySQL UUID生成策略

mybatis mysql uuid主鍵生成<insert id="insert" parameterType="com.gzwb.wbspm.model.SpmInvoice" > <selectKey keyProperty="invoiceId" //主鍵列名 resultType

mybatis插入返回

pan pri generate ron ret val art start cti useGeneratedKeys="true" keyProperty="id" <insert id="insertReturnPrimaryKey" parameterType

mybatis insert 返回id

mybatis insert 返回主鍵id 一、如果是springMVC框架的話。 前提條件: 需要在ibatis  Configuration中設定UseGeneratedKeys為true Configuration cfg = new org.apache.ibati

MyBatis Insert返回和批量插入

一、 insert元素 屬性詳解                               其屬性如下:    parameterType ,入參的全限定類名或類型別名    keyColumn ,設定資料表自動生成的主鍵名。對特定資料庫(如PostgreSQL

深入淺出mybatis返回ID

目錄 新增記錄後獲取主鍵ID,這是一個很常見的需求,特別是在一次前端呼叫中需要插入多個表的場景。 除了新增單條記錄時獲取主鍵值,有時候可能需要獲取批量新增記錄時各記錄的主鍵值,MyBatis從3.3.1版本開始支援批量新增記錄並返回各記錄主鍵欄位值。 新增單一記錄時返

MybatisMySql批量insert後返回

app bsp long 需求 values ram value tis pub 需求:使用批量插入後,需要insert之後的每一條記錄的ID 註意:Mybatis3.3.1的版本以後支持批量插入後返回主鍵ID 示例: domin.java: public class U

MyBatis+MySQL 返回插入記錄的ID

一、nginx基本配置 二、nginx日誌檔案配置 在nginx中conf中的nginx.conf有一段關於日誌檔案的配置 三、製作按時間切割日誌檔案的指令碼 1.製作切割日誌指令碼cutlog.sh 2.使用定時執行命令 (1)crontab -e 進入編輯 (2)輸

mybatis mysql儲存成功返回不生效

mapper.xml配置如下: <insert id="insertReturnKey" parameterType="com.entity.CarBaseBrand" keyProperty="id" useGeneratedKeys="true"> insert int

Mybatis+Mysql 批量插入的時候返回ID

<insert id="insertAlarmLinkmanList" useGeneratedKeys="true" keyProperty="alarmLinkmanId" > insert into alarm_linkman (user_name,

MyBatis框架點滴】——mybatis插入資料返回mysql、oracle)

  向資料庫中插入資料時,大多數情況都會使用自增列或者UUID做為主鍵。主鍵的值都是插入之前無法知道的,但很多情況下我們在插入資料後需要使用剛剛插入資料的主鍵,比如向兩張關聯表A、B中插入資料(A的

MyBatis+MySQL返回插入的ID

需求:使用MyBatis往MySQL資料庫中插入一條記錄後,需要返回該條記錄的自增主鍵值。 Mybatis xml檔案: <insert id="insert" parameterType="

mybatis插入資料時返回(mysql資料庫)

第一種方式使用useGeneratedKeys屬性 User類 public class User { private i

java mybatis中insert 操作 返回的小技巧。。。。

res des lec oracl 嵌入 tid batis 元素 ble 第一種方式: 在實體類的映射文件 "*Mapper.xml" 這樣寫: <insert id="insertvmatedic" keyColumn="mdid" useGeneratedKe

mybatis添加記錄時返回id

測試數據 image creat ger org 設計 efault 需要 generated 參考:https://www.cnblogs.com/nuccch/p/7687281.html 場景 有些時候我們在添加記錄成功後希望能直接獲取到該記錄的主鍵id值,而不需要

通過mybatis添加數據記錄時,如何返回

通過mybatis添加數據記錄時如何返java private SqlSession session = null; @BeforeClass public void init() throws IOException { // SqlSession--->SqlS

mybatismybatis中insert 自增和不自增的插入情況【mysql

pro SQ class TE IV rop generate mys bat 主鍵不自增:返回值是插入的條數 <insert id="add" parameterType="EStudent"> insert into TStudent(name,

Mysql自增長表中添加數據並返回

lec rom sele ast 其中 添加 select insert 查詢 表level,其主鍵為lid 1.select max(id) from table   查詢語句:SELECT MAX(lid) FROM LEVEL        返回插入主鍵 2.sel

mybatis插入數據並返回(oracle)

數據庫 stat batis 還需 varchar 返回 序列值 long values 通常我們執行一個inser語句,即使有返回,也只是會返回影響了多少條數據 @insert("insert into t_user (id,name) values (suser.

mybatis 返回注意事項

有時候當一條記錄插入之後,需要用到當前記錄的id, 方法有二: 一:插入當前記錄之後,在根據條件去表中查詢; 二:插入記錄時直接返回主鍵; 第一種方法就不說了,我就說說第二種方法:       <insert id="insert