org.xml.sax.SAXParseException : 序言中不允许包含内容

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

这是我的项目demo,我想使用MyBatisxml配置。但是我得到了一个 SAXParseException 错误,就像这样

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

这是我的映射器 xml 文件:

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

问题出在您的 mybatis 配置上:

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

mapper-locations 更改为:

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

如果您进行更改,您会收到映射器中缺少 DOCTYPE 的错误 XML,因此添加类似这样的内容

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