MyBatis 新增数据 成功 返回 自增主键ID值

实现:自增ID值将返回到 Commodity(对应 parameterType="com.ljw.po.Commodity") 新增对象的 commodityId(对于 keyProperty="commodityId") 字段

 

方式一 :添加 useGeneratedKeys="true" keyProperty="commodityId" 

  
    insert into commodity
    
      
        commodity_category_id,
      
      
        commodity_name,
      
    
    
      
        #{commodityCategoryId,jdbcType=INTEGER},
      
      
        #{commodityName,jdbcType=VARCHAR},
      
    
  

方式二 :查询最后自增ID映射到 commodityId 列


    insert into commodity
    
      
        commodity_category_id,
      
      
        commodity_name,
      
    
    
      
        #{commodityCategoryId,jdbcType=INTEGER},
      
      
        #{commodityName,jdbcType=VARCHAR},
      
    
    
    
      select LAST_INSERT_ID() as commodityId
     
  

 

你可能感兴趣的:(MySQL,SpringMVC)