写MyBatis时问题报错:
org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/fzhiy/dao/StudentMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 10; 已经看到 doctype。
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
at com.fzhiy.util.SqlSessionFactoryUtil.getSqlSessionFactory(SqlSessionFactoryUtil.java:29)
at com.fzhiy.util.SqlSessionFactoryUtil.openSession(SqlSessionFactoryUtil.java:19)
at com.fzhiy.test.StudentTest.main(StudentTest.java:12)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 10; 已经看到 doctype。
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 4 more
原因:
MyBatis连接数据库的配置文件StudentMapper.xml和mybatis-config.xml复制时 dtd 未修改发生错误。
修改前配置文件如下:
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<mapper namespace="com.fzhiy.dao.StudentDao">
<!-- parameterTpye对应typeAlias别名 -->
<insert id="insertStudent" parameterType="Student">
insert into t_student values(null,#{name},#{age})
</insert>
</mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 引入外部资源文件 -->
<properties resource="jdbc.properties"/>
<typeAliases> <!-- 别名 -->
<typeAlias alias="Student" type="com.fzhiy.entity.Student"/>
</typeAliases>
<!-- 设置驼峰匹配 -->
<!-- <settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings> -->
<!-- 配置环境:可以配置多个环境,default:配置某一个环境的唯一标识,表示默认使用哪个环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 配置连接信息 -->
<property name="driver" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 配置映射文件 -->
<mappers>
<mapper resource="com/fzhiy/dao/StudentMapper.xml"/>
</mappers>
</configuration>
修改后:
<?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.fzhiy.dao.StudentDao">
<!-- parameterTpye对应typeAlias别名 -->
<insert id="insertStudent" parameterType="Student">
insert into t_student values(null,#{name},#{age})
</insert>
</mapper>