spring boot 集成 mybatis , 不同package下同名mapper启动报错

抹茶奶爸0223 发布于 2018/03/06 09:00
阅读 2K+
收藏 0
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'sysButtonMapper' for bean class [com.sienline.wmsserver.dao.SysButtonMapper] conflicts with existing, non-compatible bean definition of same name and class [com.sienline.wmsserver.dao.dbo.SysButtonMapper]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:345) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.mybatis.spring.mapper.ClassPathMapperScanner.checkCandidate(ClassPathMapperScanner.java:236) ~[mybatis-spring-1.3.1.jar:1.3.1]
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:283) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.mybatis.spring.mapper.ClassPathMapperScanner.doScan(ClassPathMapperScanner.java:163) ~[mybatis-spring-1.3.1.jar:1.3.1]
	at org.mybatis.spring.annotation.MapperScannerRegistrar.registerBeanDefinitions(MapperScannerRegistrar.java:104) ~[mybatis-spring-1.3.1.jar:1.3.1]
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:359) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:143) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:320) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at com.sienline.wmsserver.WmsServerApplication.main(WmsServerApplication.java:19) [classes/:na]

2018-03-06 08:56:07.851  INFO 8816 --- [           main] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43aaf813: startup date [Tue Mar 06 08:56:05 CST 2018]; root of context hierarchy

通过myabatis generator 生成的对应mapperxml、dao、model,因为还要自定义,有的文件就重名了,请问除了改文件名,还有什么方法可以解决,求帮助~

加载中
0
beyondforever68
beyondforever68

应该可以分别指定扫描的package吧

抹茶奶爸0223
抹茶奶爸0223
好的,我先试试
0
萧瑟秋天
萧瑟秋天

首先这个跟spring boot没有关系,跟mybatis注册mapper的BeanName有关,你自定义beanNameGenerator就行了

0
d
dev_only

通过查看Spring源码得知,当我们使用注解创建bean时,spring使用了AnnotationBeanNameGenerator来创建bean的名称。
解决方案:自己写一个Generator吧。如下:

@SpringBootApplication
@ComponentScan(nameGenerator = MultidataApplication.SpringBeanNameGenerator.class)
public class MultidataApplication {

    public static class SpringBeanNameGenerator extends AnnotationBeanNameGenerator {
        @Override
        protected String buildDefaultBeanName(BeanDefinition definition) {
            return definition.getBeanClassName();
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(MultidataApplication.class, args);
    }

}

Mybatis的MapperScan里新增:

nameGenerator = MultidataApplication.SpringBeanNameGenerator.class

示例:

@MapperScan(basePackages="com.onlyas.appserver.dao", nameGenerator = MultidataApplication.SpringBeanNameGenerator.class)

 

返回顶部
顶部