在mybatis-plus中,由于extends BaseMapper,因此我们是不需要手动写sql语句的,因此我们不会遇到语法错误,但是我们可能会遇到下面列举的错误:
(1)org.springframework.jdbc.BadSqlGrammarException:Cause: java.sql.SQLSyntaxErrorException
出现该异常的原因:数据库中我们设置的字段名称和数据库中的关键字名称重名,需要更改与数据库中关键字重名的字段名称。
MySQL 关键字和保留字汇总(MySQL 8.0)[1]:
MySQL 关键字以及保留字
A
ACCESSIBLE
®ACCOUNT
ACTION
ACTIVE
; added in 8.0.14 (nonreserved)ADD
®ADMIN
; became nonreserved in 8.0.12AFTER
AGAINST
AGGREGATE
ALGORITHM
ALL
®ALTER
®ALWAYS
ANALYSE
; removed in 8.0.1ANALYZE
®AND
®ANY
ARRAY
; added in 8.0.17 (reserved); became nonreserved in 8.0.19AS
®ASC
®ASCII
ASENSITIVE
®AT
ATTRIBUTE
; added in 8.0.21 (nonreserved)AUTOEXTEND_SIZE
AUTO_INCREMENT
AVG
AVG_ROW_LENGTH
B
BACKUP
BEFORE
®BEGIN
BETWEEN
®BIGINT
®BINARY
®BINLOG
BIT
BLOB
®BLOCK
BOOL
BOOLEAN
BOTH
®BTREE
BUCKETS
; added in 8.0.2 (nonreserved)BY
®BYTE
C
CACHE
CALL
®CASCADE
®CASCADED
CASE
®CATALOG_NAME
CHAIN
CHANGE
®CHANGED
CHANNEL
CHAR
®CHARACTER
®CHARSET
CHECK
®CHECKSUM
CIPHER
CLASS_ORIGIN
CLIENT
CLONE
; added in 8.0.3 (nonreserved)CLOSE
COALESCE
CODE
COLLATE
®COLLATION
COLUMN
®COLUMNS
COLUMN_FORMAT
COLUMN_NAME
COMMENT
COMMIT
COMMITTED
COMPACT
COMPLETION
COMPONENT
COMPRESSED
COMPRESSION
CONCURRENT
CONDITION
®CONNECTION
CONSISTENT
CONSTRAINT
®CONSTRAINT_CATALOG
CONSTRAINT_NAME
CONSTRAINT_SCHEMA
CONTAINS
CONTEXT
CONTINUE
®CONVERT
®CPU
CREATE
®CROSS
®CUBE
®; became reserved in 8.0.1CUME_DIST
®; added in 8.0.2 (reserved)CURRENT
CURRENT_DATE
®CURRENT_TIME
®CURRENT_TIMESTAMP
®CURRENT_USER
®CURSOR
®CURSOR_NAME
D
DATA
DATABASE
®DATABASES
®DATAFILE
DATE
DATETIME
DAY
DAY_HOUR
®DAY_MICROSECOND
®DAY_MINUTE
®DAY_SECOND
®DEALLOCATE
DEC
®DECIMAL
®DECLARE
®DEFAULT
®DEFAULT_AUTH
DEFINER
DEFINITION
; added in 8.0.4 (nonreserved)DELAYED
®DELAY_KEY_WRITE
DELETE
®DENSE_RANK
®; added in 8.0.2 (reserved)DESC
®DESCRIBE
®DESCRIPTION
; added in 8.0.4 (nonreserved)DES_KEY_FILE
; removed in 8.0.3DETERMINISTIC
®DIAGNOSTICS
DIRECTORY
DISABLE
DISCARD
DISK
DISTINCT
®DISTINCTROW
®DIV
®DO
DOUBLE
®DROP
®DUAL
®DUMPFILE
DUPLICATE
DYNAMIC
E
EACH
®ELSE
®ELSEIF
®EMPTY
®; added in 8.0.4 (reserved)ENABLE
ENCLOSED
®ENCRYPTION
END
ENDS
ENFORCED
; added in 8.0.16 (nonreserved)ENGINE
ENGINES
ENGINE_ATTRIBUTE
; added in 8.0.21 (nonreserved)ENUM
ERROR
ERRORS
ESCAPE
ESCAPED
®EVENT
EVENTS
EVERY
EXCEPT
®EXCHANGE
EXCLUDE
; added in 8.0.2 (nonreserved)EXECUTE
EXISTS
®EXIT
®EXPANSION
EXPIRE
EXPLAIN
®EXPORT
EXTENDED
EXTENT_SIZE
F
FAILED_LOGIN_ATTEMPTS
; added in 8.0.19 (nonreserved)FALSE
®FAST
FAULTS
FETCH
®FIELDS
FILE
FILE_BLOCK_SIZE
FILTER
FIRST
FIRST_VALUE
®; added in 8.0.2 (reserved)FIXED
FLOAT
®FLOAT4
®FLOAT8
®FLUSH
FOLLOWING
; added in 8.0.2 (nonreserved)FOLLOWS
FOR
®FORCE
®FOREIGN
®FORMAT
FOUND
FROM
®FULL
FULLTEXT
®FUNCTION
®; became reserved in 8.0.1
G
GENERAL
GENERATED
®GEOMCOLLECTION
; added in 8.0.11 (nonreserved)GEOMETRY
GEOMETRYCOLLECTION
GET
®GET_FORMAT
GET_MASTER_PUBLIC_KEY
; added in 8.0.4 (reserved); became nonreserved in 8.0.11GLOBAL
GRANT
®GRANTS
GROUP
®GROUPING
®; added in 8.0.1 (reserved)GROUPS
®; added in 8.0.2 (reserved)GROUP_REPLICATION
H
HANDLER
HASH
HAVING
®HELP
HIGH_PRIORITY
®HISTOGRAM
; added in 8.0.2 (nonreserved)HISTORY
; added in 8.0.3 (nonreserved)HOST
HOSTS
HOUR
HOUR_MICROSECOND
®HOUR_MINUTE
®HOUR_SECOND
®
I
IDENTIFIED
IF
®IGNORE
®IGNORE_SERVER_IDS
IMPORT
IN
®INACTIVE
; added in 8.0.14 (nonreserved)INDEX
®INDEXES
INFILE
®INITIAL_SIZE
INNER
®INOUT
®INSENSITIVE
®INSERT
®INSERT_METHOD
INSTALL
INSTANCE
INT
®INT1
®INT2
®INT3
®INT4
®INT8
®INTEGER
®INTERVAL
®INTO
®INVISIBLE
INVOKER
IO
IO_AFTER_GTIDS
®IO_BEFORE_GTIDS
®IO_THREAD
IPC
IS
®ISOLATION
ISSUER
ITERATE
®
J
JOIN
®JSON
JSON_TABLE
®; added in 8.0.4 (reserved)JSON_VALUE
; added in 8.0.21 (nonreserved)
K
KEY
®KEYS
®KEY_BLOCK_SIZE
KILL
®
L
LAG
®; added in 8.0.2 (reserved)LANGUAGE
LAST
LAST_VALUE
®; added in 8.0.2 (reserved)LATERAL
®; added in 8.0.14 (reserved)LEAD
®; added in 8.0.2 (reserved)LEADING
®LEAVE
®LEAVES
LEFT
®LESS
LEVEL
LIKE
®LIMIT
®LINEAR
®LINES
®LINESTRING
LIST
LOAD
®LOCAL
LOCALTIME
®LOCALTIMESTAMP
®LOCK
®LOCKED
; added in 8.0.1 (nonreserved)LOCKS
LOGFILE
LOGS
LONG
®LONGBLOB
®LONGTEXT
®LOOP
®LOW_PRIORITY
®
M
MASTER
MASTER_AUTO_POSITION
MASTER_BIND
®MASTER_COMPRESSION_ALGORITHMS
; added in 8.0.18 (nonreserved)MASTER_CONNECT_RETRY
MASTER_DELAY
MASTER_HEARTBEAT_PERIOD
MASTER_HOST
MASTER_LOG_FILE
MASTER_LOG_POS
MASTER_PASSWORD
MASTER_PORT
MASTER_PUBLIC_KEY_PATH
; added in 8.0.4 (nonreserved)MASTER_RETRY_COUNT
MASTER_SERVER_ID
MASTER_SSL
MASTER_SSL_CA
MASTER_SSL_CAPATH
MASTER_SSL_CERT
MASTER_SSL_CIPHER
MASTER_SSL_CRL
MASTER_SSL_CRLPATH
MASTER_SSL_KEY
MASTER_SSL_VERIFY_SERVER_CERT
®MASTER_TLS_CIPHERSUITES
; added in 8.0.19 (nonreserved)MASTER_TLS_VERSION
MASTER_USER
MASTER_ZSTD_COMPRESSION_LEVEL
; added in 8.0.18 (nonreserved)MATCH
®MAXVALUE
®MAX_CONNECTIONS_PER_HOUR
MAX_QUERIES_PER_HOUR
MAX_ROWS
MAX_SIZE
MAX_UPDATES_PER_HOUR
MAX_USER_CONNECTIONS
MEDIUM
MEDIUMBLOB
®MEDIUMINT
®MEDIUMTEXT
®MEMBER
; added in 8.0.17 (reserved); became nonreserved in 8.0.19MEMORY
MERGE
MESSAGE_TEXT
MICROSECOND
MIDDLEINT
®MIGRATE
MINUTE
MINUTE_MICROSECOND
®MINUTE_SECOND
®MIN_ROWS
MOD
®MODE
MODIFIES
®MODIFY
MONTH
MULTILINESTRING
MULTIPOINT
MULTIPOLYGON
MUTEX
MYSQL_ERRNO
N
NAME
NAMES
NATIONAL
NATURAL
®NCHAR
NDB
NDBCLUSTER
NESTED
; added in 8.0.4 (nonreserved)NETWORK_NAMESPACE
; added in 8.0.16 (nonreserved)NEVER
NEW
NEXT
NO
NODEGROUP
NONE
NOT
®NOWAIT
; added in 8.0.1 (nonreserved)NO_WAIT
NO_WRITE_TO_BINLOG
®NTH_VALUE
®; added in 8.0.2 (reserved)NTILE
®; added in 8.0.2 (reserved)NULL
®NULLS
; added in 8.0.2 (nonreserved)NUMBER
NUMERIC
®NVARCHAR
O
OF
®; added in 8.0.1 (reserved)OFF
; added in 8.0.20 (nonreserved)OFFSET
OJ
; added in 8.0.16 (nonreserved)OLD
; added in 8.0.14 (nonreserved)ON
®ONE
ONLY
OPEN
OPTIMIZE
®OPTIMIZER_COSTS
®OPTION
®OPTIONAL
; added in 8.0.13 (nonreserved)OPTIONALLY
®OPTIONS
OR
®ORDER
®ORDINALITY
; added in 8.0.4 (nonreserved)ORGANIZATION
; added in 8.0.4 (nonreserved)OTHERS
; added in 8.0.2 (nonreserved)OUT
®OUTER
®OUTFILE
®OVER
®; added in 8.0.2 (reserved)OWNER
P
PACK_KEYS
PAGE
PARSER
PARTIAL
PARTITION
®PARTITIONING
PARTITIONS
PASSWORD
PASSWORD_LOCK_TIME
; added in 8.0.19 (nonreserved)PATH
; added in 8.0.4 (nonreserved)PERCENT_RANK
®; added in 8.0.2 (reserved)PERSIST
; became nonreserved in 8.0.16PERSIST_ONLY
; added in 8.0.2 (reserved); became nonreserved in 8.0.16PHASE
PLUGIN
PLUGINS
PLUGIN_DIR
POINT
POLYGON
PORT
PRECEDES
PRECEDING
; added in 8.0.2 (nonreserved)PRECISION
®PREPARE
PRESERVE
PREV
PRIMARY
®PRIVILEGES
PRIVILEGE_CHECKS_USER
; added in 8.0.18 (nonreserved)PROCEDURE
®PROCESS
; added in 8.0.11 (nonreserved)PROCESSLIST
PROFILE
PROFILES
PROXY
PURGE
®
Q
QUARTER
QUERY
QUICK
R
RANDOM
; added in 8.0.18 (nonreserved)RANGE
®RANK
®; added in 8.0.2 (reserved)READ
®READS
®READ_ONLY
READ_WRITE
®REAL
®REBUILD
RECOVER
RECURSIVE
®; added in 8.0.1 (reserved)REDOFILE
; removed in 8.0.3REDO_BUFFER_SIZE
REDUNDANT
REFERENCE
; added in 8.0.4 (nonreserved)REFERENCES
®REGEXP
®RELAY
RELAYLOG
RELAY_LOG_FILE
RELAY_LOG_POS
RELAY_THREAD
RELEASE
®RELOAD
REMOTE
; added in 8.0.3 (nonreserved); removed in 8.0.14REMOVE
RENAME
®REORGANIZE
REPAIR
REPEAT
®REPEATABLE
REPLACE
®REPLICA
; added in 8.0.22 (nonreserved)REPLICAS
; added in 8.0.22 (nonreserved)REPLICATE_DO_DB
REPLICATE_DO_TABLE
REPLICATE_IGNORE_DB
REPLICATE_IGNORE_TABLE
REPLICATE_REWRITE_DB
REPLICATE_WILD_DO_TABLE
REPLICATE_WILD_IGNORE_TABLE
REPLICATION
REQUIRE
®REQUIRE_ROW_FORMAT
; added in 8.0.19 (nonreserved)RESET
RESIGNAL
®RESOURCE
; added in 8.0.3 (nonreserved)RESPECT
; added in 8.0.2 (nonreserved)RESTART
; added in 8.0.4 (nonreserved)RESTORE
RESTRICT
®RESUME
RETAIN
; added in 8.0.14 (nonreserved)RETURN
®RETURNED_SQLSTATE
RETURNING
; added in 8.0.21 (nonreserved)RETURNS
REUSE
; added in 8.0.3 (nonreserved)REVERSE
REVOKE
®RIGHT
®RLIKE
®ROLE
; became nonreserved in 8.0.1ROLLBACK
ROLLUP
ROTATE
ROUTINE
ROW
®; became reserved in 8.0.2ROWS
®; became reserved in 8.0.2ROW_COUNT
ROW_FORMAT
ROW_NUMBER
®; added in 8.0.2 (reserved)RTREE
S
SAVEPOINT
SCHEDULE
SCHEMA
®SCHEMAS
®SCHEMA_NAME
SECOND
SECONDARY
; added in 8.0.16 (nonreserved)SECONDARY_ENGINE
; added in 8.0.13 (nonreserved)SECONDARY_ENGINE_ATTRIBUTE
; added in 8.0.21 (nonreserved)SECONDARY_LOAD
; added in 8.0.13 (nonreserved)SECONDARY_UNLOAD
; added in 8.0.13 (nonreserved)SECOND_MICROSECOND
®SECURITY
SELECT
®SENSITIVE
®SEPARATOR
®SERIAL
SERIALIZABLE
SERVER
SESSION
SET
®SHARE
SHOW
®SHUTDOWN
SIGNAL
®SIGNED
SIMPLE
SKIP
; added in 8.0.1 (nonreserved)SLAVE
SLOW
SMALLINT
®SNAPSHOT
SOCKET
SOME
SONAME
SOUNDS
SOURCE
SPATIAL
®SPECIFIC
®SQL
®SQLEXCEPTION
®SQLSTATE
®SQLWARNING
®SQL_AFTER_GTIDS
SQL_AFTER_MTS_GAPS
SQL_BEFORE_GTIDS
SQL_BIG_RESULT
®SQL_BUFFER_RESULT
SQL_CACHE
; removed in 8.0.3SQL_CALC_FOUND_ROWS
®SQL_NO_CACHE
SQL_SMALL_RESULT
®SQL_THREAD
SQL_TSI_DAY
SQL_TSI_HOUR
SQL_TSI_MINUTE
SQL_TSI_MONTH
SQL_TSI_QUARTER
SQL_TSI_SECOND
SQL_TSI_WEEK
SQL_TSI_YEAR
SRID
; added in 8.0.3 (nonreserved)SSL
®STACKED
START
STARTING
®STARTS
STATS_AUTO_RECALC
STATS_PERSISTENT
STATS_SAMPLE_PAGES
STATUS
STOP
STORAGE
STORED
®STRAIGHT_JOIN
®STREAM
; added in 8.0.20 (nonreserved)STRING
SUBCLASS_ORIGIN
SUBJECT
SUBPARTITION
SUBPARTITIONS
SUPER
SUSPEND
SWAPS
SWITCHES
SYSTEM
®; added in 8.0.3 (reserved)
T
TABLE
®TABLES
TABLESPACE
TABLE_CHECKSUM
TABLE_NAME
TEMPORARY
TEMPTABLE
TERMINATED
®TEXT
THAN
THEN
®THREAD_PRIORITY
; added in 8.0.3 (nonreserved)TIES
; added in 8.0.2 (nonreserved)TIME
TIMESTAMP
TIMESTAMPADD
TIMESTAMPDIFF
TINYBLOB
®TINYINT
®TINYTEXT
®TLS
; added in 8.0.21 (nonreserved)TO
®TRAILING
®TRANSACTION
TRIGGER
®TRIGGERS
TRUE
®TRUNCATE
TYPE
TYPES
U
UNBOUNDED
; added in 8.0.2 (nonreserved)UNCOMMITTED
UNDEFINED
UNDO
®UNDOFILE
UNDO_BUFFER_SIZE
UNICODE
UNINSTALL
UNION
®UNIQUE
®UNKNOWN
UNLOCK
®UNSIGNED
®UNTIL
UPDATE
®UPGRADE
USAGE
®USE
®USER
USER_RESOURCES
USE_FRM
USING
®UTC_DATE
®UTC_TIME
®UTC_TIMESTAMP
®
V
VALIDATION
VALUE
VALUES
®VARBINARY
®VARCHAR
®VARCHARACTER
®VARIABLES
VARYING
®VCPU
; added in 8.0.3 (nonreserved)VIEW
VIRTUAL
®VISIBLE
W
WAIT
WARNINGS
WEEK
WEIGHT_STRING
WHEN
®WHERE
®WHILE
®WINDOW
®; added in 8.0.2 (reserved)WITH
®WITHOUT
WORK
WRAPPER
WRITE
®
X
X509
XA
XID
XML
XOR
®
Y
YEAR
YEAR_MONTH
®
Z
ZEROFILL
®ZONE
; added in 8.0.22 (nonreserved)
MySQL 8.0新关键字和保留字
下表显示了与MySQL 5.7相比在MySQL 8.0中添加的关键字和保留字。保留的关键字标有(R)。
A
ACTIVE
ADMIN
ARRAY
ATTRIBUTE
B
BUCKETS
C
CLONE
COMPONENT
CUME_DIST
®
D
DEFINITION
DENSE_RANK
®DESCRIPTION
E
EMPTY
®ENFORCED
ENGINE_ATTRIBUTE
EXCEPT
®EXCLUDE
F
FAILED_LOGIN_ATTEMPTS
FIRST_VALUE
®FOLLOWING
G
GEOMCOLLECTION
GET_MASTER_PUBLIC_KEY
GROUPING
®GROUPS
®
H
HISTOGRAM
HISTORY
I
INACTIVE
INVISIBLE
J
JSON_TABLE
®JSON_VALUE
L
LAG
®LAST_VALUE
®LATERAL
®LEAD
®LOCKED
M
MASTER_COMPRESSION_ALGORITHMS
MASTER_PUBLIC_KEY_PATH
MASTER_TLS_CIPHERSUITES
MASTER_ZSTD_COMPRESSION_LEVEL
MEMBER
N
NESTED
NETWORK_NAMESPACE
NOWAIT
NTH_VALUE
®NTILE
®NULLS
O
OF
®OFF
OJ
OLD
OPTIONAL
ORDINALITY
ORGANIZATION
OTHERS
OVER
®
P
PASSWORD_LOCK_TIME
PATH
PERCENT_RANK
®PERSIST
PERSIST_ONLY
PRECEDING
PRIVILEGE_CHECKS_USER
PROCESS
R
RANDOM
RANK
®RECURSIVE
®REFERENCE
REPLICA
REPLICAS
REQUIRE_ROW_FORMAT
RESOURCE
RESPECT
RESTART
RETAIN
RETURNING
REUSE
ROLE
ROW_NUMBER
®
S
SECONDARY
SECONDARY_ENGINE
SECONDARY_ENGINE_ATTRIBUTE
SECONDARY_LOAD
SECONDARY_UNLOAD
SKIP
SRID
STREAM
SYSTEM
®
T
THREAD_PRIORITY
TIES
TLS
U
UNBOUNDED
V
VCPU
VISIBLE
W
WINDOW
®
Z
ZONE
MySQL 8.0删除了关键字和保留字
下表显示了与MySQL 5.7相比在MySQL 8.0中删除的关键字和保留字。保留的关键字标有(R)。
ANALYSE
DES_KEY_FILE
PARSE_GCOL_EXPR
REDOFILE
SQL_CACHE
总结,在我们在向数据库中添加字段时,避免字段名称和数据库中的关键字重名
(2)org.springframework.jdbc.BadSqlGrammarException::Error querying database. Cause: java.sql.SQLSyntaxErrorException: Unknown column ‘sale_count’ in ‘field list’
出现该异常的原因:在使用mybatis-plus时,mysql中字段的名称命名时不能使用驼峰命名,在两个单词之间使用下划线“_”进行分割。
比如上图中的错误,由于数据库中字段名称是saleCount,出现该异常,将该字段名称修改为sale_count即可。
References
[1] 朱朱朱–.(2020). mybatis-plus中遇到org.springframework.jdbc.BadSqlGrammarException. CSDN. URL https://blog.csdn.net/m0_56921622/article/details/122975904.