From b0e7ae05d5f95edd34af56fccd0c26456be928b3 Mon Sep 17 00:00:00 2001 From: zhaoyong_sh Date: Mon, 12 Apr 2021 17:38:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=A0=A1=E9=AA=8C=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common-common/pom.xml | 36 ++++++++++++ .../common/annotation/DateFormatCheck.java | 47 ++++++++++++++++ .../common/annotation/ValidValue.java | 55 +++++++++++++++++++ .../support/DataFormatCheckValidator.java | 36 ++++++++++++ .../support/ValidValueValidator.java | 49 +++++++++++++++++ .../common/model/CommonResponse.java | 43 +++++++++++++++ .../framework/common/utils/ArrayUtils.java | 54 ++++++++++++++++++ .../framework/common/utils/StringUtils.java | 41 ++++++++++++++ pom.xml | 37 +++++++++++-- smart-datasource/pom.xml | 14 +++++ 10 files changed, 408 insertions(+), 4 deletions(-) create mode 100644 common-common/src/main/java/cn/azxo/framework/common/annotation/DateFormatCheck.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/annotation/ValidValue.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/annotation/support/DataFormatCheckValidator.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/annotation/support/ValidValueValidator.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/model/CommonResponse.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/utils/ArrayUtils.java create mode 100644 common-common/src/main/java/cn/azxo/framework/common/utils/StringUtils.java diff --git a/common-common/pom.xml b/common-common/pom.xml index 00cae20..fc86ef4 100644 --- a/common-common/pom.xml +++ b/common-common/pom.xml @@ -12,12 +12,34 @@ common-common + + UTF-8 + 1.8 + 1.8 + + 1.7.5 + 1.18.18 + 4.3.19.RELEASE + 2.0.0.Final + 6.0.16.Final + + org.slf4j slf4j-api provided + + javax.validation + validation-api + provided + + + org.hibernate + hibernate-validator + provided + org.springframework spring-context @@ -30,4 +52,18 @@ + + + + rdc-releases + Nexus Release Repository + https://packages.aliyun.com/maven/repository/2005773-release-XI7cl5/ + + + rdc-snapshots + Nexus Snapshot Repository + https://packages.aliyun.com/maven/repository/2005773-snapshot-V5Gjdf/ + + + diff --git a/common-common/src/main/java/cn/azxo/framework/common/annotation/DateFormatCheck.java b/common-common/src/main/java/cn/azxo/framework/common/annotation/DateFormatCheck.java new file mode 100644 index 0000000..f0b1932 --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/annotation/DateFormatCheck.java @@ -0,0 +1,47 @@ +package cn.azxo.framework.common.annotation; + +import cn.azxo.framework.common.annotation.support.DataFormatCheckValidator; +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 日期格式校验 + * + * @author zhaoyong_sh + * @see DateFormatCheck + * @since 2021-04-02 16:02 + */ +@Target({ ElementType.FIELD }) +@Retention(RetentionPolicy.RUNTIME) +@Constraint(validatedBy = DataFormatCheckValidator.class) +@Documented +public @interface DateFormatCheck { + + String value() default ""; + + String message() default "data format is illegal"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) + @Retention(RUNTIME) + @Documented + @interface List { + DateFormatCheck[] value(); + } + +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/annotation/ValidValue.java b/common-common/src/main/java/cn/azxo/framework/common/annotation/ValidValue.java new file mode 100644 index 0000000..63e574c --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/annotation/ValidValue.java @@ -0,0 +1,55 @@ +package cn.azxo.framework.common.annotation; + +import cn.azxo.framework.common.annotation.support.ValidValueValidator; +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * 有效的值 + * + * @author zhaoyong_sh + * @see ValidValue + * @since 2020-10-10 13:56 + */ +@Target({ FIELD}) +@Retention(RUNTIME) +@Constraint(validatedBy = ValidValueValidator.class) +@Documented +public @interface ValidValue { + + /** + * 有效字符串集合类 + * @return + */ + String[] value() default {}; + + /** + * 字符是否可以为空 + * @return + */ + boolean canNull() default false; + + String message() default "invalid value"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) + @Retention(RUNTIME) + @Documented + @interface List { + ValidValue[] value(); + } + +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/annotation/support/DataFormatCheckValidator.java b/common-common/src/main/java/cn/azxo/framework/common/annotation/support/DataFormatCheckValidator.java new file mode 100644 index 0000000..098d200 --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/annotation/support/DataFormatCheckValidator.java @@ -0,0 +1,36 @@ +package cn.azxo.framework.common.annotation.support; + +import cn.azxo.framework.common.annotation.DateFormatCheck; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +/** + * DateFormatCheck 校验注解实现类 + * + * @author zhaoyong_sh + * @see DataFormatCheckValidator + * @since 2021-04-02 16:04 + */ +public class DataFormatCheckValidator implements ConstraintValidator { + + private String pattern; + + @Override + public void initialize(DateFormatCheck constraintAnnotation) { + pattern = constraintAnnotation.value(); + } + + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + SimpleDateFormat format = new SimpleDateFormat(pattern); + try { + format.parse(value); + return true; + } catch (ParseException e) { + return false; + } + } +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/annotation/support/ValidValueValidator.java b/common-common/src/main/java/cn/azxo/framework/common/annotation/support/ValidValueValidator.java new file mode 100644 index 0000000..9f9ff76 --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/annotation/support/ValidValueValidator.java @@ -0,0 +1,49 @@ +package cn.azxo.framework.common.annotation.support; + +import cn.azxo.framework.common.utils.ArrayUtils; +import cn.azxo.framework.common.utils.StringUtils; +import cn.azxo.framework.common.annotation.ValidValue; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.util.Objects; + +/** + * ValidValue 注解实现类 + * + * @author zhaoyong_sh + * @see ValidValueValidator + * @since 2020-10-10 13:57 + */ +public class ValidValueValidator implements ConstraintValidator { + + private String[] validValues; + + private boolean canNull; + + @Override + public void initialize(ValidValue constraintAnnotation) { + validValues = constraintAnnotation.value(); + canNull = constraintAnnotation.canNull(); + } + + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + if(StringUtils.isBlank(value)){ + if(canNull) { + return true; + } else { + return false; + } + } + if(ArrayUtils.isEmpty(validValues)){ + return false; + } + for (String validValue : validValues) { + if(Objects.equals(validValue, value)){ + return true; + } + } + return false; + } +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/model/CommonResponse.java b/common-common/src/main/java/cn/azxo/framework/common/model/CommonResponse.java new file mode 100644 index 0000000..2fd0c79 --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/model/CommonResponse.java @@ -0,0 +1,43 @@ +package cn.azxo.framework.common.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 通用响应对象 + * + * @author zhaoyong_sh + * @see CommonResponse + * @since 2021-03-29 14:25 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CommonResponse { + + private String code; + + private String msg; + + private T data; + + public static CommonResponse success(Object data) { + return new CommonResponse("200", "success", null); + } + + public static CommonResponse success(String code, String message, Object data) { + return new CommonResponse(code, message, data); + } + + public static CommonResponse error(String code, String message){ + return new CommonResponse(code, message, null); + } + + public static CommonResponse error(String code, String message, Object data){ + return new CommonResponse(code, message, data); + } + +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/utils/ArrayUtils.java b/common-common/src/main/java/cn/azxo/framework/common/utils/ArrayUtils.java new file mode 100644 index 0000000..e43e2dd --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/utils/ArrayUtils.java @@ -0,0 +1,54 @@ +package cn.azxo.framework.common.utils; + +import java.lang.reflect.Array; + +/** + * 数组工具类 + * + * @author zhaoyong_sh + * @see ArrayUtils + * @since 2021-04-12 15:31 + */ +public abstract class ArrayUtils { + + // ---------------------------------------------------------------------- + /** + *

