MybatisPlus中插入数据后获取该对象主键值的实现

时间:2022-04-23 17:42:32

实体对象 主键IdType要设置为AUTO 表示数据库ID自增

?
1
2
3
4
5
6
7
8
9
10
11
12
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class Employee implements Serializable {
 private static final long serialVersionUID = 1L;
 @TableId(value = "id", type = IdType.AUTO)
 private Integer id;
 private String lastName;
 private String email;
 private Integer gender;
 private Integer age;
}

返回的实体就会包含主键值

?
1
2
3
4
5
6
7
8
@PostMapping("add")
@ResponseBody
public Employee addEmployee() {
 Employee employee = new Employee();
 employee.setLastName("chen").setAge(18).setEmail("10000@qq.com").setGender(1);
 employeeService.saveOrUpdate(employee);
 return employee;
}

或者mapper层使用insert方法也会返回主键

?
1
2
3
4
5
@Override
public Employee saveEmp(Employee employee) {
 baseMapper.insert(employee);
 return employee;
}

到此这篇关于MybatisPlus中插入数据后获取该对象主键值的文章就介绍到这了,更多相关MybatisPlus 获取对象主键值内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_45631876/article/details/106517711