|
16 | 16 | package tk.mybatis.mapper.autoconfigure;
|
17 | 17 |
|
18 | 18 | import java.beans.PropertyDescriptor;
|
| 19 | +import java.util.Arrays; |
19 | 20 | import java.util.List;
|
20 | 21 | import java.util.Optional;
|
21 | 22 | import java.util.Set;
|
|
69 | 70 | import org.springframework.util.CollectionUtils;
|
70 | 71 | import org.springframework.util.ObjectUtils;
|
71 | 72 | import org.springframework.util.StringUtils;
|
| 73 | +import tk.mybatis.spring.annotation.BaseProperties; |
72 | 74 | import tk.mybatis.spring.mapper.MapperFactoryBean;
|
73 | 75 | import tk.mybatis.spring.mapper.MapperScannerConfigurer;
|
| 76 | +import tk.mybatis.spring.mapper.SpringBootBindUtil; |
74 | 77 |
|
75 | 78 | /**
|
76 | 79 | * {@link EnableAutoConfiguration Auto-Configuration} for Mybatis. Contributes a {@link SqlSessionFactory} and a
|
@@ -249,17 +252,27 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
|
249 | 252 | return;
|
250 | 253 | }
|
251 | 254 |
|
252 |
| - logger.debug("Searching for mappers annotated with @Mapper"); |
| 255 | + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class); |
253 | 256 |
|
254 |
| - List<String> packages = AutoConfigurationPackages.get(this.beanFactory); |
255 |
| - if (logger.isDebugEnabled()) { |
256 |
| - packages.forEach(pkg -> logger.debug("Using auto-configuration base package '{}'", pkg)); |
| 257 | + BaseProperties properties = SpringBootBindUtil.bind(environment, BaseProperties.class, BaseProperties.MYBATIS_PREFIX); |
| 258 | + if (properties != null && properties.getBasePackages() != null && properties.getBasePackages().length > 0) { |
| 259 | + List<String> basePackages = Arrays.asList(properties.getBasePackages()); |
| 260 | + if (logger.isDebugEnabled()) { |
| 261 | + basePackages.forEach(pkg -> logger.debug("Using mybatis.basePackages configuration package '{}'", pkg)); |
| 262 | + } |
| 263 | + builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(basePackages)); |
| 264 | + } else { |
| 265 | + //设置了包名的情况下,不需要指定该注解 |
| 266 | + logger.debug("Searching for mappers annotated with @Mapper"); |
| 267 | + builder.addPropertyValue("annotationClass", Mapper.class); |
| 268 | + List<String> packages = AutoConfigurationPackages.get(this.beanFactory); |
| 269 | + if (logger.isDebugEnabled()) { |
| 270 | + packages.forEach(pkg -> logger.debug("Using auto-configuration base package '{}'", pkg)); |
| 271 | + } |
| 272 | + builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(packages)); |
257 | 273 | }
|
258 | 274 |
|
259 |
| - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class); |
260 | 275 | builder.addPropertyValue("processPropertyPlaceHolders", true);
|
261 |
| - builder.addPropertyValue("annotationClass", Mapper.class); |
262 |
| - builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(packages)); |
263 | 276 | builder.addPropertyValue("mapperProperties", this.environment);
|
264 | 277 | BeanWrapper beanWrapper = new BeanWrapperImpl(MapperScannerConfigurer.class);
|
265 | 278 | Set<String> propertyNames = Stream.of(beanWrapper.getPropertyDescriptors()).map(PropertyDescriptor::getName)
|
|
0 commit comments