mybatisspring-mybatis

org.xml.sax.SAXParseException : Content is not allowed in prolog


This is my project demo, I want to use MyBatis xml configuration. But I got a SAXParseException error, like this

Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:260)
    at org.apache.ibatis.parsing.XPathParser.<init>(XPathParser.java:126)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.<init>(XMLMapperBuilder.java:80)
    at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:519)
    ... 56 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1472)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:994)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
    at org.apache.ibatis.parsing.XPathParser.createDocument(XPathParser.java:258)
    ... 59 more

this is my mapper xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="com.ryanqy.mapper.UserMapper" xmlns="http://mybatis.org/schema/mybatis-mapper">

    <resultMap id="userResultMap" type="com.ryanqy.entity.UserEntity">
        <result column="username" property="username"/>
        <result column="password" property="password"/>
    </resultMap>

    <select id="findUserByUserName" resultMap="userResultMap">
        select * from uuc_user where username = #{userName}
    </select>

</mapper>

Solution

  • Problem is with your mybatis config:

    mybatis:
      configuration:
        map-underscore-to-camel-case: true
      type-aliases-package: com.ryanqy.entity
      mapper-locations: mapper
    

    change mapper-locations to:

      mapper-locations: mapper/UserMapper.xml
      #OR
      mapper-locations: mapper/*.xml
    

    If you change, you receive error with missing DOCTYPE in your mapper XML, so add something like this

    <!DOCTYPE mapper
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-mapper.dtd">