mapper標籤引入映射器四種方式

今天使用了第四種方式,報錯(後來發現持久層接口名稱和xml文件名稱不一樣,所以導致錯誤,特此轉載

原文章地址:https://www.cnblogs.com/canger/p/9911958.html

 

 

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

<!-- Using classpath relative resources -->
<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引入網絡資源或者本地磁盤資源

<!-- Using url fully qualified paths -->
<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接口採用註解的方式,則無需映射文件
  • windows系統下,映射文件不區分大小寫,linux系統沒有驗證
<!-- Using mapper interface classes -->
<mappers>
  <mapper class="org.mybatis.builder.AuthorMapper"/>
  <mapper class="org.mybatis.builder.BlogMapper"/>
  <mapper class="org.mybatis.builder.PostMapper"/>
</mappers>

 

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

  • 如mapper接口採用註解的方式,則無需映射文件
  • windows系統下,映射文件不區分大小寫,linux系統沒有驗證
<!-- Register all interfaces in a package as mappers -->
<mappers>
  <package name="org.mybatis.builder"/>
</mappers>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章