SpirngBoot整合MyBatis出现“SAXParseException”和“文件提前结束”异常解决办法

    技术2022-07-10  141

    故障现象:

    程序是使用 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

    重新打包,打出来的包问题再没有重现

    Processed: 0.012, SQL: 9