1、问题描述
几种代码写法会有不同的ID
返回值,下面我们一一分析。
2、问题分析
首先一种插入写法,源码如下:
SysUser .java
@Data
@ApiModel("用户管理")
public class SysUser implements Serializable {
private static final long serialVersionUID = 993569543176442301L;
@Id
private Long id;
private String name;
private String nickName;
private String avatar;
private String password;
private String salt;
private String email;
private String mobile;
private Object status;
private Long deptId;
private String createBy;
private Date createTime;
private String lastUpdateBy;
private Date lastUpdateTime;
private Object delFlag;
}
测试类
@Test
void insert() {
SysUser sysUser = new SysUser();
sysUser.setName("zhangsan3");
sysUser.setNickName("张三3");
SysUser insert = sysUserService.insert(sysUser);
System.out.println(insert);
}
结果输出:
SysUser(id=null, name=zhangsan3, nickName=张三3, avatar=null, password=null, salt=null, email=null, mobile=null, status=null, deptId=null, createBy=null, createTime=null, lastUpdateBy=null, lastUpdateTime=null, delFlag=null)
是的我们获取到的是id=null
,这显然不符合我们的预期。于是修改代码。只改测试类。比如输入id=0
。源码如下:
@Test
void insert() {
SysUser sysUser = new SysUser();
sysUser.setId(0L);
sysUser.setName("zhangsan6");
sysUser.setNickName("张三6");
SysUser insert = sysUserService.insert(sysUser);
System.out.println(insert);
}
再次运行程序,控制台输出如下:
SysUser(id=0, name=zhangsan4, nickName=张三4, avatar=null, password=null, salt=null, email=null, mobile=null, status=null, deptId=null, createBy=null, createTime=null, lastUpdateBy=null, lastUpdateTime=null, delFlag=null)
嗯,好像确实输出了id=0
,符合我们的预期,但是真的是这样吗?我们看看数据库限制是什么样。
没错,我们的真实id
是38。这里就有问题了。查阅相关资料说是在实体类中添加@GeneratedValue(strategy = GenerationType.IDENTITY)
就可以了。于是我们照做。修改实体类如下:
@Data
@ApiModel("用户管理")
public class SysUser implements Serializable {
private static final long serialVersionUID = 993569543176442301L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String nickName;
private String avatar;
private String password;
private String salt;
private String email;
private String mobile;
private Object status;
private Long deptId;
private String createBy;
private Date createTime;
private String lastUpdateBy;
private Date lastUpdateTime;
private Object delFlag;
}
主要就是改了这里。OK,继续运行程序,结果如下:
SysUser(id=0, name=zhangsan5, nickName=张三5, avatar=null, password=null, salt=null, email=null, mobile=null, status=null, deptId=null, createBy=null, createTime=null, lastUpdateBy=null, lastUpdateTime=null, delFlag=null)
这里的id
还是0,什么鬼。再看看数据库。
明显不是0。所以这里不能设置id的属性。修改测试类。源码如下:
@Test
void insert() {
SysUser sysUser = new SysUser();
sysUser.setName("zhangsan6");
sysUser.setNickName("张三6");
SysUser insert = sysUserService.insert(sysUser);
System.out.println(insert);
}
继续运行程序,结果如下:
SysUser(id=40, name=zhangsan6, nickName=张三6, avatar=null, password=null, salt=null, email=null, mobile=null, status=null, deptId=null, createBy=null, createTime=null, lastUpdateBy=null, lastUpdateTime=null, delFlag=null)
这次id=40
了,查看数据库。
很明显对上了。
3、总结
到此这篇关于tk.Mybatis 插入数据获取Id问题的文章就介绍到这了,更多相关tk.Mybatis 插入数据获取Id内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!