Mybatis 如何扫描多个包下的mapper,
今天做毕业设计的时候,添加了一个mapper到项目中,然后发现报了一个错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name defaultParamController: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.zyj.web.service.defaultParam.controller.DefaultParamController.defaultParamService; nested exception is Error creating bean with name defaultParamServiceImpl: Injection of autowired dependencies failed; nested exception is public com.zyj.web.service.defaultParam.mapper.DefaultParamExtendsMapper com.zyj.web.service.defaultParam.service.impl.DefaultParamServiceImpl.defaultParamExtendsMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
追根究底就是mapper没有被扫描到,而原先我以为的是@autowired注解的bean没有配置进IOC容器中,结果居然是mapper没有被扫描到,接下来解决这个问题的方法就是:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <property name="basePackage" value="com.zyj.authorization.mapper,com.zyj.web.service.defaultParam.mapper"></property> </bean>
在basePackage中使用逗号或者分号作为分隔符去给映射器接口文件设置多个包的路径,让其能够被递归扫描到。