Springboot+MyBatis,启动报‘Skipping MapperFactoryBean with name 'xxx' and 'xxx'mapperInterface. ’警告

问题描述

在SpringBoot + MyBatis项目开发时,使用了Mapper xml和Mapper Interface的方式开发,启动时控制台输出如下警告:

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-04-18 16:40:55.698  INFO 8668 --- [  restartedMain] c.x.z.e.web.api.ApiWebApplication        : Starting ApiWebApplication on SKY-20181015HGX with PID 8668 (D:\xbb\second_edition\ecommerce\ecommerce-web\ecommerce-web-api\target\classes started by Administrator in D:\xbb\second_edition\ecommerce)
2019-04-18 16:40:55.702 DEBUG 8668 --- [  restartedMain] c.x.z.e.web.api.ApiWebApplication        : Running with Spring Boot v2.1.4.RELEASE, Spring v5.1.6.RELEASE
2019-04-18 16:40:55.702  INFO 8668 --- [  restartedMain] c.x.z.e.web.api.ApiWebApplication        : The following profiles are active: dev
2019-04-18 16:40:55.752  INFO 8668 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-04-18 16:40:55.752  INFO 8668 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-04-18 16:40:56.888  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'commodityClassificationMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.CommodityClassificationMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.888  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'commodityImageMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.CommodityImageMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'commodityMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.CommodityMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'couponMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.CouponMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'shopImageMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.ShopImageMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'shopMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.ShopMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'shopMembershipLevelMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.ShopMembershipLevelMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'shopMembershipMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.ShopMembershipMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'userAccessShopLogMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.UserAccessShopLogMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'userCollectShopMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.UserCollectShopMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'userCouponMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.UserCouponMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'userMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.UserMapper' mapperInterface. Bean already defined with the same name!
2019-04-18 16:40:56.889  WARN 8668 --- [  restartedMain] t.m.s.mapper.ClassPathMapperScanner      : Skipping MapperFactoryBean with name 'userMembershipMapper' and 'club.xyes.zkh.ecommerce.dao.mapper.UserMembershipMapper' mapperInterface. Bean already defined with the same name!

项目启动类如下

@MapperScan(ApplicationConstants.Context.MAPPER_PACKAGE)
@SpringBootApplication(scanBasePackages = ApplicationConstants.Context.COMPONENT_PACKAGE)
public class ApiWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiWebApplication.class, args);
    }
}

Mapper接口如下:
这里使用了@Repository注解,不影响正常代码,可忽略

@Repository
public interface ShopMembershipMapper extends BaseMapper<ShopMembership> {
}

mapper xml如下



<mapper namespace="club.xyes.zkh.ecommerce.dao.mapper.UserMembershipMapper">
    <sql id="base_select">sql>
    <resultMap id="um_result" type="club.xyes.zkh.ecommerce.commons.entity.UserMembership">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="membership_level_id" property="membershipLevelId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    resultMap>
mapper>

问题解决

去掉配置类(或启动类)的 @MapperScan(xxx) 注解后警告消失。

原因猜测

实现声明:由于项目进度原因,本人没有详细去研究具体的警告发生原因,这里仅就问题的表现做一些猜测:
猜测是由于@MapperScan注解的存在是的Spring在启动时首先根据注解配置的包名去扫描了一次Mapper接口,并将其注册到Spring容器中,而且由于在application.yml中配置了mapper xml的地址,mybatis读取xml配置指定的mapper接口后又尝试将mapper注册到容器中,但此时容器中已存在相同的bean,故警告发生;(上述表达中,扫描顺序可能不一定正确,也可能是mybatis先根据xml配置扫描, 再根据@MapperScan扫描);

解决方案的不足

基于如上猜测,这个解决方法可能存在以下不足
若项目的Mapper接口不存在对应的mapper xml文件,可能会发生Mapper找不到的情况(未测试,遇到的兄弟可以验证一下)

你可能感兴趣的:(SpringBoot,MyBatis,警告,java)