注意:我的问题是配置文件中的mapper-location只写了mapper,加上/*.xml即可。这可能只是这个错误的一种情况。
主要报错如下:
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance.Cause:org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
Caused by: org.xml.sax.SAXParseException: 前言中不允许有内容。
好久没怎么写代码了,今天创建了SpringBoot工程添加依赖,然后配置配置文件,一跑项目就报了这个错误。然后我就看控制台输出啊,呐,下面是两条WARN日志(异常的话最后主要就是上面两条)
WARN:No MyBatis mapper was found in ‘[com.lks.demo1]’ package. Please check your configuration.
WARN:Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory’ threw exception with message: Failed to parse mapping resource: ‘class path resource [mapper]’
看这个情况就是resources下面mapper中的问题了,刚开始以为是文件里面有问题,然后搜了一下还有人说是文件编码问题,试了之后还是不行,后来我打开之前的代码好好对比看了一下,映射文件是一样的应该没问题啊。再后来我发现配置文件中我只写了 mapper-locations: classpath:mapper ,hhh,太sq了吧我,笑死,脑子瓦特了,加上/*.xml后就可以了。
想起来之前和同学一起的时候,同学:“我和老师写的一模一样,怎么就报错了呢!!!”,hhh。