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;
posted @ 2020-05-27 18:03  社会大哥  阅读(3379)  评论(0编辑  收藏  举报