1. 程式人生 > >Mybatis測試執行異常之Type interface com.testmybatis.dao.bill.BillMapper is not known to the MapperRegistry.

Mybatis測試執行異常之Type interface com.testmybatis.dao.bill.BillMapper is not known to the MapperRegistry.

問題描述:寫了一個BillMapper.xml檔案,相應的寫了一個dao層操作介面BillMapper.java介面,全部寫完ok,在測試類中測試,執行後console視窗報以下異常:



org.apache.ibatis.binding.BindingException:
Type interface com.testmybatis.dao.bill.BillMapper is not known to the
MapperRegistry.

      at
org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:42)

      at
org.apache.ibatis.session.Configuration.getMapper(Configuration.java:639)

      at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:218)

      at com.testmybatis.test.BillTest.test3(BillTest.java:75)

      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)


  異常的字面意思是com.testmybatis.dao.bill型別介面。MapperRegistry不認識BillMapper。通過字面意思覺得應該是mapper.xml檔案未被配置,遂去mybatis-config.xml下去檢視<mappers>元素中的mapper對映


<mappers>

       <mapper resource="com/testmybatis/dao/UserMapper.xml"></mapper>

       <mapper resource="com/testmybatis/dao/provider/ProviderMapper.xml"></mapper>

       <mapper resource="com/testmybatis/dao/bill/BillMapper.xml"></mapper>

    </mappers>


,發現對映路徑無誤,正納悶問題出在哪裡呢,遂靈機一動,即然對映路徑沒錯,會不會是BillMapper.xml的namespace命名空間出錯了呢?果然:

由於之前加過一層包,導致了namespace配置的和包的路徑不一致,改為正確的路徑後
 執行正常