Jelajahi Sumber

根据dropTableFlag,判断公式表示通过页面修改,还是通过sql文件修改:
新增获取配置文件dropTableFlag,如果为true则每次重启都删除表并根据sql文件重新建表,如果为false,则每次重启不对表做任何删改

zhangchenglong 2 tahun lalu
induk
melakukan
04dd95a4d1

+ 4 - 2
src/main/java/com/syjy/calculate/config/StarterAutoConfigure.java

@@ -8,9 +8,11 @@ import com.syjy.calculate.service.AccuracyPassRateCalculateService;
 import com.syjy.calculate.util.CreateAndInsertSqlUtil;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
+@EnableConfigurationProperties(StarterProperties.class)
 @Configuration
 public class StarterAutoConfigure {
 
@@ -45,14 +47,14 @@ public class StarterAutoConfigure {
     @Bean
     @ConditionalOnMissingBean
     @ConditionalOnProperty(prefix = "calculate.service", value = "enabled", havingValue = "true")
-    Test test (){
+    Test test() {
         return new Test();
     }
 
     @Bean
     @ConditionalOnMissingBean
     @ConditionalOnProperty(prefix = "calculate.service", value = "enabled", havingValue = "true")
-    JarCalculate jarCalculate (){
+    JarCalculate jarCalculate() {
         return new JarCalculate();
     }
 

+ 17 - 0
src/main/java/com/syjy/calculate/config/StarterProperties.java

@@ -0,0 +1,17 @@
+package com.syjy.calculate.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+@ConfigurationProperties("calculate.service")
+public class StarterProperties {
+
+    private String dropTableFlag;
+
+    public String getDropTableFlag() {
+        return dropTableFlag;
+    }
+
+    public void setDropTableFlag(String dropTableFlag) {
+        this.dropTableFlag = dropTableFlag;
+    }
+}

+ 11 - 0
src/main/java/com/syjy/calculate/conotroller/Test.java

@@ -2,10 +2,13 @@ package com.syjy.calculate.conotroller;
 
 import com.alibaba.fastjson.JSONObject;
 import com.syjy.calculate.service.AccuracyPassRateCalculateService;
+import com.syjy.calculate.util.CreateAndInsertSqlUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
 
+import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.util.*;
 
@@ -14,6 +17,8 @@ import java.util.*;
 public class Test {
     @Autowired
     private AccuracyPassRateCalculateService accuracyPassRateCalculateService;
+    @Resource
+    private CreateAndInsertSqlUtil createAndInsertSqlUtil;
 
     @RequestMapping(value = "/test")
     public void saveElectricField() {
@@ -51,4 +56,10 @@ public class Test {
         }
     }
 
+    @RequestMapping("testExportSql")
+    @ResponseBody
+    public void testExportSql() {
+        createAndInsertSqlUtil.start("test-db-dev", "ipfcst-v3");
+    }
+
 }

+ 30 - 4
src/main/java/com/syjy/calculate/repository/repositoryImpl/CalculationFormulaRepositoryImpl.java

@@ -1,16 +1,22 @@
 package com.syjy.calculate.repository.repositoryImpl;
 
+import com.syjy.calculate.config.StarterProperties;
 import com.syjy.calculate.entity.CalculationFormula;
 import com.syjy.calculate.repository.CalculationFormulaRepository;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DataAccessException;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.ResultSetExtractor;
 import org.springframework.stereotype.Service;
 import org.springframework.util.ClassUtils;
+
 import javax.annotation.Resource;
 import java.io.*;
 import java.net.URL;
+import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -18,6 +24,8 @@ import java.util.List;
 @Slf4j
 public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepository {
 
+    @Autowired
+    StarterProperties properties;
     @Resource
     public JdbcTemplate jdbcTemplate;
 
@@ -26,6 +34,22 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
      */
     @Override
     public void createTable() {
+        String config = properties.getDropTableFlag();
+        // 如果不删除表,通过页面更改
+        if ("false".equals(config)) {
+            // 判断表是否存在
+            String checkSql = "SHOW TABLES LIKE 't_calculation_formula'";
+            Boolean created = jdbcTemplate.query(checkSql, null, null, new ResultSetExtractor<Boolean>() {
+                @Override
+                public Boolean extractData(ResultSet rs) throws SQLException, DataAccessException {
+                    return rs.next();
+                }
+            });
+            // 如果表存在,不对表做任何操作
+            if (created) {
+                return;
+            }
+        }
         try {
             // 获取初始化公式表sql
             String path = "sql/t_calculation_formula.sql";
@@ -46,6 +70,7 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
             inputStream.close();
             // 执行sql文件
             jdbcTemplate.execute(builder.toString());
+
         } catch (DataAccessException e) {
             e.printStackTrace();
             log.info("删除/创建表错误:" + e.toString());
@@ -58,20 +83,21 @@ public class CalculationFormulaRepositoryImpl implements CalculationFormulaRepos
 
     /**
      * 查询公式数据
-     * @param type     类型
+     *
+     * @param type         类型
      * @param provinceEnum
      * @return
      */
     @Override
-    public List<CalculationFormula> findByTypeAndProvince(String type,String provinceEnum) {
+    public List<CalculationFormula> findByTypeAndProvince(String type, String provinceEnum) {
         // 模糊查询参数
-        provinceEnum = "%"+provinceEnum+";%";
+        provinceEnum = "%" + provinceEnum + ";%";
         // 查询sql
         String sql = "SELECT * from t_calculation_formula where TYPE = ? and PROVINCE_ENUM like ?  ";
         // 根据类型和省调查询公式
         List<CalculationFormula> calculationFormulaList = new ArrayList<>();
         try {
-            calculationFormulaList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CalculationFormula.class), type ,provinceEnum);
+            calculationFormulaList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CalculationFormula.class), type, provinceEnum);
         } catch (DataAccessException e) {
             e.printStackTrace();
         }