From 8ed12b03c64e44cfa6489cd65ffd4e2938146ec2 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Sat, 17 Jun 2017 13:52:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AD=97=E6=AE=B5=E6=8B=93?= =?UTF-8?q?=E5=B1=95=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/mybatis/MyBatisAutoConfiguration.java | 1 + .../dao/mybatis/MybatisMapperCustomer.java | 13 ++++++ .../web/dao/mybatis/MybatisProperties.java | 44 +++++++++++++++---- .../entity/factory/MapperEntityFactory.java | 4 +- 4 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisMapperCustomer.java diff --git a/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MyBatisAutoConfiguration.java b/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MyBatisAutoConfiguration.java index 1f88d63bc..ce7bb6b1d 100644 --- a/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MyBatisAutoConfiguration.java +++ b/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MyBatisAutoConfiguration.java @@ -68,6 +68,7 @@ public class MyBatisAutoConfiguration { @Autowired(required = false) private DatabaseIdProvider databaseIdProvider; + @Bean @Primary @ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX) diff --git a/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisMapperCustomer.java b/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisMapperCustomer.java new file mode 100644 index 000000000..c0893a68a --- /dev/null +++ b/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisMapperCustomer.java @@ -0,0 +1,13 @@ +package org.hswebframework.web.dao.mybatis; + +/** + * 排除不需要加载的mapper.xml + * + * @author zhouhao + * @since 3.0 + */ +public interface MybatisMapperCustomer { + String[] getExcludes(); + + String[] getIncludes(); +} diff --git a/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisProperties.java b/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisProperties.java index 74062be90..65e33ab29 100644 --- a/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisProperties.java +++ b/hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/MybatisProperties.java @@ -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 mybatisMappers; + + @Autowired(required = false) + public void setMybatisMappers(List 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 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()]; diff --git a/hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/factory/MapperEntityFactory.java b/hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/factory/MapperEntityFactory.java index a3bad0a82..868ce5005 100644 --- a/hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/factory/MapperEntityFactory.java +++ b/hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/factory/MapperEntityFactory.java @@ -45,7 +45,7 @@ public class MapperEntityFactory implements EntityFactory { this.realTypeMapper.putAll(realTypeMapper); } - public MapperEntityFactory addMapping(Class target, Mapper mapper) { + public MapperEntityFactory addMapping(Class target, Mapper mapper) { realTypeMapper.put(target, mapper); return this; } @@ -101,7 +101,7 @@ public class MapperEntityFactory implements EntityFactory { try { realType = (Class) 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())) {