補助mongodb.redisのいくつかのよくある定数,列挙,JSOnResult方法,注釈クラスの簡単な設計
16223 ワード
列挙クラス設計
定数設計
JSOnResultベースの例外リターン設計
カスタム注記設計
package cn.wolfcode.wolf2w.redis.util;
import cn.wolfcode.wolf2w.util.Consts;
import lombok.Getter;
@Getter
public enum RedisKeys {
VERIFY_CODE("verify_code", Consts.VERIFY_CODE_VAI_TIME*60L),
USER_LOGIN_TOKEN("user_login_token" ,Consts.USER_INFO_TOKEN_VAI_TIME*60L);
private String prefix ;
private Long time ;
private RedisKeys(String prefix,Long time){
this.prefix=prefix;
this.time=time;
}
public String join(String... values){
StringBuilder sb = new StringBuilder();
sb.append(this.prefix);
for (String value : values) {
sb.append(":").append(value);
}
return sb.toString();
}
}
定数設計
package cn.wolfcode.wolf2w.util;
/**
*
*/
public class Consts {
//
public static final int VERIFY_CODE_VAI_TIME = 5; //
//token
public static final int USER_INFO_TOKEN_VAI_TIME = 30; //
}
JSOnResultベースの例外リターン設計
package cn.wolfcode.wolf2w.util;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Setter
@Getter
@NoArgsConstructor
public class JsonResult<T> {
public static final int CODE_SUCCESS = 200;
public static final String MSG_SUCCESS = " ";
public static final int CODE_NOLOGIN = 401;
public static final String MSG_NOLOGIN = " ";
public static final int CODE_ERROR = 500;
public static final String MSG_ERROR = " , ";
public static final int CODE_ERROR_PARAM = 501; //
private int code; // , true false
private String msg;
private T data; // ,
public JsonResult(int code, String msg, T data){
this.code = code;
this.msg = msg;
this.data = data;
}
public static <T> JsonResult success(T data){
return new JsonResult(CODE_SUCCESS, MSG_SUCCESS, data);
}
public static JsonResult success(){
return new JsonResult(CODE_SUCCESS, MSG_SUCCESS, null);
}
public static <T> JsonResult error(int code, String msg, T data){
return new JsonResult(code, msg, data);
}
public static JsonResult defaultError(){
return new JsonResult(CODE_ERROR, MSG_ERROR, null);
}
public static JsonResult noLogin() {
return new JsonResult(CODE_NOLOGIN, MSG_NOLOGIN, null);
}
}
カスタム注記設計
package cn.wolfcode.wolf2w.annontation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequireLogin {
}