mybatis和mybatisPlus中解决实体类字段与数据库关键字冲突问题

mybatis和mybatisPlus中解决实体类字段与数据库关键字冲突问题
强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

写在前面

  由于数据库表字段名称设计不合理,导致与MySQL数据库关键字或者预留关键字一致,在这种情况下,将会导致数据插入不成功,这里主要针对Mybatis和MybatisPlus提出对应的解决方案,仅供参考!

一、Mybatis中解决方案

1、针对XML文件,可以在冲突的字段名添加 ` ` ( 反单引号在 键盘Esc键下面的那个,注意切换英文输入法 ) 引起来,如下所示:

<insert id="addParam"  useGeneratedKeys="true" keyProperty="id"  parameterType="com.pct.dotware.pams.entity.SysParam">
        insert into sys_param
        (
            `key`,
            `value`,
            `status`,
            `create_by`,
            `create_time`,
            `remark`
        )
        values(
            #{item.key},
            #{item.value},
            #{item.status},
            #{item.createBy},
            #{item.createTime},
            #{item.remark}
        )
</insert>        

2、如果不想修改xml文件,也可针对实体类进行修改,可以通过添加@Column注解,如下所示:

@Column(name = "`key`")
private String key;

针对Mybatis,以上两种任选其一即可。

二、MybatisPlus解决方案

对于MybatisPlus解决就很简单了,直接在实体类上添加  @TableField 注解,给上别名加上反单引号即可。如下所示:

@TableFiled(name = "`key`")
private String key;

附录:

  MySQL关键字列表:

ADD ALL ALTER
ANALYZE AND AS
ASC ASENSITIVE BEFORE
BETWEEN BIGINT BINARY
BLOB BOTH BY
CALL CASCADE CASE
CHANGE CHAR CHARACTER
CHECK COLLATE COLUMN
CONDITION CONNECTION CONSTRAINT
CONTINUE CONVERT CREATE
CROSS CURRENT_DATE CURRENT_TIME
CURRENT_TIMESTAMP CURRENT_USER CURSOR
DATABASE DATABASES DAY_HOUR
DAY_MICROSECOND DAY_MINUTE DAY_SECOND
DEC DECIMAL DECLARE
DEFAULT DELAYED DELETE
DESC DESCRIBE DETERMINISTIC
DISTINCT DISTINCTROW DIV
DOUBLE DROP DUAL
EACH ELSE ELSEIF
ENCLOSED ESCAPED EXISTS
EXIT EXPLAIN FALSE
FETCH FLOAT FLOAT4
FLOAT8 FOR FORCE
FOREIGN FROM FULLTEXT
GOTO GRANT GROUP
HAVING HIGH_PRIORITY HOUR_MICROSECOND
HOUR_MINUTE HOUR_SECOND IF
IGNORE IN INDEX
INFILE INNER INOUT
INSENSITIVE INSERT INT
INT1 INT2 INT3
INT4 INT8 INTEGER
INTERVAL INTO IS
ITERATE JOIN KEY
KEYS KILL LABEL
LEADING LEAVE LEFT
LIKE LIMIT LINEAR
LINES LOAD LOCALTIME
LOCALTIMESTAMP LOCK LONG
LONGBLOB LONGTEXT LOOP
LOW_PRIORITY MATCH MEDIUMBLOB
MEDIUMINT MEDIUMTEXT MIDDLEINT
MINUTE_MICROSECOND MINUTE_SECOND MOD
MODIFIES NATURAL NOT
NO_WRITE_TO_BINLOG NULL NUMERIC
ON OPTIMIZE OPTION
OPTIONALLY OR ORDER
OUT OUTER OUTFILE
PRECISION PRIMARY PROCEDURE
PURGE RAID0 RANGE
READ READS REAL
REFERENCES REGEXP RELEASE
RENAME REPEAT REPLACE
REQUIRE RESTRICT RETURN
REVOKE RIGHT RLIKE
SCHEMA SCHEMAS SECOND_MICROSECOND
SELECT SENSITIVE SEPARATOR
SET SHOW SMALLINT
SPATIAL SPECIFIC SQL
SQLEXCEPTION SQLSTATE SQLWARNING
SQL_BIG_RESULT SQL_CALC_FOUND_ROWS SQL_SMALL_RESULT
SSL STARTING STRAIGHT_JOIN
TABLE TERMINATED THEN
TINYBLOB TINYINT TINYTEXT
TO TRAILING TRIGGER
TRUE UNDO UNION
UNIQUE UNLOCK UNSIGNED
UPDATE USAGE USE
USING UTC_DATE UTC_TIME
UTC_TIMESTAMP VALUES VARBINARY
VARCHAR VARCHARACTER VARYING
WHEN WHERE WHILE
WITH WRITE X509
XOR YEAR_MONTH ZEROFILL
本文来源星空流年,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/6922

发表评论