在学习mybatis,曾报错:
org.apache.ibatis.exceptions.PersistenceException
Error building SqlSession.
### The error may exist in mappers/StudentMapper.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 'mappers/StudentMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{}. Check syntax #{property|(expression), var1=value1, var2=value2, ...}
在一点点查询代码语法是否出错无解后,无疑中看到一篇文章评论中写道,于是就把问题解决了
原映射文件代码为
<?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">
<mapper namespace="com.tian.dao.StudentDAO">
<insert id="insertStudent" parameterType="com.tian.pojo.Student">
-- id里面为DAO中的方法名
-- #{}中间的为Student类中的参数,因为insertStudent传参有Student类所以直接可以写Student中的属性
insert into tb_students(stu_num,stu_name,stu_gender,stu_age)
values(#{stuNum},#{stuName},#{stuGender},#{stuAge})
</insert>
<!-- <delete id="deleteStudent">-->
<!-- delete from tb_students where stu_num=#{stuNum}-->
<!-- </delete>-->
</mapper>
把注释从 -- 修改为 <!---->,就可以了
现代码为:
<?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">
<mapper namespace="com.tian.dao.StudentDAO">
<insert id="insertStudent" parameterType="com.tian.pojo.Student">
<!--
id里面为DAO中的方法名
#{}中间的为Student类中的参数,因为insertStudent传参有Student类所以直接可以写Student中的属性
-->
<!-- -->
insert into tb_students(stu_num,stu_name,stu_gender,stu_age)
values(#{stuNum},#{stuName},#{stuGender},#{stuAge})
</insert>
<!-- <delete id="deleteStudent">-->
<!-- delete from tb_students where stu_num=#{stuNum}-->
<!-- </delete>-->
</mapper>
另外我还有了个发现,在映射文件中使用 -- 或者 /**/ xml是不会解析为注释的,只有
<!-- -->是不会被解析里面的内容的