正常情況下,我們設置表的主鍵自增,然后:
@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);