mybatis获取自增长id

需求

mybatis在插入数据的同时获取自增长id的值以供接下来的业务使用;

 

代码

mapper

public interface CustomizedConditionMapper {
    //插入数据并返回当前id
    int insertSelective(CustomizedConditionPO customizedConditionPO);
}

 

mapper.xml

注意这里要加:useGeneratedKeys="true" keyProperty="id"


    insert into report_customized_condition
    
      
        id,
      
      
        customized_conditions_id,
      
      
        condition_id,
      
      
        created_time,
      
      
        updated_time,
      
      
        created_by,
      
      
        updated_by,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{customizedConditionsId,jdbcType=INTEGER},
      
      
        #{conditionId,jdbcType=INTEGER},
      
      
        #{createdTime,jdbcType=TIMESTAMP},
      
      
        #{updatedTime,jdbcType=TIMESTAMP},
      
      
        #{createdBy,jdbcType=VARCHAR},
      
      
        #{updatedBy,jdbcType=VARCHAR},
      
    
  

 

业务代码

注意这边不能直接用

Integer customizedConditionsId=customizedConditionsMapper.insertCustomizedConditions(customizedConditionsPO);

这个是影响多少行,而应该使用下面的方式获取!!!

customizedConditionsMapper.insertCustomizedConditions(customizedConditionsPO);
int customizedConditionsId = customizedConditionsPO.getId();

补充说明

该数据表的id务必要设置成自增长!!!

你可能感兴趣的:(mybatis)