存放我们的实体类,与数据库中的属性值基本保持一致。
// Employee
@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;
}
对数据库进行数据持久化操作,他的方法语句是直接针对数据库操作的
// mapper
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {}
存放业务逻辑处理,也是一些关于数据库处理的操作
// Service
@Service
public interface EmployeeService extends IService<Employee> {}
// ServiceImpl
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>
implements EmployeeService {
}
controller通过接收前端传过来的参数进行业务操作
// Controller
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping("/login")
public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
// 1. md5加密
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
// 2. 查询数据库,通过名称查询用户
LambdaQueryWrapper<Employee> queryWapper = new LambdaQueryWrapper<>();
queryWapper.eq(Employee::getUsername, employee.getUsername());
Employee emp = employeeService.getOne(queryWapper);
// 3. 判断是否查到
if(emp == null) {
return R.error("登录失败");
}
// 4. 如果密码不一致
if(!emp.getPassword().equals(password)) {
return R.error("密码不一致");
}
// 5. 查看用户是否被禁用
if(emp.getStatus() == 0) {
return R.error("用户被禁用");
}
// 设置session
request.getSession().setAttribute("employee", emp.getId());
return R.success(emp);
}
@PostMapping("/logout")
public R<String> logout(HttpServletRequest request) {
request.getSession().removeAttribute("employee");
return R.success("退出登录成功");
}
}