Mybatis:插入数据返回自增主键

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操作加上两个参数

  1. useGeneratedKeys :确定使用自增主键
  2. keyProperty :将主键的值放在实体类的字段中
posted @ 2022-04-15 11:17  今天代码写完了吗  阅读(94)  评论(0编辑  收藏  举报