Checks if an array of Objects is empty or {@code null}. + * + * @param array the array to test + * @return {@code true} if the array is empty or {@code null} + * @since 2.1 + */ + public static boolean isEmpty(final Object[] array) { + return getLength(array) == 0; + } + + //----------------------------------------------------------------------- + /** + *

Returns the length of the specified array. + * This method can deal with {@code Object} arrays and with primitive arrays. + * + *

If the input array is {@code null}, {@code 0} is returned. + * + *

+     * ArrayUtils.getLength(null)            = 0
+     * ArrayUtils.getLength([])              = 0
+     * ArrayUtils.getLength([null])          = 1
+     * ArrayUtils.getLength([true, false])   = 2
+     * ArrayUtils.getLength([1, 2, 3])       = 3
+     * ArrayUtils.getLength(["a", "b", "c"]) = 3
+     * 
+ * + * @param array the array to retrieve the length from, may be null + * @return The length of the array, or {@code 0} if the array is {@code null} + * @throws IllegalArgumentException if the object argument is not an array. + * @since 2.1 + */ + public static int getLength(final Object array) { + if (array == null) { + return 0; + } + return Array.getLength(array); + } + +} diff --git a/common-common/src/main/java/cn/azxo/framework/common/utils/StringUtils.java b/common-common/src/main/java/cn/azxo/framework/common/utils/StringUtils.java new file mode 100644 index 0000000..fae822b --- /dev/null +++ b/common-common/src/main/java/cn/azxo/framework/common/utils/StringUtils.java @@ -0,0 +1,41 @@ +package cn.azxo.framework.common.utils; + +/** + * 字符串工具类 + * + * @author zhaoyong_sh + * @see StringUtils + * @since 2021-04-12 15:29 + */ +public abstract class StringUtils { + + /** + *

Checks if a CharSequence is whitespace, empty ("") or null.

+ * + *
+     * StringUtils.isBlank(null)      = true
+     * StringUtils.isBlank("")        = true
+     * StringUtils.isBlank(" ")       = true
+     * StringUtils.isBlank("bob")     = false
+     * StringUtils.isBlank("  bob  ") = false
+     * 
+ * + * @param cs the CharSequence to check, may be null + * @return {@code true} if the CharSequence is null, empty or whitespace + * @since 2.0 + * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence) + */ + public static boolean isBlank(final CharSequence cs) { + int strLen; + if (cs == null || (strLen = cs.length()) == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (Character.isWhitespace(cs.charAt(i)) == false) { + return false; + } + } + return true; + } + +} diff --git a/pom.xml b/pom.xml index a1012e0..c6e4cba 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,11 @@ 1.7.3 1.0 + 1.7.5 + 1.18.18 + 4.3.19.RELEASE + 2.0.0.Final + 6.0.16.Final @@ -44,27 +49,51 @@ aopalliance ${aopalliance.version} + + javax.validation + validation-api + ${javax.validation.version} + + + org.hibernate + hibernate-validator + ${hibernate.validator.version} + org.slf4j slf4j-api - 1.7.5 + ${slf4j-api.version} org.springframework spring-jdbc - 4.3.19.RELEASE + ${spring.framework.version} org.springframework spring-context - 4.3.19.RELEASE + ${spring.framework.version} org.projectlombok lombok - 1.18.18 + ${lombok.version} + + + + rdc-releases + Nexus Release Repository + https://packages.aliyun.com/maven/repository/2005773-release-XI7cl5/ + + + rdc-snapshots + Nexus Snapshot Repository + https://packages.aliyun.com/maven/repository/2005773-snapshot-V5Gjdf/ + + + \ No newline at end of file diff --git a/smart-datasource/pom.xml b/smart-datasource/pom.xml index 25caa66..62f66a1 100644 --- a/smart-datasource/pom.xml +++ b/smart-datasource/pom.xml @@ -40,4 +40,18 @@ + + + + rdc-releases + Nexus Release Repository + https://packages.aliyun.com/maven/repository/2005773-release-XI7cl5/ + + + rdc-snapshots + Nexus Snapshot Repository + https://packages.aliyun.com/maven/repository/2005773-snapshot-V5Gjdf/ + + +