فهرست منبع

修改名称,代码调用等

david 2 سال پیش
والد
کامیت
d2b27c7a8b

+ 49 - 49
src/main/java/com/roma/romaapi/controller/PrepareSqlController.java → src/main/java/com/roma/romaapi/controller/DbApiController.java

@@ -1,49 +1,49 @@
-package com.roma.romaapi.controller;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import jakarta.servlet.http.HttpServletRequest;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.redis.core.StringRedisTemplate;
-import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
-import org.springframework.util.StringUtils;
-import org.springframework.web.bind.annotation.*;
-import com.roma.romaapi.service.SqlService;
-
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-
-@RestController
-public class PrepareSqlController {
-
-    @Autowired
-    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
-    @Autowired
-    StringRedisTemplate stringRedisTemplate;
-    @Autowired
-    SqlService sqlService;
-    @RequestMapping("/test1")
-    public Object test1() throws JsonProcessingException {
-//        // 获取redis中的值
-//        String res = stringRedisTemplate.opsForValue().get("sysConfig");
-//        System.out.print("res --->");
-//        System.out.print(res);
-        ObjectMapper mapper = new ObjectMapper();
-        String json = "{\"name\":\"mkyong\", \"age\":\"37\"}";
-
-        Object jsonMap = mapper.readValue(json, Map.class);
-        System.out.print(jsonMap);
-
-        return "sqlId";
-    }
-
-    @RequestMapping("/api/{code}")
-    public Map<String, Object> prepareSql(@PathVariable("code") String sqlCode, HttpServletRequest request) {
-        Map<String, String[]> maps = request.getParameterMap();
-        // 通过sqlId执行sql语句
-        Map<String, Object> ret = sqlService.getSql(sqlCode, maps);
-        return ret;
-    }
-}
-
+package com.roma.romaapi.controller;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.roma.romaapi.service.DbApiService;
+import com.roma.romaapi.utils.CustomResponse;
+import jakarta.servlet.http.HttpServletRequest;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Map;
+
+@RestController
+public class DbApiController {
+
+    @Autowired
+    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
+    @Autowired
+    StringRedisTemplate stringRedisTemplate;
+    @Autowired
+    DbApiService dbApiService;
+    @RequestMapping("/test1")
+    public Object test1() throws JsonProcessingException {
+//        // 获取redis中的值
+//        String res = stringRedisTemplate.opsForValue().get("sysConfig");
+//        System.out.print("res --->");
+//        System.out.print(res);
+        ObjectMapper mapper = new ObjectMapper();
+        String json = "{\"name\":\"mkyong\", \"age\":\"37\"}";
+
+        Object jsonMap = mapper.readValue(json, Map.class);
+        System.out.print(jsonMap);
+
+        return "sqlId";
+    }
+
+    @RequestMapping("/dbapi/{code}")
+    public Map dbApi(@PathVariable("code") String sqlCode, HttpServletRequest request) {
+        Map<String, String[]> maps = request.getParameterMap();
+        // 通过sqlId执行sql语句
+        Map<String, Object> ret = dbApiService.transferSql(sqlCode, maps);
+        return CustomResponse.formatResponse(ret);
+    }
+}
+

+ 2 - 2
src/main/java/com/roma/romaapi/controller/PageController.java

@@ -1,7 +1,7 @@
 package com.roma.romaapi.controller;
 
 import com.roma.romaapi.service.PageService;
-import com.roma.romaapi.utils.Response;
+import com.roma.romaapi.utils.CustomResponse;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -18,6 +18,6 @@ public class PageController {
     @RequestMapping("/page/{code}")
     public Map pageJson(@PathVariable("code") String pageCode) {
         // 获取页面的json
-        return Response.success(pageService.getPageJsonByCode(pageCode));
+        return CustomResponse.formatResponse(pageService.getPageJsonByCode(pageCode));
     }
 }

+ 66 - 67
src/main/java/com/roma/romaapi/dao/SqlApiDao.java → src/main/java/com/roma/romaapi/dao/DbApiDao.java

