1.在研究SpringBoot+MySQL后台接口时记录
<insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.winterchen.model.Address">
INSERT INTO
<include refid="BASE_TABLE"/>
<trim prefix="(" suffix=")" suffixOverrides=",">
name,phone,address_region,address_info,checked
</trim>
<trim prefix="VALUES(" suffix=")" suffixOverrides=",">
#{name, jdbcType=VARCHAR},#{phone, jdbcType=VARCHAR},#{address_region, jdbcType=VARCHAR},#{address_info, jdbcType=VARCHAR},#{checked, jdbcType=VARCHAR}
</trim>
</insert>
注:标签属性useGeneratedKeys="true"表示设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的model属性中,keyProperty="id"主键对应对象的属性
2.在insert后可以调用实体的get方法获取到返回的主键
public int addAddress(Address address) {
int insert = addressDao.insert(address);
System.out.println("插入数据的id为:"+address.getId());
return insert;
}