项目场景:
SSM集成项目,内容管理平台,mysql数据库的修改和新增
问题描述:
对数据库操作时报错
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'like,enable)
value('/upload/photo/1621691921279.png','111','1111','111111','11' at line 2
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: insert into t_teacher(photoPath,name,rank,introduction,advantage,motto,like,enable) value(?,?,?,?,?,?,?,?)
映射文件中写法
<insert id="insert">
insert into
t_teacher(photoPath,name,rank,introduction,advantage,motto,like,enable)
value(#{photoPath},#{name},#{rank},#{introduction},#{advantage},#{motto},#{like},#{enable})
</insert>
原因分析:
其实不是什么大问题,但是涉及到SSM集成,可能大家不是很能发现,如果放在navicat中新建查询,然后把语句放进去,有navicat的sql语句提示功能就能发现了。
在原本设计中,教师数据库有一个“like”字段,用来统计对教师的点赞数,但是“like"是sql语句中的关键字,所以在使用时会报错
解决方案:
在映射文件中把”like“改为由" ` "(波浪符号)包围起来的“’like‘”(后面使用了单引号,因为csdn编辑器波浪号是关键字,初次使用,没找到怎么转义,惭愧),就可以解决问题了。
总结一下,凡是编程语句,一般都有关键字的设定,使用中需要避免使用关键字作为标识符,或者必须使用的话最好使用对应的转义方法,让系统识别到这是标识符而不是关键字。
作为JAVA刚入门的新人,这是我遇到过一些比较小但挺有意思的坑,希望能给遇到相同问题的朋友带来一点思路。