解决springboot配置文件yml中classpath找不到src/main/java下的xml文件

使用mybatis配置文件xml形式时,需要在springboot配置文件application中配置xml路径。

如果xml文件,不想放到resources下,而是直接放到 src/main/java 下与mapper放到一块,

需要额外的配置。下图目录结构:

    

 

application.yml文件:

server:
  port: 8081
spring:
  application:
    name: item-service
  datasource:
    username: root
    password: root
    #使用 MySQL连接驱动是8.0以上,需要在Url后面加上时区, GMT%2B8代表中国时区,不然报时区错误
    url: jdbc:mysql://localhost:3306/leyou?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF8
    # 注意: 新版本驱动包,要使用以下类作为驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:com/leyou/item/mapper/xml/*Mapper.xml

 yml中配置的是 mapper-locations:   classpath:com/leyou/item/mapper/xml/*Mapper.xml

此时 运行项目,会报错:

BindingException

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.leyou.item.mapper.BrandMapper.queryByCategoryId
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:227) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:49) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58) ~[mybatis-3.4.6.jar:3.4.6]
	at com.sun.proxy.$Proxy91.queryByCategoryId(Unknown Source) ~[na:na]
	at com.leyou.item.service.BrandService.queryBrandByCid(BrandService.java:77) ~[classes/:na]

 显示mapper中的方法未定义绑定,就是没成功加载xml文件。

以上配置出错原因:

yml中配置的是 mapper-locations:   classpath:com/leyou/item/mapper/xml/*Mapper.xml  

classpath 不能成功加载src/main/java下的内容,,默认可以成功加载resources下的

解决办法:

在项目的pom文件中,添加build配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.leyou.service</groupId>
    <artifactId>ly-item-service</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--自带mybatis 和 jdbc-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

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

配置后,application.yml 中 classpath:便可以加载src/main/java下xml后缀的文件

运行项目,不报错,能找到方法,正确运行。

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值