smm项目报错org.springframework.jdbc.BadSqlGrammarException解决方法
报错如下:
type Exception report
message Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
从报错信息可以看出是sql语法出现了问题,最后对比发现是Dao包下的文件中写sql语句处出现了问题,
应将图中的 $ 修改为#。
另:$ 和#区别
-
Mybatis 的Mapper.xml语句中parameterType向SQL语句传参有两种方式:#{}和${}
-
我们经常使用的是#{},一般解说是因为这种方式可以防止SQL注入,简单的说#{}这种方式SQL语句是经过预编译的,它是把#{}中间的参数转义成字符串,举个例子:
select * from student where student_name = #{name}
预编译后,会动态解析成一个参数标记符?:
select * from student where student_name = ?而使用${}在动态解析时候,会传入参数字符串
select * from student where student_name = ‘lyrics’ -
总的来说:
#{} 这种取值是编译好SQL语句再取值
${} 这种是取值以后再去编译SQL语句 -
#{}方式能够很大程度防止sql注入。
$方式无法防止Sql注入。
$方式一般用于传入数据库对象,例如传入表名。
一般能用#的就别用 $ 。 -
sql语句一般使用#
从作用域中取数据一般用$