mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-01 02:15:07 +08:00
优化字段拓展配置
This commit is contained in:
@@ -68,6 +68,7 @@ public class MyBatisAutoConfiguration {
|
||||
@Autowired(required = false)
|
||||
private DatabaseIdProvider databaseIdProvider;
|
||||
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.hswebframework.web.dao.mybatis;
|
||||
|
||||
/**
|
||||
* 排除不需要加载的mapper.xml
|
||||
*
|
||||
* @author zhouhao
|
||||
* @since 3.0
|
||||
*/
|
||||
public interface MybatisMapperCustomer {
|
||||
String[] getExcludes();
|
||||
|
||||
String[] getIncludes();
|
||||
}
|
||||
@@ -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()];
|
||||
|
||||
@@ -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())) {
|
||||
|
||||
Reference in New Issue
Block a user