Bläddra i källkod

启动时自动执行sql脚本

123 2 år sedan
förälder
incheckning
7335a12802

+ 18 - 2
src/main/java/com/syjy/calculate/repository/repositoryImpl/CalculationFormulaRepositoryImpl.java

@@ -8,7 +8,9 @@ import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.ResultSetExtractor;
 import org.springframework.stereotype.Service;
+
 import javax.annotation.Resource;
+import java.io.*;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.List;
@@ -32,6 +34,7 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
         });
 
         try {
+
             // 如果表存在,删除表
             if (created) {
                 String dropSql = "DROP table t_calculation_formula";
@@ -42,8 +45,17 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
 
             jdbcTemplate.execute(createSql);
 
+            File sqlFile = new File("classpath:/sql/test.sql");
+            FileReader fileReader = new FileReader(sqlFile);
+            BufferedReader reader = new BufferedReader(fileReader);
+            String s;
+            StringBuilder builder = new StringBuilder();
+            while ((s = reader.readLine()) != null) {
+                builder.append(s);
+            }
+            jdbcTemplate.execute(builder.toString());
             // 拼接插入语句,配置文件要增加&allowMultiQueries=true ,允许一次提交多条sql语句
-            String insertSql =
+     /*       String insertSql =
                     "INSERT INTO `t_calculation_formula` VALUES (1, 'DQ', 1, 'sum:math.abs(VariateA-VariateB)', 'E62', NULL, NULL, NULL, NULL, NULL);" +
                     "INSERT INTO `t_calculation_formula` VALUES (2, 'DQ', 2, 'sum:math.pow((VariateA-VariateB),2)*math.abs(VariateA-VariateB)/result1', 'E62', NULL, NULL, NULL, NULL, NULL);" +
                     "INSERT INTO `t_calculation_formula` VALUES (3, 'DQ', 3, '(1-(math.sqrt(result2)/VariateC))*100', 'E62', NULL, NULL, NULL, NULL, NULL);" +
@@ -59,10 +71,14 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
                     "INSERT INTO `t_calculation_formula` VALUES (13, 'RMSES', 1, 'sum:math.pow((VariateA-VariateB),2)', 'E62', NULL, NULL, NULL, NULL, NULL);" +
                     "INSERT INTO `t_calculation_formula` VALUES (14, 'RMSES', 2, '(1-(result1/VariateC*math.sqrt(VariateD)))*100', 'E62', NULL, NULL, NULL, NULL, NULL);";
 
-            jdbcTemplate.execute(insertSql);
+            jdbcTemplate.execute(insertSql);*/
         } catch (DataAccessException e) {
             e.printStackTrace();
             log.info("删除/创建表错误:" + e.toString());
+        } catch (FileNotFoundException e) {
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
         }
     }