Mybatis可以把自增主键的值放到实体中返回
这个是实体类
public class UserEntity {
private Long id;
private String username;
private String type;
private String name;
private Long number;
private String password;
}
xml如下:
<insert id="insert" parameterType="com.example.springboot.entity.UserEntity" keyProperty="id" useGeneratedKeys="true" >
insert into `t_user`(user_name, type,number) VALUES (#{username},#{type},1)
</insert>
需要给insert操作加上两个参数
useGeneratedKeys
:确定使用自增主键keyProperty
:将主键的值放在实体类的字段中