调试centerBank接口,业务实现类里面想要通过获取表格中主键id,来进行逻辑判断,从而进行更新;
直接撸代码–mapper.xml中的插入方法:
<insert id="insertSelective" parameterType="com.baidu.example.entity.CenterBankRecordsEntity" useGeneratedKeys="true" keyProperty="id">
insert into center_bank_records
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="startDate != null" >
start_date,
</if>
<if test="endDate != null" >
end_date,
</if>
<if test="orderId != null" >
order_id,
</if>
<if test="returnCode != null" >
return_code,
</if>
<if test="returnMessage != null" >
return_message,
</if>
<if test="createTime != null" >
create_time,
</if>
<if test="lastModifyTime != null" >
last_modify_time,
</if>
<if test="status != null" >
status,
</if>
<if test="mac != null" >
mac,
</if>
<if test="userId != null" >
user_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=BIGINT},
</if>
<if test="orderId!= null" >
#{orderId,jdbcType=VARCHAR},
</if>
<if test="startDate != null" >
#{startDate,jdbcType=BIGINT},
</if>
<if test="endDate != null" >
#{endDate,jdbcType=BIGINT},
</if>
<if test="returnCode != null" >
#{returnCode,jdbcType=VARCHAR},
</if>
<if test="returnMessage != null" >
#{returnMessage,jdbcType=VARCHAR},
</if>
<if test="createTime != null" >
#{createTime,jdbcType=BIGINT},
</if>
<if test="lastModifyTime != null" >
#{lastModifyTime,jdbcType=BIGINT},
</if>
<if test="status != null" >
#{status,jdbcType=VARCHAR},
</if>
<if test="mac != null" >
#{mac,jdbcType=VARCHAR},
</if>
<if test="userId != null" >
#{userId,jdbcType=VARCHAR},
</if>
</trim>
</insert>
然后想通过service实现类里面的判断id是否存在,此时的id的存在与否,就在于我们在上方代码中是否设置useGeneratedKeys的值为true,
实现类中的代码(entity也是CenterBankRecordsEntity定义的对象,只是之前赋值过部分变量):
CenterBankRecordsEntity updateEntity=new CenterBankRecordsEntity();
if( entity.getId() != null) {
updateEntity.setId(entity.getId());
if (Constants.SUCCESS.equals(resultInfo.getRspCode())) {
updateEntity.setStatus(StatusEnum.SUCCESS.toString());
} else {
updateEntity.setStatus(StatusEnum.FAILED.toString());
}
updateEntity.setReturnCode(result.getResult_code());
updateEntity.setReturnMessage(result.getResult_msg());
updateEntity.setLastModifyTime(DateUtil.getCurrentTime());
centerBankRecordsDao.updateByPrimaryKeySelective(updateEntity);
}
return resultInfo;
}
综述:
useGeneratedKeys 取值是boolean 类型,要么是true,要么是false; 默认值是:false。
它的含义:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的model属性中。