No MyBatis mapper was found in '[com.study.dal.***.mapper]' package

使用 MapperScannerConfigurer 自动扫描mapper下的文件时,一直出现下面警告: No MyBatis mapper was found in ‘[com.study.boot.dal.*.mapper]’ package.
并且也没有注册mapper的文件

配置如下

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFaction" />
        <property name="annotationClass" value="javax.annotation.Resource" />
        <property name="basePackage" value="com.study.boot.dal.***.mapper" />
    </bean>

原因是: 由于增加了 annotationClass的配置, 导致只会扫描被 Resource 注解的文件,因此只需要把这段配置注释掉就ok

修改之后的配置如下

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFaction" />
        <property name="basePackage" value="com.study.boot.dal.***.mapper" />
    </bean>

使用annotationClass的源码如下:

protected void registerDefaultFilters() {
      boolean acceptAllInterfaces = true;
      // if specified, use the given annotation and / or marker interface
      if (MapperScannerConfigurer.this.annotationClass != null) {
        addIncludeFilter(new AnnotationTypeFilter(MapperScannerConfigurer.this.annotationClass));
        acceptAllInterfaces = false;
      }

      // override AssignableTypeFilter to ignore matches on the actual marker interface
      if (MapperScannerConfigurer.this.markerInterface != null) {
        addIncludeFilter(new AssignableTypeFilter(MapperScannerConfigurer.this.markerInterface) {
          @Override
          protected boolean matchClassName(String className) {
            return false;
          }
        });
        acceptAllInterfaces = false;
      }

      if (acceptAllInterfaces) {
        // default include filter that accepts all classes
        addIncludeFilter(new TypeFilter() {
          public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
            return true;
          }
        });
      }
    }
发布了190 篇原创文章 · 获赞 19 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/zengchenacmer/article/details/78336512