Mybatis @Insert插入数据返回自增的主键id

mapper层

    @Insert("insert into t_user (username,password,valid,create_time) values (#{username},#{password},#{valid},#{createTime})")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    int add(User user);
​

 

获取返回的id

User user = new User();
user.setUsername("测试");
​
userMapper.add(user);
​
System.out.println("用户的自增id为"+user.getId());

 

 

posted @ 2021-11-01 14:24  武卡卡  阅读(420)  评论(0编辑  收藏  举报