直接解释,这个问题的发生,首先去看你的配置文件,两个扫描包的位置:
正确如下:
1.
<!-- 自动扫描 --> <context:component-scan base-package="com.fx.*"> <!-- 扫描符合@Service @Repository @Component的类 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"> </context:include-filter></context:component-scan>
2.
<!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.fx.anniversary.core.**.dao"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </property></bean><br/>
3.注意标红的位置,正确的写法,官网的一段说明:
MapperScannerConfigurer 支 持 过 滤 由 指 定 的 创 建 接 口 或 注 解 创 建 映 射 器 。annotationClass 属性指定了要寻找的注解名称。markerInterface 属性指定了要寻找的父 接口。如果两者都被指定了,加入到接口中的映射器会匹配两种标准。 默认情况下,这两个 属性都是 null,所以在基包中给定的所有接口可以作为映射器加载。
4.
出错的配置中basePackage用的是com.fx.*,这样MapperScannerConfigurer做扫描的时候,把Service也扫进去了,就这样出错了,所有配置的dao的路径还要再细分。
