mybatis提供了注解方式編寫sql,省去了配置並編寫xml mapper文件的麻煩,今天遇到了獲取自增長主鍵返回值的問題,發現相關問答比較少,還好最后還是圓滿解決了,現把重點記錄一下,解決問題的關鍵就是以下幾行代碼:
1 @Insert("insert into Product(title, image, price, detail, summary, seller) values(#{title},#{image},#{price},#{detail},#{summary},#{seller})") 2 @Options(useGeneratedKeys=true, keyProperty="id")//添加該行,product中的id將被自動添加 3 public Integer insertProduct(Product product);
添加上面的第二行就可以了,其中第二個參數據說可以不需要
添加該注解后
在數據庫中添加成功后,product的id屬性就會被默認賦值。