【Mybatis】Mybatis配置文件中mappers標籤引入映射器的四種方式

第一種方式:mapper標籤,經過resource屬性引入classpath路徑的相對資源網絡

<mappers>
  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
  <mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>

第二種方式:mapper標籤,經過url引入網絡資源或者本地磁盤資源mybatis

<mappers>
  <mapper url="file:///var/mappers/AuthorMapper.xml"/>
  <mapper url="file:///var/mappers/BlogMapper.xml"/>
  <mapper url="file:///var/mappers/PostMapper.xml"/>
</mappers>

第三種方式:mapper標籤,經過class屬性指定mapper接口名稱,此時對應的映射文件必須與接口位於同一路徑下,而且名稱相同
如mapper接口採用註解的方式,則無需映射文件app

<mappers>
  <mapper class="org.mybatis.builder.AuthorMapper"/>
  <mapper class="org.mybatis.builder.BlogMapper"/>
  <mapper class="org.mybatis.builder.PostMapper"/>
</mappers>

第四種方式:package標籤,經過name屬性指定mapper接口所在的包名 ,此時對應的映射文件必須與接口位於同一路徑下,而且名稱相同
如mapper接口採用註解的方式,則無需映射文件ide

<mappers>
  <package name="org.mybatis.builder"/>
</mappers>
相關文章
相關標籤/搜索