报错代码:
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/xing/dao/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/xing/dao/UserMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{}. Check syntax #{property|(expression), var1=value1, var2=value2, ...}
解决方案:
读报错的信息可以知道,错误出现在UserMapper.xml中,我的UserMapper.xml代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace==>绑定一个对应的dao/mapper接口-->
<mapper namespace="com.xing.dao.UserDao">
<!--select查询语句;结果中要写dao中泛型中的地址,id写方法名-->
<select id="getUserList" resultType="com.xing.pojo.User">
select * from mybatis.user
</select>
<!--parameterType是指返回值类型;id = #{id}是固定的形式,#{}里的值要和User里创建的名字一致-->
<select id="getUserById" parameterType="int" resultType="com.xing.pojo.User">
select * from mybatis.user where id = #{id}
</select>
<insert id="addUser" parameterType="com.xing.pojo.User">
/*注意:#{}中的内容与实体类中属性名一致*/
insert into mybatis.user (id ,name , pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="setUser" parameterType="com.xing.pojo.User">
update mybatis.user
set name = #{name}, pwd = #{pwd}
where id = #{id};
</update>
</mapper>
而错误信息found in mapping #{}. Check syntax #{property|(expression), var1=value1, var2=value2, …} 说明我的#{}填写有误,通过查阅资料,我发现这一错误多半原因是有某一个#{}为空,但是检查我的代码发现没有问题。最后多次调试,发现错误是在/**/注释中使用了#{}/这种格式进行了备注, 解决方法是需要将这种格式改为:<!--XXX-->
。
我发现网上关于这种错误的解决方案很少,故记录下来。另外,如果在使用debug进行排错时,发现SqlSessionFactory报空指针错误,但是自己的代码书写没有问题,可以检查添加依赖的数据库版本是否一致。