加载自定义包下的 Mapper.xml 文件的一些问题

两点很重要!

  • 1.需要在 pom.xml 文件中进行配置
  • 2.需要正确配置路径

一、如果有问题,可能是 pom.xml 中没有配置

有可能提示找不到对应的 id ,并不是找不到 id,
根本没找到这个文件,需要 加上 pom.xml 中配置:

(注意位置)

<resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
加载自定义包下的 Mapper.xml 文件的一些问题-LMLPHP

二、mapper-locations 对应的 value 的配置

情况描述:

在这个路径上,也被折磨了很久,java 目录也是我们自己创建的,但是配置路径只需要从 java 下面的目录开始配置,前面使用 classpath*: 代替

配置方式一:

正确配置路径:

比如:
我的 Mapper.xml 文件在 com.xpwi.mapper 包下,

这样配置:

<property name="mapperLocations" value="classpath*:com/xpwi/mapper/*Mapper.xml"></property>

截图:

加载自定义包下的 Mapper.xml 文件的一些问题-LMLPHP

配置方式二:

使用变量的形式在 properties 文件中定义(需要加载那个 properties 文件):

(1)config.properties 文件:

mapper-locations = classpath*:com/xpwi/mapper/*.xml

(2)路径配置:

<property name="mapperLocations" value="${mapper-locations}"></property>

多个 xml 文件的配置

<property name="mapperLocations" >
	<list>
		<value>classpath*:com/sqlmap/*.xml</value>
		<value>classpath*:com/mapper/*.xml</value>
	</list>
</property>

更多爬坑记录

01-28 02:46