栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【springboot】class path resource [mybatis/mybatis-config.xml] cannot be opened because it does not ex

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【springboot】class path resource [mybatis/mybatis-config.xml] cannot be opened because it does not ex

问题描述

使用springboot 加载mybatis 配置文件,报错找不到该配置文件。

报错内容:
Caused by: java.io.FileNotFoundException: class path resource [mybatis/mybatis-config.xml] cannot be opened because it does not ex
	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:450) ~[mybatis-spring-2.0.1.jar:2.0.1]
	at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:424) ~[mybatis-spring-2.0.1.jar:2.0.1]
	at org.mybatis.spring.SqlSessionFactoryBean.getObject(SqlSessionFactoryBean.java:554) ~[mybatis-spring-2.0.1.jar:2.0.1]
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration.sqlSessionFactory(MybatisAutoConfiguration.java:150) ~[mybatis-spring-boot-autoconfigure-2.0.1.jar:2.0.1]
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$819d6861.CGLIB$sqlSessionFactory$0() ~[mybatis-spring-boot-autoconfigure-2.0.1.jar:2.0.1]
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$819d6861$$FastClassBySpringCGLIB$$f1d2d2f.invoke() ~[mybatis-spring-boot-autoconfigure-2.0.1.jar:2.0.1]
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration$$EnhancerBySpringCGLIB$$819d6861.sqlSessionFactory() ~[mybatis-spring-boot-autoconfigure-2.0.1.jar:2.0.1]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	... 55 common frames omitted
问题排查:

检查发现,文件确实存在,路径也是正确的,问题究竟出在哪呢?

springboot 的mybatis 相关配置如下:

# 配置mapper的扫描,找到所有的mapper.xml映射文件
mybatis.mapper-locations=classpath*:mybatis*Mapper.xml
# 加载全局的配置文件
mybatis.config-location=classpath:mybatis/mybatis-config.xml

通过ide启动项目后,会生成一个target文件,我们查看这个target文件发现确实没有 mybatis 这个文件夹,怪不得会报这个错。

问题原因:

在编译好的文件中,确实没有找到这个文件夹,自然会报找不到文件的错误了。

至于网上有人说需要将mybatis配置文件必须放到resources根目录下,这个解决方法应该是错误的。

解决方案:

将项目进行manven clean删除build目录下的构建输出,重新进行编译。

此时在重新查看编译后的工程。发现mybatis文件夹已生成。

问题解决!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323086.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号