Ver Fonte

fix 查找

he2802 há 3 anos atrás
pai
commit
57d8e6b535

+ 85 - 0
zhongzheng-common/src/main/java/com/zhongzheng/common/Factory/TrimmedAnnotationFormatterFactory.java

@@ -0,0 +1,85 @@
+package com.zhongzheng.common.Factory;
+import java.text.ParseException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import com.zhongzheng.common.annotation.Trimmed;
+import org.springframework.format.AnnotationFormatterFactory;
+import org.springframework.format.Formatter;
+import org.springframework.format.Parser;
+import org.springframework.format.Printer;
+
+public class TrimmedAnnotationFormatterFactory implements AnnotationFormatterFactory<Trimmed> {
+
+    private static final Map<Trimmed.TrimmerType, TrimmerFormatter> TRIMMER_FORMATTER_MAP;
+
+    static {
+        Trimmed.TrimmerType[] values = Trimmed.TrimmerType.values();
+        Map<Trimmed.TrimmerType, TrimmerFormatter> map = new HashMap<Trimmed.TrimmerType, TrimmerFormatter>(values.length);
+        for (Trimmed.TrimmerType type : values) {
+            map.put(type, new TrimmerFormatter(type));
+        }
+        TRIMMER_FORMATTER_MAP = Collections.unmodifiableMap(map);
+    }
+
+    @Override
+    public Set<Class<?>> getFieldTypes() {
+        Set<Class<?>> fieldTypes = new HashSet<Class<?>>(1, 1);
+        fieldTypes.add(String.class);
+        return fieldTypes;
+    }
+
+    @Override
+    public Parser<?> getParser(Trimmed annotation, Class<?> fieldType) {
+        return TRIMMER_FORMATTER_MAP.get(annotation.value());
+    }
+
+    @Override
+    public Printer<?> getPrinter(Trimmed annotation, Class<?> fieldType) {
+        return TRIMMER_FORMATTER_MAP.get(annotation.value());
+    }
+
+    private static class TrimmerFormatter implements Formatter<String> {
+
+        private static final Pattern PATTERN_WHITESPACES = Pattern.compile("\\s+");
+        private static final Pattern PATTERN_WHITESPACES_WITH_LINE_BREAK = Pattern.compile("\\s*\\n\\s*");
+        private static final Pattern PATTERN_WHITESPACES_EXCEPT_LINE_BREAK = Pattern.compile("[\\s&&[^\\n]]+");
+
+        private final Trimmed.TrimmerType type;
+
+        public TrimmerFormatter(Trimmed.TrimmerType type) {
+            if (type == null)
+                throw new NullPointerException();
+            this.type = type;
+        }
+
+        @Override
+        public String print(String object, Locale locale) {
+            return object;
+        }
+
+        @Override
+        public String parse(String text, Locale locale) throws ParseException {
+            text = text.trim();
+            switch (type) {
+                case ALL_WHITESPACES:
+                    return PATTERN_WHITESPACES.matcher(text).replaceAll(" ");
+                case EXCEPT_LINE_BREAK:
+                    return PATTERN_WHITESPACES_EXCEPT_LINE_BREAK
+                            .matcher(PATTERN_WHITESPACES_WITH_LINE_BREAK.matcher(text).replaceAll("\n")).replaceAll(" ");
+                case SIMPLE:
+                    return text;
+                default:
+                    // not possible
+                    throw new AssertionError();
+            }
+        }
+
+    }
+
+}

+ 16 - 0
zhongzheng-common/src/main/java/com/zhongzheng/common/annotation/Trimmed.java

@@ -0,0 +1,16 @@
+package com.zhongzheng.common.annotation;
+
+import java.lang.annotation.*;
+
+@Target(value = { ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Trimmed {
+
+    public static enum TrimmerType {
+        SIMPLE, ALL_WHITESPACES, EXCEPT_LINE_BREAK;
+    }
+
+    TrimmerType value() default TrimmerType.ALL_WHITESPACES;
+
+}

+ 2 - 0
zhongzheng-system/src/main/java/com/zhongzheng/modules/goods/bo/GoodsQueryBo.java

@@ -1,5 +1,6 @@
 package com.zhongzheng.modules.goods.bo;
 
+import com.zhongzheng.common.annotation.Trimmed;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
@@ -70,6 +71,7 @@ GoodsQueryBo extends BaseEntity {
 	private Long majorId;
 	/** 商品名称 */
 	@ApiModelProperty("商品名称")
+	@Trimmed
 	private String goodsName;
 	/** 标准价格 */
 	@ApiModelProperty("标准价格")