- package com.app.studypro.utils;
-
- /**
- * 获取用户信息的工具类
- * 采用ThreadLocal存储用户的id信息
- * @author Administrator
- */
- public class UserUtil {
- /**
- * ThreadLocal存储用户id信息
- */
- private static ThreadLocal
threadLocal=new ThreadLocal<>(); -
- /**
- * 获取当前用户的id
- * @return 返回long类型的用户id
- */
- public static Long getCurrentUserId(){
- return threadLocal.get();
- }
-
- /**
- * 设置用户id的信息
- * @param userId 用户id
- */
- public static void setCurrentUserId(Long userId){
- threadLocal.set(userId);
- }
-
- /**
- * 移除ThreadLocal中的当前线程存储的内容信息。请在使用完之后将其移除,避免存在内存泄漏
- */
- public static void removeCurrentUserId(){
- threadLocal.remove();
- }
-
- }
