实体对象 主键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 获取对象主键值内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!
- 本文固定链接: https://zxbcw.cn/post/195161/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)