正文
springboot + mybatis plus使用insert 语句并返回主键
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
mapper文件
映射文件中在insert中设置useGeneratedKeys为true,keyProperty设置为主键名称
<insert id="addEmployees" useGeneratedKeys="true" keyProperty="id">
INSERT
INTO
employees (emp_name, emp_age, emp_no, hire_date, sal, deptno, mgr,
user_name, email, phone_number, sex, emp_password, status, remark)
VALUES(#{empName},#{empAge},#{empNo},#{hireDate},cast(#{sal} as money),#{deptno},#{mgr},
#{userName},#{email},#{phoneNumber},#{sex},#{empPassword},#{status},#{remark})
</insert>
在业务层中使用对象的getter方法得到自动化生成的主键值
参考资料:
https://www.cnblogs.com/Lyn4ever/p/11390497.html
来自为知笔记(Wiz)