Преглед изворни кода

获取sql文件流并解析后执行

zhangchenglong пре 2 година
родитељ
комит
ad43c6fa19

+ 1 - 1
src/main/java/com/syjy/calculate/repository/CalculationFormulaRepository.java

@@ -15,7 +15,7 @@ public interface CalculationFormulaRepository {
     void createTable();
 
     /**
-     * 查询数据
+     * 查询公式数据
      *
      * @param type     类型
      * @return 返回记录

+ 23 - 11
src/main/java/com/syjy/calculate/repository/repositoryImpl/CalculationFormulaRepositoryImpl.java

@@ -15,6 +15,7 @@ import java.io.*;
 import java.net.URL;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.List;
 
 @Service
@@ -24,6 +25,9 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
     @Resource
     public JdbcTemplate jdbcTemplate;
 
+    /**
+     * 创建公式表
+     */
     @Override
     public void createTable() {
         // 判断表是否存在
@@ -36,24 +40,24 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
         });
 
         try {
-
             // 获取初始化公式表sql
             String path = "sql/test.sql";
+            // 用ClassLoader获取jar包中的文件,获取文件流
             ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader();
             assert defaultClassLoader != null;
             URL resource = defaultClassLoader.getResource(path);
             assert resource != null;
             InputStream inputStream = resource.openStream();
-            File sqlFile = new File("sql/test.sql");
-            FileUtils.copyToFile(inputStream,sqlFile);
-            // 读取文件
-            FileReader fileReader = new FileReader(sqlFile);
-            BufferedReader reader = new BufferedReader(fileReader);
-            String s;
+            // 循环将流放到StringBuilder中
             StringBuilder builder = new StringBuilder();
-            while ((s = reader.readLine()) != null) {
-                builder.append(s);
+            int len;
+            byte[] buf = new byte[1024];
+            while ((len = inputStream.read(buf)) != -1) {
+                builder.append(new String(buf, 0, len));
             }
+            // 关闭流
+            inputStream.close();
+            // 执行sql文件
             jdbcTemplate.execute(builder.toString());
         } catch (DataAccessException e) {
             e.printStackTrace();
@@ -65,12 +69,20 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
         }
     }
 
+    /**
+     * 查询公式数据
+     * @param type     类型
+     * @param provinceEnum
+     * @return
+     */
     @Override
     public List<CalculationFormula> findByTypeAndProvince(String type,String provinceEnum) {
+        // 模糊查询参数
         provinceEnum = "%"+provinceEnum+";%";
+        // 查询sql
         String sql = "SELECT * from t_calculation_formula where TYPE = ? and PROVINCE_ENUM like ?  ";
-
-        List<CalculationFormula> calculationFormulaList = null;
+        // 根据类型和省调查询公式
+        List<CalculationFormula> calculationFormulaList = new ArrayList<>();
         try {
             calculationFormulaList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CalculationFormula.class), type ,provinceEnum);
         } catch (DataAccessException e) {

+ 23 - 1
src/main/resources/sql/test.sql

@@ -1,6 +1,25 @@
+/*
+ Navicat Premium Data Transfer
+
+ Source Server         : local
+ Source Server Type    : MySQL
+ Source Server Version : 80029
+ Source Host           : localhost:3306
+ Source Schema         : ipfcst-v3
+
+ Target Server Type    : MySQL
+ Target Server Version : 80029
+ File Encoding         : 65001
+
+ Date: 17/08/2022 16:04:06
+*/
+
 SET NAMES utf8mb4;
 SET FOREIGN_KEY_CHECKS = 0;
 
+-- ----------------------------
+-- Table structure for t_calculation_formula
+-- ----------------------------
 DROP TABLE IF EXISTS `t_calculation_formula`;
 CREATE TABLE `t_calculation_formula`  (
   `ID` int(0) NOT NULL AUTO_INCREMENT,
@@ -16,6 +35,9 @@ CREATE TABLE `t_calculation_formula`  (
   PRIMARY KEY (`ID`) USING BTREE
 ) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
 
+-- ----------------------------
+-- Records of t_calculation_formula
+-- ----------------------------
 INSERT INTO `t_calculation_formula` VALUES (1, 'POINT_S', 1, '(1-math.abs((sj-yc)/yc))*100', 'E21Old;E61;E62;E63;E64;', NULL, NULL, NULL, NULL, NULL);
 INSERT INTO `t_calculation_formula` VALUES (2, 'POINT_U', 1, 'sum:math.abs(sj-yc)', 'E61;E62;E63;E64;', NULL, NULL, NULL, NULL, NULL);
 INSERT INTO `t_calculation_formula` VALUES (3, 'POINT_U', 2, 'math.abs((sj/(sj+yc))-0.5)*(math.abs(sj-yc)/result1)', 'E61;E62;E63;E64;', NULL, NULL, NULL, NULL, NULL);
@@ -34,4 +56,4 @@ INSERT INTO `t_calculation_formula` VALUES (15, 'RMSE_R', 2, '(1-math.sqrt((resu
 INSERT INTO `t_calculation_formula` VALUES (16, 'RMSE_S', 1, 'sum:math.pow((sj-yc),2)', 'E22;E23;E36;E37;E42;E43;E44;E45;E51;E62;', NULL, NULL, NULL, NULL, NULL);
 INSERT INTO `t_calculation_formula` VALUES (17, 'RMSE_S', 2, '(1-(result1/rl*math.sqrt(count)))*100', 'E22;E23;E36;E37;E42;E43;E44;E45;E51;E62;', NULL, NULL, NULL, NULL, NULL);
 
-SET FOREIGN_KEY_CHECKS = 1;
+SET FOREIGN_KEY_CHECKS = 1;