<insert id="insert" parameterType="com.mmall.pojo.Shipping" useGeneratedKeys="true" keyProperty="id">
insert into mmall_shipping (id, user_id, receiver_name,
receiver_phone, receiver_mobile, receiver_province,
receiver_city, receiver_district, receiver_address,
receiver_zip, create_time, update_time
)
values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{receiverName,jdbcType=VARCHAR},
#{receiverPhone,jdbcType=VARCHAR}, #{receiverMobile,jdbcType=VARCHAR}, #{receiverProvince,jdbcType=VARCHAR},
#{receiverCity,jdbcType=VARCHAR}, #{receiverDistrict,jdbcType=VARCHAR}, #{receiverAddress,jdbcType=VARCHAR},
#{receiverZip,jdbcType=VARCHAR}, now() ,now()
)
</insert>
在insert节点加上如下配置 是否使用 useGeneratedKeys 开关为true,keyProperty对应的就是要返回的字段名称
useGeneratedKeys="true" keyProperty="id"
在成功的插入数据后,会把设置的主键id填充到该shipping对象中要拿到id的值就可以直接使用shipping.getId();
public ResponseService add(Shipping shipping,Integer userId){
shipping.setUserId(userId);
int resultCount = shippingMapper.insert(shipping);
if (resultCount > 0){
Map result = Maps.newHashMap();
result.put("shippingId",shipping.getId());
return ResponseService.createBySuccess("添加地址成功",result);
}else{
return ResponseService.createByErrorMessage("添加地址失败");
}
}