【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引入網絡資源或者本地磁盤資源

<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接口采用注解的方式,則無需映射文件

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

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

<mappers>
  <package name="org.mybatis.builder"/>
</mappers>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM