Ver Fonte

优化系统安装逻辑.自动执行classpath*:/system/install/sql/{dbType}/*-data.sql

周浩 há 8 anos atrás
pai
commit
9b6cad03d6
1 ficheiros alterados com 43 adições e 30 exclusões
  1. 43 30
      hsweb-web-core/src/main/java/org/hsweb/web/core/Install.java

+ 43 - 30
hsweb-web-core/src/main/java/org/hsweb/web/core/Install.java

@@ -13,12 +13,13 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 import org.springframework.util.Assert;
 
 import javax.annotation.PostConstruct;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.Reader;
+import java.io.*;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -52,7 +53,7 @@ public class Install {
     private Logger logger = LoggerFactory.getLogger(this.getClass());
 
     @PostConstruct
-    public void install() {
+    public void install() throws Exception {
         String dc = properties.getDriverClassName();
         String dbType = dc.contains("mysql") ? "mysql" : dc.contains("oracle") ? "oracle" : dc.contains("h2") ? "h2" : null;
         DATABASE_TYPE = dbType;
@@ -65,36 +66,48 @@ public class Install {
                 firstInstall = true;
             }
             if (firstInstall) {
-                String username = properties.getUsername();
-                Reader reader = FileUtils.getResourceAsReader("system/install/sql/" + dbType + "/install.sql");
-                BufferedReader bufferedReader = new BufferedReader(reader);
-                List<String> sqlList = new ArrayList<>();
-                SqlAppender tmp = new SqlAppender();
-                bufferedReader.lines().forEach((line) -> {
-                    if (line.startsWith("--")) return;
-                    line = line.replace("${jdbc.username}", username);
-                    //去除sql中的;
-                    if (line.endsWith(";"))
-                        tmp.add(line.substring(0, line.length() - 1));
-                    else
-                        tmp.add(line);
-                    tmp.add("\n");
-                    if (line.endsWith(";")) {
-                        sqlList.add(tmp.toString());
-                        tmp.clear();
+                //表结构
+                InputStream reader = FileUtils.getResourceAsStream("system/install/sql/" + dbType + "/install.sql");
+                execInstallSql(reader);
+                String installSqlName = "classpath*:/system/install/sql/" + dbType + "/*-data.sql";
+                Resource[] resources = new PathMatchingResourcePatternResolver().getResources(installSqlName);
+                for (Resource resource : resources) {
+                    if (resource.isReadable()) {
+                        execInstallSql(resource.getInputStream());
                     }
-                });
-                sqlList.forEach((sql) -> {
-                    try {
-                        sqlExecutor.exec(new SimpleSQL(sql));
-                    } catch (Exception e) {
-                        logger.warn("install sql fail", e);
-                    }
-                });
+                }
             }
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw e;
         }
     }
 
+    protected void execInstallSql(InputStream sqlStream) {
+        String username = properties.getUsername();
+        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(sqlStream));
+        List<String> sqlList = new ArrayList<>();
+        SqlAppender tmp = new SqlAppender();
+        bufferedReader.lines().forEach((line) -> {
+            if (line.startsWith("--")) return;
+            line = line.replace("${jdbc.username}", username);
+            //去除sql中的;
+            if (line.endsWith(";"))
+                tmp.add(line.substring(0, line.length() - 1));
+            else
+                tmp.add(line);
+            tmp.add("\n");
+            if (line.endsWith(";")) {
+                sqlList.add(tmp.toString());
+                tmp.clear();
+            }
+        });
+        sqlList.forEach((sql) -> {
+            try {
+                sqlExecutor.exec(new SimpleSQL(sql));
+            } catch (Exception e) {
+                logger.warn("install sql fail", e);
+            }
+        });
+    }
+
 }