用户
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
private String userName;
private String password;
private String mobile;
private String email;
private Integer roleId;
}
ThreadLocalUtils
import com.example.recordlog.bean.User;
public class ThreadLocalUtils {
private static ThreadLocal<User> userThreadLocal = new ThreadLocal<>();
public static User getLoginUser() {
return userThreadLocal.get();
}
public static void setLoginUser(User user) {
userThreadLocal.set(user);
}
public static String getLoginUserName() {
User user = getLoginUser();
if (user != null) {
return user.getUserName();
}
return null;
}
public static Integer getLoginRoleId() {
User user = getLoginUser();
if (user != null) {
return user.getRoleId();
}
return null;
}
}
3使用:用户登录后,直接通过获取
User loginUser = ThreadLocalUtils.getLoginUser();