xml: java
<insert id="接口方法名" parameterType="實體類" keyProperty="實體類中表明id的變量名" keyColumn="主鍵字段名" useGeneratedKeys="true" > insert into 表(字段1,字段2,... )VALUES(#{屬性1},#{屬性2},...) </insert>
基於註解: spa
@Insert("insert into 表(字段1,字段2,... )VALUES(#{屬性1},#{屬性2},...)") @SelectKey(before = false, keyProperty = "實體類中表明id的變量名", resultType = Integer.class, statement = { "select last_insert_id()" }) public Integer addCominfo(形參類型 形參);
before:值爲true:在插入數據以前獲取主鍵ID;值false:插入數據後獲取主鍵ID,如MySQL中主鍵自動增加 code