contants
mycontants.java
package com.shrimpking.constants;
import java.nio.charset.StandardCharsets;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/11/4 22:37
* 自定义常量类
*/
public interface MyConstants
{
/**
* token的key名称
*/
String USER_TOKEN_KEY = "user_token";
/**
*
*/
String UTF_8_NAME = StandardCharsets.UTF_8.name();
/**
* 默认,当前页
*/
Long DEFAULT_CURRENT_PAGE = 1L;
/**
* 默认,页条数
*/
Long DEFAULT_PAGE_SIZE = 10L;
}
context
UserContext.java
package com.shrimpking.context;
import com.shrimpking.dto.UserDTO;
import com.shrimpking.pojo.User;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/11/5 10:03
*/
public class UserContext
{
private static ThreadLocal<UserDTO> threadLocal = new ThreadLocal<>();
/**
* 保存至线程中
* @param userDTO
*/
public static void set(UserDTO userDTO){
threadLocal.set(userDTO);
}
/**
* 从线程中获取
* @return
*/
public static UserDTO get(){
return threadLocal.get();
}
/**
* 从线程中移除
*/
public static void remove(){
threadLocal.remove();
}
}
dto
userdto.java
package com.shrimpking.dto;
import lombok.Data;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/11/4 17:14
* 定义了token中包含的哪些信息
*/
@Data
public class UserDTO
{
private Long id;
private String userName;
}
enums
codeEnum.java
package com.shrimpking.enums;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/11/4 18:18
* 响应码枚举类
*/
public enum CodeEnum
{
/**
* 成功
*/
SUCCESS(200,"成功"),
/**
* 失败
*/
ERROR(500,"失败"),
/**
* 其他失败
*/
PARAM_ERROR(510,"参数不正确"),
AUTH_ERROR(520,"权限不足"),
DB_ERROR(530,"数据库异常"),
DUPLICATE_KEY_ERROR(550,"唯一键约束重复"),
BIZ_ERROR(570,"业务异常"),
OTHER_ERROR(599,"其他异常"),
;
CodeEnum(Integer code,String msg){
this.code = code;
this.msg = msg;
}
private Integer code;
private String msg;
public Integer getCode()
{
return code;
}
public void setCode(Integer code)
{
this.code = code;
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
}