MybatisPlus如何实现插入数据并获取新插入对象的主键值?
- 内容介绍
- 文章标签
- 相关推荐
本文共计234个文字,预计阅读时间需要1分钟。
实体对象,主键Id使用Type设置为AUTO,表示数据库ID自增。使用@Data注解实现Equals和HashCode,@Accessors注解链式访问。以下为Employee类的简化代码:
java@Entity@TableId(type=IdType.AUTO)@Data@Accessors(chain=true)public class Employee implements Serializable { private static final long serialVersionUID=1L;}
实体对象 主键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 获取对象主键值内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计234个文字,预计阅读时间需要1分钟。
实体对象,主键Id使用Type设置为AUTO,表示数据库ID自增。使用@Data注解实现Equals和HashCode,@Accessors注解链式访问。以下为Employee类的简化代码:
java@Entity@TableId(type=IdType.AUTO)@Data@Accessors(chain=true)public class Employee implements Serializable { private static final long serialVersionUID=1L;}
实体对象 主键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 获取对象主键值内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

