SpringBoot集成Mybatis时无法扫描Mapper问题

时间:2025-04-04 22:10:15

使用mybatis官方提供的starter与SpringBoot做整合

<dependency>
            <groupId></groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
</dependency>

在IDE中启动项目没有问题,但当打成JAR启动项目时出现以下异常:

15:51:43.909 [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
: No qualifying bean found for dependency [org.zbt.service.jxc.dao.base.DataDicMapper]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1463)
 ............
 org.springframework.boot.starter.dubbo.startup.BootStrap.main(BootStrap.java:15)
2016-11-13 15:51:44.389 ERROR 3349 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter 

由异常可以知道是因为Mapper没有SpringBoot扫描到。
而官方提供的可配置参数只有以几个:

config-location MyBatis xml config file (optional)
mapper-locations    Mapper xml config files (optional)
type-aliases-package    Package to search for type aliases (optional)
type-handlers-package   Package to search for type aliases (optional)
executor-type   Executor type: SIMPLE, REUSE, BATCH (optional)
configuration   A MyBatis Configuration bean

可以看出无法指定Mapper接口的位置。
可以通过在可以被SpringBoot加载到的spring文件中加入以下配置来解决这一个问题:

<bean class="" >
          <property name="basePackage" value=""/>
          <property name="annotationClass" value=""/>
</bean>