spring boot项目启动报错,错误提示如下:
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; 前言中不允许有内容。;
仔细肉眼检查了mybatis-config.xml和各个新提交的mapper映射文件,都看不出什么问题,百思不得其解。
经大师提点,怀疑是字符编码问题,于是把新提交的mapper文件复制出来用notepad打开,点击格式菜单,发现这几个文件的编码格式都是 “以UTF-8格式编码”,改成 “以UTF-8无BOM格式编码”,保存之后再复制回来,启动项目,报错消失。
原来这几个mapper文件都是由模板生成,模板生成出来的文件是UTF-8带BOM格式,java只认UTF-8无BOM格式。
大多数记事本工具(Emedit等)默认是以utf-8 BOM方式,会在文件中加入BOM头,导致这个错误:
Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1;前言中不允许有内容。
用notepad打开,格式改成 “以UTF-8无BOM格式编码”即可。