@@ -1,67 +1,66 @@
-package com.roma.romaapi.dao;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.dao.EmptyResultDataAccessException;
-import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
-import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
-import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.core.namedparam.SqlParameterSource;
-import org.springframework.stereotype.Repository;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-
-// 执行具体sql语句
-@Repository
-public class SqlApiDao {
-
-    @Autowired
-    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
-    public Map<String, Object> getSql(String sqlCode) {
-        try {
-            Map<String, Object> paramMap = new HashMap<>();
-            paramMap.put("sql_code", sqlCode);
-            Map<String, Object> resMap2 = namedParameterJdbcTemplate.queryForMap(
-                        "SELECT * FROM `sql_details` where sql_code = :sql_code", paramMap);
-            return  resMap2;
-        } catch (EmptyResultDataAccessException e) {
-            Map<String, Object> resultMap = new HashMap<>();
-            resultMap.put("sysErrorCode", "500");
-
-            return resultMap;
-        }
-    }
-
-    // 获取sql总数,需要完整的sql和要绑定的参数
-    public Integer getSqlCount(String sql, Map bindValue) {
-        Integer count = namedParameterJdbcTemplate.queryForObject(sql, bindValue, Integer.class);
-        return  count;
-    }
-
-    // 获取查询数据--单条数据使用
-    public Map selectData(String sql, Map bindValue) {
-        try {
-            Map<String, Object> resMap2 = namedParameterJdbcTemplate.queryForMap(sql, bindValue);
-            return  resMap2;
-        } catch (EmptyResultDataAccessException e) {
-            Map<String, Object> resultMap = new HashMap<>();
-            resultMap.put("sysErrorCode", "500");
-
-            return resultMap;
-        }
-    }
-
-    // 查询数据--多条数据使用
-    public List<Map<String, Object>> selectMultipleData(String sql, Map<String, Object> bindValue){
-        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, bindValue);
-        return  resMap2;
-    }
-
-    // 执行删除语句
-    public Integer updateData(String sql, Map<String, Object> bindValue){
-        Integer row = namedParameterJdbcTemplate.update(sql, bindValue);
-        return  row;
-    }
-}
+package com.roma.romaapi.dao;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
+import org.springframework.stereotype.Repository;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+// 执行具体sql语句
+@Repository
+public class DbApiDao {
+
+    @Autowired
+    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
+    // 通过code,获取要执行的sql
+    public Map<String, Object> dbQueryForMap(String sqlCode) {
+        try {
+            Map<String, Object> paramMap = new HashMap<>();
+            paramMap.put("sql_code", sqlCode);
+            Map<String, Object> resMap2 = namedParameterJdbcTemplate.queryForMap(
+                        "SELECT * FROM `sql_details` where sql_code = :sql_code AND is_enable=1", paramMap);
+            return  resMap2;
+        } catch (EmptyResultDataAccessException e) {
+            Map<String, Object> resultMap = new HashMap<>();
+            resultMap.put("sysErrorCode", "500");
+
+            return resultMap;
+        }
+    }
+
+    // 获取sql总数,需要完整的sql和要绑定的参数
+    public Integer dbCountQueryForObject(String sql, Map bindValue) {
+        Integer count = namedParameterJdbcTemplate.queryForObject(sql, bindValue, Integer.class);
+
+        return  count;
+    }
+
+    // 获取查询数据--单条数据使用
+    public Map dbCustomQueryForMap(String sql, Map bindValue) {
+        try {
+            Map<String, Object> resMap2 = namedParameterJdbcTemplate.queryForMap(sql, bindValue);
+            return  resMap2;
+        } catch (EmptyResultDataAccessException e) {
+            Map<String, Object> resultMap = new HashMap<>();
+            resultMap.put("sysErrorCode", "500");
+
+            return resultMap;
+        }
+    }
+
+    // 查询数据--多条数据使用
+    public List<Map<String, Object>> dbCustomQueryForList(String sql, Map<String, Object> bindValue){
+        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, bindValue);
+        return  resMap2;
+    }
+
+    // 执行增删改语句
+    public Integer dbUpdate(String sql, Map<String, Object> bindValue){
+        Integer row = namedParameterJdbcTemplate.update(sql, bindValue);
+        return  row;
+    }
+}

+ 3 - 3
src/main/java/com/roma/romaapi/dao/PageDao.java

