1.springboot项目调用自定义xml中的sql接口报错:
启动项目可以发现:Property ‘mapperLocations’ was not specified or no matching resources found
调用查询接口时,直接报错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.janson.serviceedu.mapper.EduCourseMapper.getPublishCourseInfo
2.原因分析
- 由于maven 的默认加载机制,只会把 src-main-java 文件夹中的 java 类型的文件将那些加载,并保存在target 文件夹中,执行的时候,直接加载到jvm虚拟机中执行,其他类型的文件不会进行加载,比如 mapper 中的 xml 文件。因此就需要在项目中进行配置,让maven 能够自动加载 xml 文件。
3.解决方法:
(1)手动 将xml文件复制到target 文件夹中 mapper 文件夹中。(每次都要复制,比较麻烦,不推荐)
(2)通过配置文件进行配置,让 maven 能够默认加载 xml文件。需要操作步骤如下:
- 第一步: 首先在项目的pom.xml文件中进行配置,配置内容如下:(放在project 里边即可,和 dependencies 同级)
<!--用于配置maven加载src-mai-java中的xml文件 ** 代表多层目录加载,一个 * 只能加载一层目录-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
- 第二步:在项目的application.yml文件中,进行配置
mybatis-plus:
mapper-locations: classpath:com/janson/serviceedu/mapper/xml/*.xml