diff --git a/spring-boot-starter/mapper-spring-boot-autoconfigure/src/main/java/tk/mybatis/mapper/autoconfigure/MapperAutoConfiguration.java b/spring-boot-starter/mapper-spring-boot-autoconfigure/src/main/java/tk/mybatis/mapper/autoconfigure/MapperAutoConfiguration.java index b6c587bda..f1e504b26 100644 --- a/spring-boot-starter/mapper-spring-boot-autoconfigure/src/main/java/tk/mybatis/mapper/autoconfigure/MapperAutoConfiguration.java +++ b/spring-boot-starter/mapper-spring-boot-autoconfigure/src/main/java/tk/mybatis/mapper/autoconfigure/MapperAutoConfiguration.java @@ -16,6 +16,7 @@ package tk.mybatis.mapper.autoconfigure; import java.beans.PropertyDescriptor; +import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.Set; @@ -69,8 +70,10 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; +import tk.mybatis.spring.annotation.BaseProperties; import tk.mybatis.spring.mapper.MapperFactoryBean; import tk.mybatis.spring.mapper.MapperScannerConfigurer; +import tk.mybatis.spring.mapper.SpringBootBindUtil; /** * {@link EnableAutoConfiguration Auto-Configuration} for Mybatis. Contributes a {@link SqlSessionFactory} and a @@ -249,17 +252,27 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B return; } - logger.debug("Searching for mappers annotated with @Mapper"); + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class); - List packages = AutoConfigurationPackages.get(this.beanFactory); - if (logger.isDebugEnabled()) { - packages.forEach(pkg -> logger.debug("Using auto-configuration base package '{}'", pkg)); + BaseProperties properties = SpringBootBindUtil.bind(environment, BaseProperties.class, BaseProperties.MYBATIS_PREFIX); + if (properties != null && properties.getBasePackages() != null && properties.getBasePackages().length > 0) { + List basePackages = Arrays.asList(properties.getBasePackages()); + if (logger.isDebugEnabled()) { + basePackages.forEach(pkg -> logger.debug("Using mybatis.basePackages configuration package '{}'", pkg)); + } + builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(basePackages)); + } else { + //设置了包名的情况下,不需要指定该注解 + logger.debug("Searching for mappers annotated with @Mapper"); + builder.addPropertyValue("annotationClass", Mapper.class); + List packages = AutoConfigurationPackages.get(this.beanFactory); + if (logger.isDebugEnabled()) { + packages.forEach(pkg -> logger.debug("Using auto-configuration base package '{}'", pkg)); + } + builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(packages)); } - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class); builder.addPropertyValue("processPropertyPlaceHolders", true); - builder.addPropertyValue("annotationClass", Mapper.class); - builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(packages)); builder.addPropertyValue("mapperProperties", this.environment); BeanWrapper beanWrapper = new BeanWrapperImpl(MapperScannerConfigurer.class); Set propertyNames = Stream.of(beanWrapper.getPropertyDescriptors()).map(PropertyDescriptor::getName) diff --git a/spring-boot-starter/mapper-spring-boot-samples/mapper-spring-boot-sample-xml/src/main/resources/application.yml b/spring-boot-starter/mapper-spring-boot-samples/mapper-spring-boot-sample-xml/src/main/resources/application.yml index aac52a11e..7b689c046 100644 --- a/spring-boot-starter/mapper-spring-boot-samples/mapper-spring-boot-sample-xml/src/main/resources/application.yml +++ b/spring-boot-starter/mapper-spring-boot-samples/mapper-spring-boot-sample-xml/src/main/resources/application.yml @@ -28,12 +28,10 @@ spring: mybatis: config-location: mybatis-config.xml base-packages: tk.mybatis.sample.mapper - mapper-locations: - - classpath: /tk/mybatis/sample/mapper logging: level: - root: WARN + root: DEBUG tk.mybatis.sample.mapper: TRACE mapper: not-empty: true