8000 和 MyBatis 官方 Starter 同步 · coderliguoqing/Mapper@eca3c7e · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eca3c7e

Browse files
committed
和 MyBatis 官方 Starter 同步
1 parent 6c34949 commit eca3c7e

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
lines changed

spring-boot-starter/mapper-spring-boot-autoconfigure/src/main/java/tk/mybatis/mapper/autoconfigure/MapperAutoConfiguration.java

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2015-2017 the original author or authors.
2+
* Copyright 2015-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,16 +28,17 @@
2828
import org.springframework.beans.BeansException;
2929
import org.springframework.beans.factory.BeanFactory;
3030
import org.springframework.beans.factory.BeanFactoryAware;
31+
import org.springframework.beans.factory.InitializingBean;
3132
import org.springframework.beans.factory.ObjectProvider;
3233
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3334
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
3435
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
3536
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
3637
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
37-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
3838
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3939
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4040
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
41+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
4142
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
4243
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4344
import org.springframework.context.EnvironmentAware;
@@ -58,7 +59,6 @@
5859
import tk.mybatis.spring.mapper.MapperFactoryBean;
5960
import tk.mybatis.spring.mapper.SpringBootBindUtil;
6061

61-
import javax.annotation.PostConstruct;
6262
import javax.sql.DataSource;
6363
import java.util.Arrays;
6464
import java.util.List;
@@ -78,12 +78,12 @@
7878
* @author Eduardo Macarrón
7979
*/
8080
@org.springframework.context.annotation.Configuration
81-
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
82-
@ConditionalOnBean(DataSource.class)
83-
@EnableConfigurationProperties({MybatisProperties.class})
81+
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
82+
@ConditionalOnSingleCandidate(DataSource.class)
83+
@EnableConfigurationProperties(MybatisProperties.class)
8484
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
8585
@AutoConfigureBefore(name = "org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration")
86-
public class MapperAutoConfiguration {
86+
public class MapperAutoConfiguration implements InitializingBean {
8787

8888
private static final Logger logger = LoggerFactory.getLogger(MapperAutoConfiguration.class);
8989

@@ -98,19 +98,23 @@ public class MapperAutoConfiguration {
9898
private final List<ConfigurationCustomizer> configurationCustomizers;
9999

100100
public MapperAutoConfiguration(MybatisProperties properties,
101-
ObjectProvider<Interceptor[]> interceptorsProvider,
102-
ResourceLoader resourceLoader,
103-
ObjectProvider<DatabaseIdProvider> databaseIdProvider,
104-
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
101+
ObjectProvider<Interceptor[]> interceptorsProvider,
102+
ResourceLoader resourceLoader,
103+
ObjectProvider<DatabaseIdProvider> databaseIdProvider,
104+
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
105105
this.properties = properties;
106106
this.interceptors = interceptorsProvider.getIfAvailable();
107107
this.resourceLoader = resourceLoader;
108108
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
109109
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
110110
}
111111

112-
@PostConstruct
113-
public void checkConfigFileExists() {
112+
@Override
113+
public void afterPropertiesSet() {
114+
checkConfigFileExists();
115+
}
116+
117+
private void checkConfigFileExists() {
114118
if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) {
115119
Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
116120
Assert.state(resource.exists(), "Cannot find config location: " + resource
@@ -127,16 +131,7 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
127131
if (StringUtils.hasText(this.properties.getConfigLocation())) {
128132
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
129133
}
130-
Configuration configuration = this.properties.getConfiguration();
131-
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
132-
configuration = new Configuration();
133-
}
134-
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
135-
for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
136-
customizer.customize(configuration);
137-
}
138-
}
139-
factory.setConfiguration(configuration);
134+
applyConfiguration(factory);
140135
if (this.properties.getConfigurationProperties() != null) {
141136
factory.setConfigurationProperties(this.properties.getConfigurationProperties());
142137
}
@@ -149,6 +144,9 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
149144
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
150145
factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
151146
}
147+
if (this.properties.getTypeAliasesSuperType() != null) {
148+
factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
149+
}
152150
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
153151
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
154152
}
@@ -159,6 +157,19 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
159157
return factory.getObject();
160158
}
161159

