Spring+Mybatis整合讀取Jar中的mapper配置文件

在項目中通過Maven管理代碼常常遇到將部分功能作爲單獨模塊進行開發,在新建的功能模塊中將mapper文件放置到resources文件夾下;項目中依賴該模塊時遇到以下錯誤:

Error updating database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.*

通過錯誤提示可以明白時Spring在注入Mybatis的Mapper配置文件時沒有成功,導致項目中調用時出現找不到對應的執行語句。

將部分功能作爲單獨模塊開發時和項目中的代碼結構時完全一樣的,查看Spring的配置文件中讀取Mapper文件的路徑也是沒有問題的。

<property name="mapperLocations">
    <list>
        <value>classpath:/com/atcl/elwin/platform/mapper/*Mapper.xml</value>
    </list>
</property>
經過驗證發現問題出現在讀取Mapper文件的頭部classpath部分。

首先,classpath是指WEB-INF文件夾下的classes目錄。

classpath只會在classes目錄下查找指定的文件,如果需要查詢jar中的指定的配置文件需要“classpath*”。

將Spring配置文件中的讀取Mapper的地方修改成如下:

<property name="mapperLocations">
    <list>
        <value>classpath*:/com/atcl/elwin/platform/mapper/*Mapper.xml</value>
    </list>
</property>
然後重啓應用將功能單獨模塊化調用成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章