mybatis-spring已經定義了mapperLocations仍然報錯BindingException: not known to the MapperRegistry

整合mybatis-spring時,已經在配置文件中寫好了如下內容:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>
	<property name="configLocation" value="classpath:mybatis-config.xml"/>
	<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>

其中mapperLocations也已經寫好了,仍然報錯:

[DEBUG][20-04-29]Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
四月 29, 2020 10:45:51 上午 org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl warn
警告: Property 'mapperLocations' was specified but matching resources are not found.

org.apache.ibatis.binding.BindingException: Type interface mapper.UserMapper is not known to the MapperRegistry.

	at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
	at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:779)
	at org.mybatis.spring.SqlSessionTemplate.getMapper(SqlSessionTemplate.java:311)
	at mapper.impl.UserMapperImpl.listUsers(UserMapperImpl.java:20)
	at mapper.UserMapperTest.listUsersTest(UserMapperTest.java:18)
	...

解決方法

可以看到是仍未找到xml配置文件,可明明已經配置了mapperLocations,並且使用了通配符*號,但是這裏使用classpath的話通配符號不會生效

應該使用 classpath*:,這樣通配符是生效的

<!-- 使用classpath*: -->
<property name="mapperLocations" value="classpath*:mapper/*.xml"/>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章