一、接口文件与xml文件同包放置
操作:pom文件下加入
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
并且接口文件与xml文件命名必须一致
从生成的target文件结构和命名必须一致的推测来看,底层注册映射文件的方式应该是使用映射器接口实现类的完全限定类名
<!--
使用映射器接口实现类的完全限定类名
需要配置文件名称和接口名称一致,并且位于同一目录下
-->
<mappers>
<mapper class="org.mybatis.builder.AuthorMapper"/>
</mappers>
二、接口文件与xml文件分开放置
1 在resources中创建与src/main/Java下同层级的包
操作:此方法比起另外两种方法来说不用进行额外的配置,接口文件与xml文件命名必须一致
踩坑点:resources下创建包时建议一层一层创建,直接一次性创建com.example.dao容易报错,应该是idea原因
接口文件与xml文件命名必须一致,因为生成的target结构与同包放置时相同,所以底层原理与第一种相同
2 将xml文件放置在resources文件夹下自定义的mapper文件夹下
操作:此时xml文件与接口文件的命名可以不一致,需要在aplication.yml文件中进行配置:
mybatis:
mapper-locations: classpath:mapper/*.xml
生成的target的结构是
因为不需要同名同目录所以猜测底层mapper文件注册使用的方式是使用相对于类路径的资源引用
<!-- 使用相对于类路径的资源引用 -->
<mappers>
<mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>