用户管理系统
实用技术:
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、邮箱、是否是超管、状态、创建时间、更新时间