mybatis @SelectKey加於不加的區別


正常情況下,我們設置表的主鍵自增,然后:

    @Insert("insert into miaosha_order (user_id, goods_id, order_id)values(#{userId}, #{goodsId}, #{orderId})")
    public int insertMiaoshaOrder(MiaoshaOrder miaoshaOrder);

可以直接插入,秒殺訂單標的id字段用的是數據庫自增長策略

但是,如何獲在插入后,獲取id的值呢,如果通過查詢獲取id,也太low了,用@SelectKey注解:

select last_insert_id() 取到最后生成的主鍵自動放到pojo的id屬性!!!!!

  // before:在執行插入語句之前,我們設置為flase,既after(在插入這個語句之后,執行select last_insert_id()函數)   //mysql執行函數用select
    //@SelectKey用來獲取插入后的記錄id
    @SelectKey(statement = "select last_insert_id()" ,keyProperty = "id",keyColumn = "id",resultType = long.class,before = false)
    public long insert(OrderInfo orderInfo);

 


免責聲明!

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



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