故障现象:
程序是使用 SpringBoot 整合 MyBatis 的,在idea下运行的时候没有任何问题,但是打包成 jar 包单独运行就出问题,不论是在 Windows 环境下还是在 Linux 环境下,都是一样,可以排除系统环境问题。主要的堆栈信息提取出来是如下:
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 mapping resource: 'class path resource [mapper/]'; nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 文件提前结束
解决办法:
Spring Boot 的配置文件 application.properties 文件中的配置项 mybatis.mapper-locations 有问题,原本设置内容是:
mybatis.mapper-locations=classpath:mapper/** |
将配置修改为,这里为了保险起见,将 MyBatis 的mapper文件夹的目录也修改了一下
mybatis.mapper-locations=classpath:mappers/*.xml |
重新打包,打出来的包问题再没有重现

本文解决了一个SpringBoot整合MyBatis项目中,打包成jar后在Windows和Linux环境下运行时,无法正确加载mapper文件的问题。通过调整application.properties中的mybatis.mapper-locations配置项,将路径修改为具体的XML文件位置,成功解决了运行时的错误。

被折叠的 条评论
为什么被折叠?



