软件编程
位置:首页>> 软件编程>> java编程>> SpringBoot找不到映射文件的处理方式

SpringBoot找不到映射文件的处理方式

作者:999感冒灵2000  发布时间:2023-10-15 07:40:11 

标签:SpringBoot,找不到,映射

SpringBoot找不到映射文件

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.qf.mapper.UserM

如果xml文件配置都确认无误还不能解决的话,可以尝试在pom.xml文件中进行如下配置:

SpringBoot找不到映射文件的处理方式

<build>
 <resources>
   <resource>
       <directory>src/main/java</directory>
       <includes>
           <include>**/*.xml</include>
       </includes>
   </resource>
 </resources>
</build>

后面我发现在yml文件里面,下面第一种写法不行,第二种又可以。。。

mapper-locations: com/tt/mapper/*.xml
mapper-locations: com.tt.mapper/*.xml

SpringBoot映射本地文件到URL路径

有两种方法,使用配置类,或者在配置文件yml中配置

1、使用配置类

需要一个配置类,实现了WebMvcConfigurer接口

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration // 1.添加配置文件注解
public class Config implements WebMvcConfigurer { // 2.实现WebMvcConfigurer接口
//    @Value("${img.path}")
    private String locationPath = "F:\\img\\"; // 3.文件本地路径
    private static final String netPath = "/img/**"; // 映射路径
    // 目前发现如果本地路径不是以分隔符结尾,在访问时否需要把在最后一个文件夹名添加在映射路径后面
    // 如:
    // locationPath-->F:\img\       访问路径-->ip:port/img/1.png
    // locationPath-->F:\img           访问路径-->ip:port/img/img/1.png
    // locationPath-->F:\img\123\     访问路径-->ip:port/img/1.png
    // locationPath-->F:\img\123      访问路径-->ip:port/img/123/1.png
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 目前在本地Win系统测试需要在本地路径前添加 "file:"
        // 有待确认Linux系统是否需要添加(已确认)
        // Linux系统也可以添加 "file:"
        registry.addResourceHandler(netPath).addResourceLocations("file:"+locationPath);
    }
}

2、在配置文件yml中配置

该方法没有使用配置类的方法中,因本地路径不是以分隔符结尾而造成的访问问题

# 文件本地路径
img:
  #  path: /root/RandomImg/images/     #Linux
  path: F:\img\      #Win
# 映射路径
spring:
  resources:     #访问系统外部资源,将该目录下的文件映射到系统下
    static-locations: classpath:/static/, file:${img.path} #本地文件,多个路径用英文逗号隔开
  mvc:
    static-path-pattern: /img/** # 访问路径

来源:https://blog.csdn.net/qq_49397049/article/details/121381993

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com