目录
一 、登录功能开发
1.1 需求分析
1.1.1 查看原型
1.1.2 查看效果图
1.1.3 查看登录请求信息(有接口文档最佳啊)
1.1.4 数据模型 (employee表)
1.2 代码开发
1.2.1 创建实体类Employee与employee库表进行映射
1.2.2 创建controller、mapper、service、service.impl包
1.2.2.1 创建 EmployeeMapper接口
1.2.2.2 创建 EmployeeServcie接口
1.2.2.3 创建 EmployeeServcieImpl实现类
1.2.2.4 创建 EmployeeController类
1.2.3 导入返回结果类R
1.2.4 EmployeeController
1.3 功能测试
1.3.1 启动服务
1.3.2 登录测试
二、退出功能开发
2.1 需求分析
2.2 代码开发
2.2.1 EmployeeController
2.3 功能测试
2.3.1 启动服务
2.3.2 退出测试
前言:实现管理系统的登录功能开发,包含需求分析、代码开发、功能测试
一 、登录功能开发
1.1 需求分析
1.1.1 查看原型
略
1.1.2 查看效果图
启动服务
http://localhost:8080/backend/page/login/login.html
1.1.3 查看登录请求信息(有接口文档最佳啊)
1.1.4 数据模型 (employee表)
1.2 代码开发
1.2.1 创建实体类Employee与employee库表进行映射
package com.runa.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String name;
private String password;
private String phone;
private String sex;
private String idNumber;
private Integer status;
private LocalDateTime createTime;
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
}
1.2.2 创建controller、mapper、service、service.impl包
1.2.2.1 创建 EmployeeMapper接口
package com.runa.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.runa.reggie.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
1.2.2.2 创建 EmployeeServcie接口
package com.runa.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.IService;
import com.runa.reggie.entity.Employee;
public interface EmployeeServcie extends IService<Employee> {
}
1.2.2.3 创建 EmployeeServcieImpl实现类
package com.runa.reggie.service.impl.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runa.reggie.entity.Employee;
import com.runa.reggie.mapper.EmployeeMapper;
import com.runa.reggie.service.impl.EmployeeServcie;
import org.springframework.stereotype.Service;
@Service
public class EmployeeServcieImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeServcie {
}
1.2.2.4 创建 EmployeeController类
package com.runa.reggie.controller;
import com.runa.reggie.service.impl.EmployeeServcie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeServcie employeeServcie;
}
1.2.3 导入返回结果类R
package com.runa.reggie.common;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
/**
* 通用返回结果,服务端响应的数据最终都会封装成此对象
* @param <T>
*/
@Data
public class R<T> {
private Integer code; //编码:1成功,0和其它数字为失败
private String msg; //错误信息
private T data; //数据
private Map map = new HashMap(); //动态数据
public static <T> R<T> success(T object) {
R<T> r = new R<T>();
r.data = object;
r.code = 1;
return r;
}
public static <T> R<T> error(String msg) {
R r = new R();
r.msg = msg;
r.code = 0;
return r;
}
public R<T> add(String key, Object value) {
this.map.put(key, value);
return this;
}
}
1.2.4 EmployeeController
package com.runa.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.runa.reggie.common.R;
import com.runa.reggie.entity.Employee;
import com.runa.reggie.service.impl.EmployeeServcie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeServcie employeeServcie;
/**
* 员工登录
* @param request
* @param employee
* @return
*/
//HttpServletRequest request 是为了将登录对象塞入session
@PostMapping("/login")
public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
//1、将页面提交的密码password进行md5加密处理
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
//2、根据页面提交的用户名username查询数据库
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername,employee.getUsername());
Employee emp = employeeServcie.getOne(queryWrapper);
//3、如果没有查询到则返回登录失败结果
if(emp == null){
return R.error("登录失败");
}
//4、密码比对,如果不一致则返回登录失败结果
if(!emp.getPassword().equals(password)){
return R.error("登录失败");
}
//5、查看员工状态,如果为已禁用状态,则返回员工已禁用结果
if(emp.getStatus() == 0){
return R.error("账号已禁用");
}
//6、登录成功,将员工id存入Session并返回登录成功结果
request.getSession().setAttribute("employee",emp.getId());
return R.success(emp);
}
}
1.3 功能测试
1.3.1 启动服务
访问
http://localhost:8080/backend/page/login/login.html
1.3.2 登录测试
二、退出功能开发
2.1 需求分析
2.2 代码开发
2.2.1 EmployeeController
package com.runa.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.runa.reggie.common.R;
import com.runa.reggie.entity.Employee;
import com.runa.reggie.service.impl.EmployeeServcie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeServcie employeeServcie;
/**
* 员工登录
* @param request
* @param employee
* @return
*/
//HttpServletRequest request 是为了将登录对象塞入session
@PostMapping("/login")
public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
//1、将页面提交的密码password进行md5加密处理
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
//2、根据页面提交的用户名username查询数据库
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername,employee.getUsername());
Employee emp = employeeServcie.getOne(queryWrapper);
//3、如果没有查询到则返回登录失败结果
if(emp == null){
return R.error("登录失败");
}
//4、密码比对,如果不一致则返回登录失败结果
if(!emp.getPassword().equals(password)){
return R.error("登录失败");
}
//5、查看员工状态,如果为已禁用状态,则返回员工已禁用结果
if(emp.getStatus() == 0){
return R.error("账号已禁用");
}
//6、登录成功,将员工id存入Session并返回登录成功结果
request.getSession().setAttribute("employee",emp.getId());
return R.success(emp);
}
@PostMapping("/logout")
public R<String> logout(HttpServletRequest request){
// 清理Session 中保存的当前员工的id
request.getSession().removeAttribute("emplyee");
return R.success("退出成功!");
}
}
2.3 功能测试
2.3.1 启动服务
访问
http://localhost:8080/backend/page/login/login.html
2.3.2 退出测试