异常背景
在写mybatis的映射文件中,使用了注释,因为我用的是idea, 快捷键写注释符号
异常信息
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/zuoyueer/dao/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{}. Check syntax #{property|(expression), var1=value1, var2=value2, ...}
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
....
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{}. Check syntax #{property|(expression), var1=value1, var2=value2, ...}
主要信息是:Parsing error was found in mapping #{}.
意思就是说,我的映射文件中#{}
里没有写值, 于是我找啊找,我的全部配置都加了值啊,最后我一个一个删除,终于定位到了异常,居然是注释的锅
异常分析
异常的代码如下:
<delete id="delete" parameterType="int">
/* #{xxx} 是没有约束的,就是传递来的一个值,使用随便命名,不能为空#{},里面随便写一个名字就行*/
DELETE FROM t_user WHERE uid = #{xxx}
</delete>
我的代码配置没有问题,问题就是中间的这行注释/* #{xxx} 是没有约束的,就是传递来的一个值,使用随便命名,不能为空#{},里面随便写一个名字就行*/
在注释中,我使用了#{}和#{xxx}. 并且,我idea快捷键默认是/* */
注释,两者恰巧在一起,导致了xml文件解析异常,把我的注释中的#{}和#{xxx}也解析了.于是报错
解决方案
解决方案有两个:
- 把注释中的#{xxx}和#{}删除,
/* */
中只有中文注释,没有特殊符号,也就不会解析错误了 - 不要在xml文件中使用
/* */
注释!尽管idea默认给我来这种注释, 一开始我还有点惊讶xml为什么默认给我这种注释,我懒得改,于是就犯错了. 也就是说xml文件中使用< !-- -->
注释, 注释内容就不会被解析了
建议使用第二种解决法案, 记住xml文件的注释统统写<!-- >这种