mybatis和mybatisPlus中解決實體類字段與數據庫關鍵字沖突問題


可能你插入字段為關鍵字時報如下錯誤,且字段名不適合改變

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

一.mybatis中

方案一:如果是在xml文件中,插入語句時可以加上` `,例如

<!--批量新增-->
    <insert id="addBatch"  useGeneratedKeys="true" keyProperty="id"  parameterType="com.pct.dotware.pams.entity.EmpStsDetail">
        insert into emp_sts_detail
        (
            `emp_name`,
            `emp_id`,
            `item_id`,
            `item_name`,
            `price_id`,
            `emp_sts`,
            `cus_id`,
            `cus_name`,
            `cus_rank`,
            `start_date`,
            `end_date`,
            `update_date`,
            `remark`
        )
        values
        <foreach item="item" collection="list" open="(" separator="),(" close=")">
            #{item.empName},
            #{item.empId},
            #{item.itemId},
            #{item.itemName},
            #{item.priceId},
            #{item.empSts},
            #{item.cusId},
            #{item.cusName},
            #{item.cusRank},
            #{item.startDate},
            #{item.endDate},
            #{item.updateDate},
            #{item.remark}
        </foreach>
    </insert>

方案二:在實體類中加入注解

@Column(name = "`left`")
private Double left;

二.mybatisPlus中

方案一.加@TableField注解,給上別名加上反單引號,比如

@TableField("`month`")
private String month;


免責聲明!

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



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