mybatis獲取數據庫自增id



http://www.iteye.com/problems/86864


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

第二種方法,讓返回值為數據庫id
將加入insert標簽中
<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
SELECT LAST_INSERT_ID() AS ID
</selectKey>





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM