http://www.iteye.com/problems/86864
insert標簽中加上
useGeneratedKeys="true" keyProperty="id"
會將id組裝到返回對象中而不是作為返回值
官方文檔只這么說的:useGeneratedKeys該屬性會告訴Mybatis使用JDBC的getGeneratedKeys方法來取出由數據(比如:像Mysql和Sql server這樣數據庫管理系統的自動遞增字段)內部生成的主鍵。默認值:false。
<insert id="insertSelective" parameterType="com.lcworld.jiunixing.model.User" useGeneratedKeys="true" keyProperty="id" >
獲取id方法
返回值i並不是id而是影響行數,getId才是數據庫自增的id
int i=userService.insertSelective(record);
System.out.println(i);
System.out.println(record.getId());
將加入insert標簽中
<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID() AS ID
</selectKey>