zhouhao пре 6 година
родитељ
комит
e4222cddcf

+ 14 - 3
hsweb-commons/hsweb-commons-controller/src/main/java/org/hswebframework/web/controller/message/ResponseMessage.java

@@ -28,7 +28,9 @@ import java.lang.reflect.Field;
 import java.util.*;
 
 /**
- * 响应消息。controller中处理后,返回此对象,响应请求结果给客户端。
+ * 响应消息,controller中处理后,返回此对象,响应请求结果给客户端。
+ *
+ * @since 2.0
  */
 @ApiModel(description = "响应结果")
 public class ResponseMessage<T> implements Serializable {
@@ -42,6 +44,9 @@ public class ResponseMessage<T> implements Serializable {
 
     private Long timestamp;
 
+    /**
+     * @since 3.0.0-RC
+     */
     private String code;
 
     @ApiModelProperty("调用结果消息")
@@ -101,6 +106,12 @@ public class ResponseMessage<T> implements Serializable {
         return this;
     }
 
+
+    public ResponseMessage<T> code(String code) {
+        this.code = code;
+        return this;
+    }
+
     /**
      * 过滤字段:指定需要序列化的字段
      */
@@ -134,7 +145,7 @@ public class ResponseMessage<T> implements Serializable {
                     if (field1 != null) {
                         include(field1.getType(), tmp[1]);
                     }
-                } catch (Throwable e) {
+                } catch (Exception ignore) {
                 }
             } else {
                 getStringListFromMap(includes, type).add(field);
@@ -158,7 +169,7 @@ public class ResponseMessage<T> implements Serializable {
                     if (field1 != null) {
                         exclude(field1.getType(), tmp[1]);
                     }
-                } catch (Throwable e) {
+                } catch (Exception ignore) {
                 }
             } else {
                 getStringListFromMap(excludes, type).add(field);

+ 3 - 11
hsweb-commons/hsweb-commons-dao/hsweb-commons-dao-mybatis/src/main/java/org/hswebframework/web/dao/mybatis/builder/SqlParamParser.java

@@ -1,19 +1,14 @@
 package org.hswebframework.web.dao.mybatis.builder;
 
-import org.apache.commons.beanutils.BeanMap;
-import org.hswebframework.ezorm.core.Conditional;
-import org.hswebframework.ezorm.core.NestConditional;
 import org.hswebframework.ezorm.core.dsl.Query;
-import org.hswebframework.ezorm.core.param.Param;
-import org.hswebframework.ezorm.core.param.QueryParam;
 import org.hswebframework.ezorm.core.param.Term;
 import org.hswebframework.utils.StringUtils;
+import org.hswebframework.web.bean.FastBeanCopier;
 import org.hswebframework.web.commons.entity.Entity;
-import org.hswebframework.web.commons.entity.QueryEntity;
 import org.hswebframework.web.commons.entity.param.QueryParamEntity;
 
-import java.util.Collection;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.function.BiConsumer;
@@ -63,10 +58,7 @@ public class SqlParamParser {
                     }
                 });
             } else {
-                Map map = new HashMap<>(new BeanMap(obj));
-                map.remove("class");
-                map.remove("declaringClass");
-                parse(map);
+                parse(FastBeanCopier.copy(obj, new LinkedHashMap<>()));
             }
             return this;
         }

+ 5 - 0
hsweb-commons/hsweb-commons-entity/pom.xml

@@ -44,6 +44,11 @@
             <groupId>org.hswebframework</groupId>
             <artifactId>hsweb-utils</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-commons-utils</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.hibernate</groupId>
             <artifactId>hibernate-validator</artifactId>

+ 15 - 1
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/QueryEntity.java

@@ -1,9 +1,23 @@
 package org.hswebframework.web.commons.entity;
 
+import org.hswebframework.web.HttpParameterConverter;
+
+import java.util.Map;
+import java.util.StringJoiner;
+
 /**
  * @author zhouhao
  * @since 3.0
  */
 public interface QueryEntity extends Entity {
-   
+    /**
+     *  转为http查询参数
+     * @return
+     */
+    default String toHttpQueryParamString() {
+        Map<String, String> result = new HttpParameterConverter(this).convert();
+        StringJoiner joiner = new StringJoiner("&");
+        result.forEach((key, value) -> joiner.add(key.concat("=").concat(value)));
+        return joiner.toString();
+    }
 }

+ 8 - 1
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/param/DeleteParamEntity.java

