正常情况下,我们设置表的主键自增,然后:
@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);