@[doc]
开发过程中,向表中插入数据时,会用到自增ID。在插入数据后,想获取到该数据的ID,要怎么操作那?
别急,这时候需要一个配置:在对应的mapper文件中,添加useGeneratedKeys="true" keyProperty="id"
就可以了。
<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>
useGeneratedKeys :设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中;取值范围为true、false;默认为false。
keyProperty :取id的key值。
设置完成后,我们就可以获取到该自增的id了!