优化字段拓展配置

This commit is contained in:
zhouhao
2017-06-17 13:52:13 +08:00
parent 913ae30dd9
commit 8ed12b03c6
4 changed files with 52 additions and 10 deletions

View File

@@ -68,6 +68,7 @@ public class MyBatisAutoConfiguration {
@Autowired(required = false)
private DatabaseIdProvider databaseIdProvider;
@Bean
@Primary
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)

View File

@@ -0,0 +1,13 @@
package org.hswebframework.web.dao.mybatis;
/**
* 排除不需要加载的mapper.xml
*
* @author zhouhao
* @since 3.0
*/
public interface MybatisMapperCustomer {
String[] getExcludes();
String[] getIncludes();
}

View File

@@ -18,6 +18,7 @@
package org.hswebframework.web.dao.mybatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
@@ -55,6 +56,13 @@ public class MybatisProperties extends org.mybatis.spring.boot.autoconfigure.Myb
*/
private String[] mapperLocationExcludes = null;
private List<MybatisMapperCustomer> mybatisMappers;
@Autowired(required = false)
public void setMybatisMappers(List<MybatisMapperCustomer> mybatisMappers) {
this.mybatisMappers = mybatisMappers;
}
public String[] getMapperLocationExcludes() {
return mapperLocationExcludes;
}
@@ -79,7 +87,16 @@ public class MybatisProperties extends org.mybatis.spring.boot.autoconfigure.Myb
locations = new HashSet<>();
else
locations = Arrays.stream(getMapperLocations()).collect(Collectors.toSet());
locations.add(defaultMapperLocation);
if (mybatisMappers != null) {
mybatisMappers.stream()
.map(MybatisMapperCustomer::getIncludes)
.flatMap(Arrays::stream)
.forEach(locations::add);
}
for (String mapperLocation : locations) {
Resource[] mappers;
try {
@@ -90,16 +107,27 @@ public class MybatisProperties extends org.mybatis.spring.boot.autoconfigure.Myb
} catch (IOException e) {
}
}
//排除不需要的配置
Set<String> excludes = new HashSet<>();
if (mybatisMappers != null) {
mybatisMappers.stream()
.map(MybatisMapperCustomer::getExcludes)
.flatMap(Arrays::stream)
.forEach(excludes::add);
}
if (mapperLocationExcludes != null && mapperLocationExcludes.length > 0) {
for (String mapperLocationExclude : mapperLocationExcludes) {
try {
Resource[] excludesMappers = new PathMatchingResourcePatternResolver().getResources(mapperLocationExclude);
for (Resource excludesMapper : excludesMappers) {
resources.remove(excludesMapper.getURL().toString());
}
} catch (IOException e) {
for (String exclude : mapperLocationExcludes) {
excludes.add(exclude);
}
}
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//排除不需要的配置
for (String mapperLocationExclude : excludes) {
try {
Resource[] excludesMappers = resourcePatternResolver.getResources(mapperLocationExclude);
for (Resource excludesMapper : excludesMappers) {
resources.remove(excludesMapper.getURL().toString());
}
} catch (IOException e) {
}
}
Resource[] mapperLocations = new Resource[resources.size()];

View File

@@ -45,7 +45,7 @@ public class MapperEntityFactory implements EntityFactory {
this.realTypeMapper.putAll(realTypeMapper);
}
public <T> MapperEntityFactory addMapping(Class<T> target, Mapper<T> mapper) {
public <T> MapperEntityFactory addMapping(Class<T> target, Mapper<? extends T> mapper) {
realTypeMapper.put(target, mapper);
return this;
}
@@ -101,7 +101,7 @@ public class MapperEntityFactory implements EntityFactory {
try {
realType = (Class<T>) Class.forName(simpleClassName);
} catch (ClassNotFoundException e) {
// throw new NotFoundException(e.getMessage());
// throw new NotFoundException(e.getMessage());
}
}
if (!Modifier.isInterface(beanClass.getModifiers()) && !Modifier.isAbstract(beanClass.getModifiers())) {