Skip to content

Commit

Permalink
完善 mybatis.basePackages 配置,指定该配置时不指定 @Mapper 注解
Browse files Browse the repository at this point in the history
  • Loading branch information
abel533 committed Aug 20, 2024
1 parent 50cfbab commit 0b50897
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String> 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<String> 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<String> 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<String> propertyNames = Stream.of(beanWrapper.getPropertyDescriptors()).map(PropertyDescriptor::getName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0b50897

Please sign in to comment.