[用户管理系统]

实用技术:

SpringBoot、SpringMVC、MyBatis、拦截器、统一异常处理,统一数据返回、

二、添加公共模块

2.1先创建controller、service、modle、mapper层、实体类

2.2将工具hutool添加到项目中

2.3统一功能处理,添加统一异常处理,添加用户登录拦截器,添加统一返回格式

2.3.1添加统一异常处理


/*
统一异常的拦截处理类
 */
//@ControllerAdvice
@RestControllerAdvice
public class MyExceptionAdvice {

@ExceptionHandler(Exception.class)
    public Object exceptionAdvice(Exception e){
    HashMap<String ,Object> result = new HashMap<>();
    result.put("state",-1);
    result.put("msg","程序出现异常:"+e.getMessage());
    result.put("data","");
    return result;
}
}

2.3.2 添加用户登陆拦截器

1.自定义拦截器

/*
//自定义用户拦截器
 */
@Component
public class LoginIntercept implements HandlerInterceptor  {
    /*
     * true 表示用户已经登陆
     * false 表示未登录,跳转到登录页面
     */
    @Override
    public boolean preHandle(HttpServletRequest request, 
                             HttpServletResponse response, Object      handler) throws Exception {
     HttpSession session = request.getSession(false);
     if(session!=null&&session.getAttribute(ConstVariable.USER_SESSION_KEY)!=null){
        //表示已经登录
        return true;
    }
    //执行到此行表示未登录,则跳转到登录页面
        response.sendRedirect("/login.html");
  return false;
    }
}

2.将自定义拦截器加入到系统配置中,并设置拦截规则

/**
 * 系统配置文件类
 */
@Configuration //
public class AppConfig implements WebMvcConfigurer {
    @Autowired
    private LoginIntercept loginIntercept;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginIntercept).
                addPathPatterns("/**").
                excludePathPatterns("/login").
                excludePathPatterns("/css/**").
                excludePathPatterns("/fonts/**").
                excludePathPatterns("/images/**").
                excludePathPatterns("/js/**").
                excludePathPatterns("/**/login.html");
    }
}

2.3.3 添加统一返回格式

1.标识返回的类为ControllerAdvice
2.实现ResponseBodyAdvice接口

/*
返回统一的数据格式
 */
@ControllerAdvice
public class MyResponseAdvice implements ResponseBodyAdvice {
    //选择是否对返回的数据进行重写
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        HashMap<String,Object> result = new HashMap<>();
        result.put("state",1);
        result.put("msg","");
        result.put("data",body);
        return result;
    }
}

将项目的前端文件放到resource和static下

数据库设计

建表:userinfo
字段:uid、用户名、登录名、密码、性别、年龄、地址、qq、邮箱、是否是超管、状态、创建时间、更新时间
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值