'org.mybatis.spring.mapper.MapperScannerConfigurer' to required type 'xxx' for property 'XX'
程序员文章站
2022-07-15 13:13:04
...
2019/4/8
问题描述
在bean中通过通过org.mybatis.spring.mapper.MapperScannerConfigurer进行mapper的批量配置,代码如下
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 制定批量产生那个包中的mapper文件 -->
<property name="basePackage" value="mapper"></property>
</bean>
<bean id="studentService" class="service.Impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
结果运行测试类的时候报如下错:
'org.mybatis.spring.mapper.MapperScannerConfigurer' to required type 'mapper.StudentMapper' for property 'studentMapper'
解决思路
由于该方式是通过批量配置mapper文件,所以第一个 bean id="studentMapper"显然有问题,因为这里id代表配置多个mapper文件,然后第二bean又使用了ref=studentMapper。矛盾在于,第一个id表示批量配置mapper的一个id,第二个bean表示某一个mapper文件的ref,这就产生了冲突。
解决方法
1.把第一bean id 的值改成"mappers",表示批量配置
2.把第二个bean的ref值改成对应mapper的java文件名。(批量产生Mapper在SpringIOC中的id值默认就是接口名)
修改后代码如下
<bean id="mappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<!-- 制定批量产生那个包中的mapper文件 -->
<property name="basePackage" value="mapper"></property>
</bean>
<bean id="studentService" class="service.Impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
推荐阅读
-
SpringBoot :No property xx found for type xx!
-
org.springframework.data.mapping.PropertyReferenceException: No property xxx found for type xxx
-
使用spring-data-jpa进行count时报错No property count found for type xxx的解决方案
-
No property name found for type XXX解决
-
Spring Data: no property xxx found for type xxxx
-
JPA踩坑:No property xxx found for type xxx
-
'org.mybatis.spring.mapper.MapperScannerConfigurer' to required type 'xxx' for property 'XX'
-
Parameter 1 of constructor in com.xxx.controller.Xxx required a bean of type
-
SpringMVC自定义日期转换器不起作用,报400错误,Failed to convert value of type xxx to required type xxx
-
A component required a bean of type ‘XXX‘ that could not be found 解决办法