首页 > 编程语言 > MybatisPlus中插入数据后获取该对象主键值的实现
2020
10-10

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

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

@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;
}

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

@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方法也会返回主键

@Override
public Employee saveEmp(Employee employee) {
 baseMapper.insert(employee);
 return employee;
}

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

编程技巧