一、更改mapper文件
增加useGeneratedKeys, keyProperty, keyColumn三个属性。keyProperty和keyColumn分别代表数据库记录主键字段和java对象成员属性名
<insert id="insert" parameterType="com.product.selftraining.repo.entity.Consumer"
useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into consumer (id, email, account,
username, chathead, signature,
regTime, password, type,
telephone)
values (#{id,jdbcType=INTEGER}, #{email,jdbcType=VARCHAR}, #{account,jdbcType=DOUBLE},
#{username,jdbcType=VARCHAR}, #{chathead,jdbcType=VARCHAR}, #{signature,jdbcType=VARCHAR},
#{regtime,jdbcType=TIMESTAMP}, #{password,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER},
#{telephone,jdbcType=VARCHAR})
</insert>
二、如何使用
1.错误的示范:其实xml里面sql执行后返回的值是受影响行数
public Result register(LoginInfo user) {
Long userId = loginMapper.insertLogin(user);
System.out.println(userId);
}
2.正确方式
public Result register(LoginInfo user) {
loginMapper.insertLogin(user);
System.out.println(user.getUserId());
}