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






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM