zoukankan      html  css  js  c++  java
  • Springboot 项目 无法读取resources下的mapper文件夹的.xml文件

    之前学习的时候遇到的一个问题

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/jevin98/demo/config/dao/SessionFactoryConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [classpath*:/mapper/**.xml]'; nested exception is java.io.FileNotFoundException: class path resource [classpath*:/mapper/**.xml] cannot be opened because it does not exist
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:860) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    

    主要关注此行:

    Failed to parse mapping resource: 'class path resource [classpath*:/mapper/**.xml]'; nested exception is java.io.FileNotFoundException: class path resource [classpath*:/mapper/**.xml] cannot be opened because it does not exist
    

    类路径资源[classpath*:/mapper/**]。不能打开xml,因为它不存在

    没道理啊?我的文件目录
    在这里插入图片描述

    resources下明明就有这个mapper文件夹,和这个xml文件,为什么会找不到呢

    解决思路:
    首先,确定了文件名和application,properties下的路径名没错

    其次,检查一下测试的方法
    在这里插入图片描述

    问题可能主要出现在配置mapper上,所以检查SessionFactoryConfiguration

    关于sqlSessionFactory:
    https://blog.csdn.net/u013412772/article/details/73648537
    关于sqlSeesionFactoryBean: https://blog.csdn.net/u013412772/article/details/73648537

    在这里插入图片描述

    仔细检查路径和类基本上都没错,知道看到这行:

    sqlSessionFactoryBean.setMapperLocations(resolver.getResource(packageSearchPath));
    

    sqlSessionFactoryBean调用的方法中setMapperLocations可能是导致读取不到mapper资源的原因

    关于PathMatchingResourcePatternResolver : https://www.cnblogs.com/loveLands/articles/9863195.html
    果然getResource 和 getResources不同

    getResource():
    1.从类的根路径下获取文件
    getResources():
    1.获取所有类路径下的指定文件
    可以通过classpath前缀指定,从所有的类路径下获取指定文件,与classpath前缀的区别是classpath前缀只能获取当类路径下的资源文件,而classpath前缀可以获取所有类路径下的资源文件,包括jar包中的。

  • 相关阅读:
    43. VUE 脚手架 2 版本 新建项目过程
    42 VUE 脚手架 安装 和 基本使用(创建项目)【命令】
    JDBC 原始缺点分析 和 解决
    39-8 WEBPACK —— 搭建本地服务器
    39-7 WEBPACK — js压缩的Plugin
    14. SpringBoot 更换指定的 日志框架
    39-6 打包html的plugin
    39-5 插件 — 添加版权的Plugin
    【HDU 1027】Ignatius and the Princess II
    【洛谷 1896】互不侵犯_new
  • 原文地址:https://www.cnblogs.com/jev-0987/p/12839193.html
Copyright © 2011-2022 走看看