zhouhao 7 роки тому
батько
коміт
8ed12b03c6

+ 1 - 0
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)

+ 13 - 0
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();
+}

+ 36 - 8
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<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()];

+ 2 - 2
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 <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())) {