160+
private void applyConfiguration(SqlSessionFactoryBean factory) {
161+
Configuration configuration = this.properties.getConfiguration();
162+
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
163+
configuration = new Configuration();
164+
}
165+
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
166+
for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
167+
customizer.customize(configuration);
168+
}
169+
}
170+
factory.setConfiguration(configuration);
171+
}
172+
162173
@Bean
163174
@ConditionalOnMissingBean
164175
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
@@ -242,11 +253,11 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
242253
* on the same component-scanning path as Spring Boot itself.
243254
*/
244255
@org.springframework.context.annotation.Configuration
245-
@Import({AutoConfiguredMapperScannerRegistrar.class})
256+
@Import({ AutoConfiguredMapperScannerRegistrar.class })
246257
@ConditionalOnMissingBean(MapperFactoryBean.class)
247-
public static class MapperScannerRegistrarNotFoundConfiguration {
258+
public static class MapperScannerRegistrarNotFoundConfiguration implements InitializingBean {
248259

249-
@PostConstruct
260+
@Override
250261
public void afterPropertiesSet() {
251262
logger.debug("No {} found.", MapperFactoryBean.class.getName());
252263
}

spring-boot-starter/mapper-spring-boot-autoconfigure/src/main/java/tk/mybatis/mapper/autoconfigure/MybatisProperties.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
@ConfigurationProperties(prefix = BaseProperties.MYBATIS_PREFIX)
4040
public class MybatisProperties extends BaseProperties {
4141

42+
private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
43+
4244
/**
4345
* Location of MyBatis xml config file.
4446
*/
@@ -54,6 +56,12 @@ public class MybatisProperties extends BaseProperties {
5456
*/
5557
private String typeAliasesPackage;
5658

59+
/**
60+
* The super class for filtering type alias.
61+
* If this not specifies, the MyBatis deal as type alias all classes that searched from typeAliasesPackage.
62+
*/
63+
private Class<?> typeAliasesSuperType;
64+
5765
/**
5866
* Packages to search for type handlers. (Package delimiters are ",; \t\n")
5967
*/
@@ -129,6 +137,20 @@ public void setTypeAliasesPackage(String typeAliasesPackage) {
129137
this.typeAliasesPackage = typeAliasesPackage;
130138
}
131139

140+
/**
141+
* @since 1.3.3
142+
*/
143+
public Class<?> getTypeAliasesSuperType() {
144+
return typeAliasesSuperType;
145+
}
146+
147+
/**
148+
* @since 1.3.3
149+
*/
150+
public void setTypeAliasesSuperType(Class<?> typeAliasesSuperType) {
151+
this.typeAliasesSuperType = typeAliasesSuperType;
152+
}
153+
132154
public boolean isCheckConfigLocation() {
133155
return this.checkConfigLocation;
134156
}
@@ -168,18 +190,21 @@ public void setConfiguration(Configuration configuration) {
168190
}
169191

170192
public Resource[] resolveMapperLocations() {
171-
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
172193
List<Resource> resources = new ArrayList<Resource>();
173194
if (this.mapperLocations != null) {
174195
for (String mapperLocation : this.mapperLocations) {
175-
try {
176-
Resource[] mappers = resourceResolver.getResources(mapperLocation);
177-
resources.addAll(Arrays.asList(mappers));
178-
} catch (IOException e) {
179-
// ignore
180-
}
196+
resources.addAll(Arrays.asList(getResources(mapperLocation)));
181197
}
182198
}
183199
return resources.toArray(new Resource[resources.size()]);
184200
}
201+
202+
private Resource[] getResources(String location) {
203+
try {
204+
return resourceResolver.getResources(location);
205+
} catch (IOException e) {
206+
return new Resource[0];
207+
}
208+
}
209+
185210
}

0 commit comments

Comments (0)
0