【Java】Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException

2021/7/6 19:08:08

本文主要是介绍【Java】Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

写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>

 



这篇关于【Java】Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程