mirror of
https://gitee.com/likeadmin/likeadmin_java.git
synced 2026-05-20 09:49:06 +08:00
Merge branch 'feature/generate' into develop
This commit is contained in:
@@ -17,6 +17,9 @@ public class GlobalConfig {
|
||||
// 系统加密字符
|
||||
public static String secret = "UVTIyzCy";
|
||||
|
||||
// Mysql表前缀
|
||||
public static String tablePrefix = "ls_";
|
||||
|
||||
// Redis键前缀
|
||||
public static String redisPrefix = "Like:";
|
||||
|
||||
|
||||
@@ -474,4 +474,65 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化文本, {} 表示占位符<br>
|
||||
* 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
|
||||
* 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
|
||||
* 例:<br>
|
||||
* 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
|
||||
* 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
|
||||
* 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
|
||||
*
|
||||
* @param strPattern 文本模板,被替换的部分用 {} 表示
|
||||
* @param argArray 参数值
|
||||
* @return 格式化后的文本
|
||||
*/
|
||||
public static String format(String strPattern, Object... argArray) {
|
||||
String EMPTY_JSON = "{}";
|
||||
char C_BACKSLASH = '\\';
|
||||
char C_DELIM_START = '{';
|
||||
|
||||
if (isEmpty(argArray) || isEmpty(strPattern)) {
|
||||
return strPattern;
|
||||
}
|
||||
|
||||
final int strPatternLength = strPattern.length();
|
||||
StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
|
||||
int handledPosition = 0;
|
||||
int delimIndex;
|
||||
for (int argIndex = 0; argIndex < argArray.length; argIndex++) {
|
||||
delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition);
|
||||
if (delimIndex == -1) {
|
||||
if (handledPosition == 0) {
|
||||
return strPattern;
|
||||
} else {
|
||||
sbuf.append(strPattern, handledPosition, strPatternLength);
|
||||
return sbuf.toString();
|
||||
}
|
||||
} else {
|
||||
if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) {
|
||||
if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) {
|
||||
sbuf.append(strPattern, handledPosition, delimIndex - 1);
|
||||
sbuf.append(argArray[argIndex]);
|
||||
handledPosition = delimIndex + 2;
|
||||
} else {
|
||||
// 占位符被转义
|
||||
argIndex--;
|
||||
sbuf.append(strPattern, handledPosition, delimIndex - 1);
|
||||
sbuf.append(C_DELIM_START);
|
||||
handledPosition = delimIndex + 1;
|
||||
}
|
||||
} else {
|
||||
// 正常占位符
|
||||
sbuf.append(strPattern, handledPosition, delimIndex);
|
||||
sbuf.append(argArray[argIndex]);
|
||||
handledPosition = delimIndex + 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sbuf.append(strPattern, handledPosition, strPattern.length());
|
||||
return sbuf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.hxkj.common.validator;
|
||||
|
||||
import com.hxkj.common.validator.annotation.IntArrayEmpty;
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
|
||||
/**
|
||||
* 验证整数数组是否为空
|
||||
*/
|
||||
public class IntArrayEmptyValidator implements ConstraintValidator<IntArrayEmpty, int[]> {
|
||||
|
||||
@Override
|
||||
public void initialize(IntArrayEmpty constraintAnnotation) {
|
||||
ConstraintValidator.super.initialize(constraintAnnotation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(int[] value, ConstraintValidatorContext context) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return value.length > 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.hxkj.common.validator.annotation;
|
||||
|
||||
import com.hxkj.common.validator.IntArrayEmptyValidator;
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Documented
|
||||
@Constraint(validatedBy = IntArrayEmptyValidator.class)
|
||||
@Target({ ElementType.PARAMETER,ElementType.FIELD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface IntArrayEmpty {
|
||||
|
||||
String message() default "数组不允许为空";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default { };
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user