소스 검색

分页和传参判空问题

shiqian 4 년 전
부모
커밋
16a5e2d0a1

+ 24 - 0
boman-common/boman-common-core/src/main/java/com/boman/common/core/utils/obj/ObjectUtils.java

@@ -87,6 +87,30 @@ public class ObjectUtils {
         return input;
     }
 
+
+    public static JSONObject ifNullSetEmpty(JSONObject input) {
+        if (input == null || input.isEmpty()) {
+            input = new JSONObject();
+        }
+        return input;
+    }
+
+    public static JSONArray ifNullSetEmpty(JSONArray input) {
+        if (input == null || input.isEmpty()) {
+            input = new JSONArray();
+        }
+        return input;
+    }
+
+
+    public static boolean isNotEmpty(JSONObject input){
+        return input != null && !input.isEmpty();
+    }
+
+    public static boolean isNotEmpty(JSONArray input){
+        return input != null && !input.isEmpty();
+    }
+
     /**
      * 功能描述: 暂且只做 string collection long 三种类型的校验
      *

+ 16 - 4
boman-modules/boman-system/src/main/java/com/boman/system/common/BaseTableSaveDTO.java

@@ -56,12 +56,24 @@ public class BaseTableSaveDTO implements Serializable {
     /**
      * 分页
      */
-    @JSONField(name = "limit")
-    private Integer limit;
+    @JSONField(name = "pageNo")
+    private Integer pageNo;
 
     /**
      * 分页
      */
-    @JSONField(name = "offset")
-    private Integer offset;
+    @JSONField(name = "pageSize")
+    private Integer pageSize;
+
+
+    public int getLimit() {
+        return pageNo == 0 ? 0 : (pageNo - 1) * pageSize;
+    }
+
+    public int getOffset(){
+        return pageSize == 0 ? 10 : pageSize;
+    }
+
 }
+
+

+ 5 - 4
boman-modules/boman-system/src/main/java/com/boman/system/common/TableServiceCmdService.java