@@ -2,6 +2,7 @@ package org.hswebframework.web.commons.entity.param;
 
 import org.hswebframework.ezorm.core.param.Param;
 import org.hswebframework.web.commons.entity.Entity;
+import org.hswebframework.web.commons.entity.QueryEntity;
 
 /**
  * 查询参数实体,使用<a href="https://github.com/hs-web/hsweb-easy-orm">easyorm</a>进行动态查询参数构建<br>
@@ -13,7 +14,7 @@ import org.hswebframework.web.commons.entity.Entity;
  * @see Entity
  * @since 3.0
  */
-public class DeleteParamEntity extends Param implements Entity {
+public class DeleteParamEntity extends Param implements QueryEntity {
     private static final long serialVersionUID = 6120598637420234301L;
 
     /**
@@ -26,4 +27,10 @@ public class DeleteParamEntity extends Param implements Entity {
     public static DeleteParamEntity build() {
         return new DeleteParamEntity();
     }
+
+    @Override
+    public String toString() {
+        return toHttpQueryParamString();
+    }
+
 }

+ 12 - 0
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/param/QueryParamEntity.java

@@ -41,7 +41,19 @@ public class QueryParamEntity extends QueryParam implements QueryEntity {
      * @see QueryParam#where(String, Object)
      */
     public static QueryParamEntity single(String field, Object value) {
+        return of(field, value);
+    }
+
+    /**
+     * @see this#single(String, Object)
+     */
+    public static QueryParamEntity of(String field, Object value) {
         return empty().where(field, value);
     }
 
+    @Override
+    public String toString() {
+        return toHttpQueryParamString();
+    }
+
 }

+ 8 - 1
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/param/UpdateParamEntity.java

@@ -2,6 +2,7 @@ package org.hswebframework.web.commons.entity.param;
 
 import org.hswebframework.ezorm.core.param.UpdateParam;
 import org.hswebframework.web.commons.entity.Entity;
+import org.hswebframework.web.commons.entity.QueryEntity;
 
 /**
  * 修改参数实体,使用<a href="https://github.com/hs-web/hsweb-easy-orm">easyorm</a>进行动态参数构建
@@ -11,7 +12,7 @@ import org.hswebframework.web.commons.entity.Entity;
  * @see Entity
  * @since 3.0
  */
-public class UpdateParamEntity<T> extends UpdateParam<T> implements Entity {
+public class UpdateParamEntity<T> extends UpdateParam<T> implements QueryEntity {
     private static final long serialVersionUID = -4074863219482678510L;
 
     public UpdateParamEntity() {
@@ -52,4 +53,10 @@ public class UpdateParamEntity<T> extends UpdateParam<T> implements Entity {
     public static <T> UpdateParamEntity<T> build(T data, String field, Object value) {
         return new UpdateParamEntity<>(data).where(field, value);
     }
+
+    @Override
+    public String toString() {
+        return toHttpQueryParamString();
+    }
+
 }

+ 8 - 3
hsweb-core/src/main/java/org/hswebframework/web/bean/FastBeanCopier.java

@@ -129,7 +129,6 @@ public final class FastBeanCopier {
                 "\n}\n" +
                 "\n}";
         try {
-//            System.out.println(method);
             return Proxy.create(Copier.class)
                     .addMethod(method)
                     .newInstance();
@@ -140,16 +139,22 @@ public final class FastBeanCopier {
     }
 
     private static Map<String, ClassProperty> createProperty(Class type) {
+
+        List<String> fieldNames = Arrays.stream(type.getDeclaredFields())
+                .map(Field::getName).collect(Collectors.toList());
+
         return Stream.of(propertyUtils.getPropertyDescriptors(type))
                 .filter(property -> !property.getName().equals("class") && property.getReadMethod() != null && property.getWriteMethod() != null)
                 .map(BeanClassProperty::new)
-                .collect(Collectors.toMap(ClassProperty::getName, Function.identity()));
+                //让字段有序
+                .sorted(Comparator.comparing(property -> fieldNames.indexOf(property.name)))
+                .collect(Collectors.toMap(ClassProperty::getName, Function.identity(), (k, k2) -> k, LinkedHashMap::new));
 
     }
 
     private static Map<String, ClassProperty> createMapProperty(Map<String, ClassProperty> template) {
         return template.values().stream().map(classProperty -> new MapClassProperty(classProperty.name))
-                .collect(Collectors.toMap(ClassProperty::getName, Function.identity()));
+                .collect(Collectors.toMap(ClassProperty::getName, Function.identity(), (k, k2) -> k, LinkedHashMap::new));
     }
 
     private static String createCopierCode(Class source, Class target) {