@@ -14,18 +14,18 @@ public class PageDao {
     private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
 
     // 获取页面json
-    public Map getPageJsonByCode(String pageCode) {
+    public Map dbQueryForMap(String pageCode) {
         try {
             Map<String, Object> paramMap = new HashMap<>();
             paramMap.put("page_code", pageCode);
             Map<String, Object> resMap2 = namedParameterJdbcTemplate.queryForMap(
-                    "SELECT `detail` FROM `page_details` where `page_code` = :page_code", paramMap);
+                    "SELECT `page_json` FROM `page_details` where `page_code` = :page_code", paramMap);
 
-            System.out.print("-----获取页面json的结果-----");
             return resMap2;
         } catch (EmptyResultDataAccessException e) {
             Map<String, Object> resultMap = new HashMap<>();
             resultMap.put("sysErrorCode", "500");
+            resultMap.put("sysErrorMessage", "operation failed");
 
             return resultMap;
         }

+ 109 - 110
src/main/java/com/roma/romaapi/service/SqlService.java → src/main/java/com/roma/romaapi/service/DbApiService.java

@@ -1,110 +1,109 @@
-package com.roma.romaapi.service;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-import com.roma.romaapi.dao.SqlApiDao;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-@Service
-public class SqlService {
-
-    @Autowired
-    SqlApiDao sqlApiDao;  // 引入dao层
-    public Map<String, Object> getSql(String sqlCode, Map<String, String[]> maps){
-        Map hasSql = sqlApiDao.getSql(sqlCode);
-        boolean isEmpty = hasSql.containsKey("sysErrorCode");
-        if(isEmpty) {
-            return  hasSql;
-        }
-        // 获取全部的请求参数
-        Map bindData = new HashMap<>();
-        String page = "1"; // 页数
-        String perPage = "15"; // 显示多少条
-        // 组装要绑定的参数数据
-        for (Map.Entry<String, String[]> entry : maps.entrySet()) {
-            String paramsKey = entry.getKey();
-            String paramsValue = entry.getValue()[0];
-            String fourBeforestring = "";
-            if(paramsKey.length() >= 4) {
-                fourBeforestring = paramsKey.substring(0,4);
-                if (fourBeforestring.equals("api_")) {
-                    bindData.put(paramsKey, paramsValue);
-                }
-            }
-            if(paramsKey.equals("page")) {
-                page = paramsValue;
-            }
-            if(paramsKey.equals("perPage")) {
-                perPage = paramsValue;
-            }
-        }
-
-        Object sqlType = hasSql.get("sql_type");
-        Map ret = new HashMap<>();
-        switch (sqlType.toString()) {
-            case "select":
-                // 查询语句,可带分页,或者不带分页
-                ret = this.transferSelect(hasSql, bindData, page, perPage);
-                break;
-            case "update":
-                Integer updateRow = this.transferUpdate(hasSql, bindData);
-                if(updateRow <= 0) {
-                    ret.put("sysErrorCode", "500");
-                }
-                break;
-            default:
-                break;
-        }
-
-        return ret;
-    }
-
-
-    // 转换查询
-    public Map transferSelect(Map sqlData, Map bindData, String page, String perPage) {
-        Map res = new HashMap<>();
-        Object sql = sqlData.get("detail");// 获取sql语句
-        Object isProcedure = sqlData.get("is_procedure");// 是否存储过程
-        Object isList = sqlData.get("is_list");// 是否查单条数据
-        // 如果不是存储过程
-        if(isProcedure.equals(0)) {
-            if(isList.equals(2)) {
-                // 如果查询单条数据
-                String selectSql = sql.toString();
-                Map info = sqlApiDao.selectData(selectSql, bindData);
-                Integer count = info.isEmpty() ? 0 : 1;
-                res.put("count", count);
-                res.put("rows", info);
-            } else if(isList.equals(1)) {
-                // 如果是查询列表
-                Integer nowPage = Integer.parseInt(page);  // 当前第几页
-                Integer nowPerPage = Integer.parseInt(perPage); // 每页多少条
-                String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + sql + " ) AS ROMA";
-                Integer count = sqlApiDao.getSqlCount(countSql, bindData);
-                Integer maxPage = count / nowPerPage + (count % nowPerPage != 0 ? 1 : 0);// 总数/每页多少条=最多多少页
-                if (nowPage <= 1) {
-                    nowPage = 1;
-                } else if (maxPage > 0 && nowPage >= maxPage) {
-                    nowPage = maxPage;
-                }
-                Integer startNum = (nowPage - 1) * nowPerPage;
-                String selectSql = sql + " LIMIT " + startNum + "," + nowPerPage;
-                List info = sqlApiDao.selectMultipleData(selectSql, bindData);
-                res.put("count", count);
-                res.put("rows", info);
-            }
-        }
-        return  res;
-    }
-
-    // 执行删除语句
-    public Integer transferUpdate(Map sqlData, Map bindData){
-        Object sql = sqlData.get("detail");// 获取sql语句
-        Integer ret = sqlApiDao.updateData(sql.toString(), bindData);
-
-        return  ret;
-    }
-}
+package com.roma.romaapi.service;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import com.roma.romaapi.dao.DbApiDao;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Service
+public class DbApiService {
+
+    @Autowired
+    DbApiDao dbApiDao;
+    public Map<String, Object> transferSql(String sqlCode, Map<String, String[]> maps){
+        Map hasSql = dbApiDao.dbQueryForMap(sqlCode);
+        boolean isEmpty = hasSql.containsKey("sysErrorCode");
+        if(isEmpty) {
+            return  hasSql;
+        }
+        // 获取全部的请求参数
+        Map bindData = new HashMap<>();
+        String page = "1"; // 页数
+        String perPage = "15"; // 显示多少条
+        // 组装要绑定的参数数据
+        for (Map.Entry<String, String[]> entry : maps.entrySet()) {
+            String paramsKey = entry.getKey();
+            String paramsValue = entry.getValue()[0];
+            String fourBeforestring = "";
+            if(paramsKey.length() >= 4) {
+                fourBeforestring = paramsKey.substring(0,4);
+                if (fourBeforestring.equals("api_")) {
+                    bindData.put(paramsKey, paramsValue);
+                }
+            }
+            if(paramsKey.equals("page")) {
+                page = paramsValue;
+            }
+            if(paramsKey.equals("perPage")) {
+                perPage = paramsValue;
+            }
+        }
+
+        Object sqlBehavior = hasSql.get("sql_type");
+        Map ret = new HashMap<>();
+        // query查询行为 update数据有变动行为
+        switch (sqlBehavior.toString()) {
+            case "query":
+                // 查询语句,可带分页,或者不带分页
+                ret = this.transferSelect(hasSql, bindData, page, perPage);
+                break;
+            case "update":
+                Integer updateRow = this.transferUpdate(hasSql, bindData);
+                if(updateRow <= 0) {
+                    ret.put("sysErrorCode", "500");
+                }
+                break;
+            default:
+                break;
+        }
+
+        return ret;
+    }
+
+
+    // 转换查询
+    public Map transferSelect(Map sqlData, Map bindData, String page, String perPage) {
+        Map res = new HashMap<>();
+        Object sql = sqlData.get("sql_string");// 获取sql语句
+        Object isList = sqlData.get("is_list");// 是否查单条数据
+
+        if(isList.equals(2)) {
+            // 如果查询单条数据
+            String selectSql = sql.toString();
+            Map info = dbApiDao.dbCustomQueryForMap(selectSql, bindData);
+            Integer count = info.isEmpty() ? 0 : 1;
+            res.put("count", count);
+            res.put("rows", info);
+        } else if(isList.equals(1)) {
+            // 如果是查询列表
+            Integer nowPage = Integer.parseInt(page);  // 当前第几页
+            Integer nowPerPage = Integer.parseInt(perPage); // 每页多少条
+            String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + sql + " ) AS ROMA";
+            Integer count = dbApiDao.dbCountQueryForObject(countSql, bindData);
+            Integer maxPage = count / nowPerPage + (count % nowPerPage != 0 ? 1 : 0);// 总数/每页多少条=最多多少页
+            if (nowPage <= 1) {
+                nowPage = 1;
+            } else if (maxPage > 0 && nowPage >= maxPage) {
+                nowPage = maxPage;
+            }
+            Integer startNum = (nowPage - 1) * nowPerPage;
+            String selectSql = sql + " LIMIT " + startNum + "," + nowPerPage;
+            List info = dbApiDao.dbCustomQueryForList(selectSql, bindData);
+            res.put("count", count);
+            res.put("rows", info);
+        }
+
+        return  res;
+    }
+
+    // 执行增删改
+    public Integer transferUpdate(Map sqlData, Map bindData){
+        Object sql = sqlData.get("sql_string");// 获取sql语句
+        Integer ret = dbApiDao.dbUpdate(sql.toString(), bindData);
+
+        return  ret;
+    }
+}

+ 1 - 1
src/main/java/com/roma/romaapi/service/PageService.java

@@ -11,7 +11,7 @@ public class PageService {
     @Autowired
     PageDao pageDao;  // 引入dao层
     public Map getPageJsonByCode(String pageCode){
-        Map ret = pageDao.getPageJsonByCode(pageCode);
+        Map ret = pageDao.dbQueryForMap(pageCode);
 
         return ret;
     }

+ 46 - 0
src/main/java/com/roma/romaapi/utils/CustomResponse.java

@@ -0,0 +1,46 @@
+package com.roma.romaapi.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+// 相应工具类
+public class CustomResponse {
+    public static <T> Map success(T data) {
+        Map<String, Object> result = new HashMap<>();
+        result.put("code", "200");
+        result.put("message", "success");
+        result.put("data", data);
+
+        return result;
+    }
+
+    public static <T> Map error(T data, String code, String message) {
+        Map<String, Object> result = new HashMap<>();
+        result.put("code", code);
+        result.put("message", message);
+        result.put("data", data);
+
+        return result;
+    }
+
+    // 格式化响应
+    public static Map formatResponse(Map data) {
+        Map<String, Object> result = new HashMap<>();
+        String code = "200";
+        String message = "success";
+        boolean isEmpty = data.containsKey("sysErrorCode");
+        if(isEmpty) {
+            Object errorCode = data.get("sysErrorCode");
+            Object errorMessage = data.containsKey("sysErrorMessage") ? data.get("sysErrorMessage")
+                    : new String("operation failed!");
+            result.put("code", errorCode.toString());
+            result.put("message", errorMessage.toString());
+            result.put("data", data);
+        } else {
+            result.put("code", code);
+            result.put("message", message);
+            result.put("data", data);
+        }
+
+        return result;
+    }
+}

+ 0 - 24
src/main/java/com/roma/romaapi/utils/Response.java

@@ -1,24 +0,0 @@
-package com.roma.romaapi.utils;
-
-import java.util.HashMap;
-import java.util.Map;
-// 相应工具类
-public class Response {
-    public static <T> Map success(T data) {
-        Map<String, Object> result = new HashMap<>();
-        result.put("code", "200");
-        result.put("message", "success");
-        result.put("data", data);
-
-        return result;
-    }
-
-    public static <T> Map error(T data, String code, String message) {
-        Map<String, Object> result = new HashMap<>();
-        result.put("code", code);
-        result.put("message", message);
-        result.put("data", data);
-
-        return result;
-    }
-}

+ 1 - 1
src/main/resources/application.properties

@@ -1 +1 @@
-#服务
server.port=8081

#数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://172.29.197.37:3306/lowcode?serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = mypass

#redis
spring.redis.database=0
spring.redis.password=
#spring.redis.password=name:password
spring.redis.port=6379
spring.redis.host=172.17.206.98
spring.redis.timeout=5000
spring.redis.lettuce.pool.max-active=3
spring.redis.lettuce.pool.min-idle=2
spring.redis.lettuce.pool.max-idle=3
spring.redis.lettuce.pool.max-wait=-1
#spring.redis.lettuce.shutdown-timeout=100
#spring.cache.redis.cache-null-values=false


+#服务
server.port=8081

#数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://172.26.143.101:3306/lowcode?serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = mypass

#redis
spring.redis.database=0
spring.redis.password=
#spring.redis.password=name:password
spring.redis.port=6379
spring.redis.host=172.26.143.101
spring.redis.timeout=5000
spring.redis.lettuce.pool.max-active=3
spring.redis.lettuce.pool.min-idle=2
spring.redis.lettuce.pool.max-idle=3
spring.redis.lettuce.pool.max-wait=-1
#spring.redis.lettuce.shutdown-timeout=100
#spring.cache.redis.cache-null-values=false