@@ -134,14 +134,15 @@ public class TableServiceCmdService {
         // 拿到每个字段对应的查询类型,=、 like、 >、 <
         GenTable genTable = getTableFromRedisByTableName(RedisKey.TABLE_INFO, dto.getTable());
         JSONObject fixedData = dto.getFixedData();
-        requireNonNull(fixedData);
+        fixedData = ObjectUtils.ifNullSetEmpty(fixedData);
+
         // 查询条件
-        JSONObject condition = fixedData.getJSONObject(CONDITION);
+        JSONObject condition = ObjectUtils.ifNullSetEmpty(fixedData.getJSONObject(CONDITION));
         List<GenTableColumn> columns = genTable.getColumns();
         // 封装好以后的查询条件
-        JSONObject packCondition =  packColCondition(columns, condition);
+        JSONObject packCondition = ObjectUtils.ifNullSetEmpty(packColCondition(columns, condition));
         // 需要返回到前台的列
-        JSONArray showData = fixedData.getJSONArray(SHOW_DATA);
+        JSONArray showData = ObjectUtils.ifNullSetEmpty(fixedData.getJSONArray(SHOW_DATA));
         JSONObject rows = new JSONObject();
         int total = selectService.countByCondition(genTable.getTableName(), condition, packCondition);
         rows.put(FormDataConstant.PAGE_TOTAL, total);

+ 30 - 32
boman-modules/boman-system/src/main/java/com/boman/system/mapper/StandardlyMapper.java

@@ -457,28 +457,20 @@ public interface StandardlyMapper {
             wholeSql.append("select ");
             // showData
             StringBuilder showDataSql = new StringBuilder();
-            for (Object columnObj : showData) {
-                String columnName = (String) columnObj;
-                showDataSql.append(columnName).append(", ");
+            if (ObjectUtils.isNotEmpty(showData)) {
+                for (Object columnObj : showData) {
+                    String columnName = (String) columnObj;
+                    showDataSql.append(columnName).append(", ");
+                }
+                wholeSql.append(StringUtils.substringBeforeLast(showDataSql.toString(), ","));
+            } else {
+                showDataSql.append(" * ");
             }
 
-            wholeSql.append(StringUtils.substringBeforeLast(showDataSql.toString(), ","));
-            wholeSql.append(" from ").append(tableName);
-            wholeSql.append(" where ");
+            wholeSql.append(showDataSql).append(" from ").append(tableName);
             // 条件
-            StringBuilder conditionSql = new StringBuilder();
-            for (Map.Entry<String, Object> entry : packCondition.entrySet()) {
-                String key = entry.getKey();
-                Object valueObj = entry.getValue();
-                String valueStr = ((String) valueObj);
-                String[] split = valueStr.split("_");
-                String value = split[0];
-                String queryType = split[1];
-                String columnType = split[2];
-                conditionSql.append(key).append(covert(queryType, columnType, key, value)).append(" and ");
-            }
+            packCondition(packCondition, wholeSql);
 
-            wholeSql.append(StringUtils.substringBeforeLast(conditionSql.toString(), " and"));
             wholeSql.append(" order by ").append(orderBy).append(" limit ").append(limit);
             if (ObjectUtils.isNotEmpty(offset)) {
                 wholeSql.append(", ").append(offset);
@@ -498,27 +490,33 @@ public interface StandardlyMapper {
             StringBuilder wholeSql = new StringBuilder();
             wholeSql.append("select count(1)");
             wholeSql.append(" from ").append(tableName);
-            wholeSql.append(" where ");
             // 条件
-            StringBuilder conditionSql = new StringBuilder();
-            for (Map.Entry<String, Object> entry : packCondition.entrySet()) {
-                String key = entry.getKey();
-                Object valueObj = entry.getValue();
-                String valueStr = ((String) valueObj);
-                // {@link com.boman.system.common.TableServiceCmdService.packValue} 这里是拼参数的地方
-                String[] split = valueStr.split("_");
-                String value = split[0];
-                String queryType = split[1];
-                String columnType = split[2];
-                conditionSql.append(key).append(covert(queryType, columnType, key, value)).append(" and ");
-            }
+            packCondition(packCondition, wholeSql);
 
-            wholeSql.append(StringUtils.substringBeforeLast(conditionSql.toString(), " and"));
             String result = wholeSql.toString();
             LOGGER.info("查询count拼出的sql语句为:{}", result);
             return result;
         }
 
+        private void packCondition(JSONObject packCondition, StringBuilder wholeSql) {
+            if (ObjectUtils.isNotEmpty(packCondition)) {
+                wholeSql.append(" where ");
+                StringBuilder conditionSql = new StringBuilder();
+                for (Map.Entry<String, Object> entry : packCondition.entrySet()) {
+                    String key = entry.getKey();
+                    Object valueObj = entry.getValue();
+                    String valueStr = ((String) valueObj);
+                    // {@link com.boman.system.common.TableServiceCmdService.packValue} 这里是拼参数的地方
+                    String[] split = valueStr.split("_");
+                    String value = split[0];
+                    String queryType = split[1];
+                    String columnType = split[2];
+                    conditionSql.append(key).append(covert(queryType, columnType, key, value)).append(" and ");
+                }
+                wholeSql.append(StringUtils.substringBeforeLast(conditionSql.toString(), " and"));
+            }
+        }
+
         private String covert(String queryType, String columnType, String key, String value) {
             // false 不需要转义
             boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR)

+ 3 - 6
boman-modules/boman-system/src/main/java/com/boman/system/service/impl/BaseSelectServiceImpl.java

@@ -39,11 +39,10 @@ public class BaseSelectServiceImpl implements IBaseSelectService {
      * @return java.util.List<com.alibaba.fastjson.JSONObject>
      */
     @Override
-    public List<JSONObject> selectByCondition(String tableName, JSONObject condition, JSONObject packCondition, JSONArray showData, String orderBy, Integer limit, Integer offset) {
+    public List<JSONObject> selectByCondition(String tableName, JSONObject condition
+            , JSONObject packCondition, JSONArray showData, String orderBy, Integer limit, Integer offset) {
         requireNonNull(tableName);
-        requireNonNull(condition);
-        requireNonNull(packCondition);
-        requireNonNull(showData);
+//        requireNonNull(showData);
         requireNonNull(limit);
         requireNonNull(orderBy);
         return mapper.selectByCondition(tableName, condition, packCondition, showData, orderBy, limit, offset);
@@ -60,8 +59,6 @@ public class BaseSelectServiceImpl implements IBaseSelectService {
     @Override
     public int countByCondition(String tableName, JSONObject condition, JSONObject packCondition) {
         requireNonNull(tableName);
-        requireNonNull(condition);
-        requireNonNull(packCondition);
         return mapper.countByCondition(tableName, condition, packCondition);
     }