辅助mongodb.redis一些常见常量,枚举,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 {
}

你可能感兴趣的:(自定义类,redis,mongodb,java)