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