在操作數據庫中,插入一條記錄,表中的主鍵是自增的,如果我們需要獲得這個主鍵,就需要在插入的時候,返回這條主鍵,寫法如下:
INSERT INTO `gusers_xxxx_log`
(`user_id`,
`admin_user`,
`change_amount`,
`total_amount`,
`add_time`,
`paid_time`,
`reason`,
`gift_card_id`,
`business_id`,
`is_paid`
)
VALUES
(#{userId},
0,
#{changeAmount},
#{totalAmount},
#{addTime},
0,
#{reason},
#{giftCardId},
0,
#{isPaid}
)
<selectKey keyProperty="logId" resultType="int">
SELECT @@IDENTITY
AS logId
</selectKey>
上述sql語句,在日誌表中插入一條記錄,根據定義的實體類,返回的主鍵id爲logId, /**
* 日誌ID
*/
private Integer logId;
通過log.getLogId()即可獲得這條日誌記錄的主鍵id值