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"/>
原创文章 187 获赞 29 访问量 6万+

猜你喜欢

转载自blog.csdn.net/weixin_43826242/article/details/105834549