org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不容許有內容。

spring boot項目啓動報錯,錯誤提示以下:html

nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; 
nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: 
class path resource [mybatis-config.xml]; nested exception is 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: 1; columnNumber: 1; 前言中不容許有內容。;java

 

仔細肉眼檢查了mybatis-config.xml和各個新提交的mapper映射文件,都看不出什麼問題,百思不得其解。spring

經大師提點,懷疑是字符編碼問題,因而把新提交的mapper文件複製出來用notepad打開,點擊格式菜單,發現這幾個文件的編碼格式都是 「以UTF-8格式編碼」,改爲 「以UTF-8無BOM格式編碼」,保存以後再複製回來,啓動項目,報錯消失。sql

原來這幾個mapper文件都是由模板生成,模板生成出來的文件是UTF-8帶BOM格式,java只認UTF-8無BOM格式。apache

 大多數記事本工具(Emedit等)默認是以utf-8 BOM方式,會在文件中加入BOM頭,致使這個錯誤:session

 Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1;前言中不容許有內容。

mybatis

用notepad打開,格式改爲 「以UTF-8無BOM格式編碼」便可。
引用:
https://www.cnblogs.com/zhangcybb/p/6529597.htmlapp