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

2025-05-29 0 25

实体对象 主键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 获取对象主键值内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 MybatisPlus中插入数据后获取该对象主键值的实现 https://www.kuaiidc.com/117448.html

相关文章

发表评论
暂无评论