系列五、Spring整合MyBatis不忽略mapper接口同目录的xxxMapper.xml

一、概述

默认情况下maven要求我们将xml配置、properties配置等都放在resources目录下,如果我们强行将其放在java目录,即将xxxMapper.xml和xxxMapper接口放在同一个目录下,那么默认情况下maven打包时会将这个xxxMapper.xml文件忽略掉,对于这个问题我们有两种解决方案,如下:

二、解决

2.1、方案1(不忽略xml配置)

在pom.xml中添加如下配置,让maven不要忽略java目录的xml配置,如下:

XML 复制代码
<build>
	<resources>
		<resource>
			<directory>src/main/java</directory>
			<includes>
				<include>**/*.xml</include>
			</includes>
		</resource>
		<resource>
			<directory>src/main/resources</directory>
		</resource>
	</resources>
</build>

2.2、方案二(按照maven的要求来)

按照maven的要求来,将xml文件放在resources目录,但是mybatis中默认情况下要求xxxMapper.xml文件和xxxMapper接口必须放在一起,所以我们需要手动在resources资源文件夹下创建和xxxMapper接口的同级目录。

注意事项:在resources目录创建和xxxMapper同级目录时,不要一次性创建,例如UserMapper的全限定类名为org.star.mapper.UserMapper,那么在resources目录创建同级目录时,要先创建org目录,然后再org目录创建star目录,然后再star目录创建mapper目录,接着再mapper目录创建UserMapper.xml文件,这是个坑,需要注意!因为直接在resources目录一次性创建org.star.mapper目录,org.star.mapper会被当做一个目录而不是三个目录,即org.star.mapper。

相关推荐
直奔標竿5 小时前
Java开发者AI转型第二十七课!Spring AI 个人知识库实战(六)——全栈闭环收官,解锁前端流式渲染终极技巧
java·开发语言·前端·人工智能·后端·spring
空中海11 小时前
Spring Cloud 专家级面试题库
spring·spring cloud·面试
直奔標竿12 小时前
SpringAI + RAG + MCP + Agent 零基础全栈实战(完结篇)| 27课完整汇总,Java开发者AI转型必看
java·开发语言·人工智能·spring boot·后端·spring
云烟成雨TD12 小时前
Spring AI 1.x 系列【31】向量数据库:进阶使用指南
java·人工智能·spring
冷小鱼13 小时前
MyBatis 与 MyBatis-Plus:从入门到精通的完整指南
java·tomcat·mybatis
counting money14 小时前
Spring框架基础(依赖注入-全注解形式)
java·数据库·spring
counting money14 小时前
Spring框架基础(依赖注入-半注解形式)
java·后端·spring
香香甜甜的辣椒炒肉16 小时前
SpringMVC高级应用和MyBatis-Plus的概念和使用
mybatis
鸡蛋灌Bean17 小时前
mybatis分页深入了解
java·数据库·mybatis
sing~~17 小时前
SpringCloud的了解和使用
后端·spring·spring cloud