登录功能的开发
- 前端
- 1、创建实体类Employee和employee表进行映射,可以直接导入资料中提供的实体类
-
- 1.1、字段名称对应上,有下划线的使用驼峰对应,因为在配置文件中进行了配置
- [1.2、employee 文件](#1.2、employee 文件)
- 2、创建Controller、Service、Mapper
- 3、测试登录接口
前端
本篇文章讲解springboot + mybatis plus来进行实现登录功能
1、创建实体类Employee和employee表进行映射,可以直接导入资料中提供的实体类
1.1、字段名称对应上,有下划线的使用驼峰对应,因为在配置文件中进行了配置
1.2、employee 文件
java
package com.example.ruiji_demo.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import org.springframework.util.ReflectionUtils;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
* @author jitwxs
* @date 2024年03月11日 22:59
*/
@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;
}
2、创建Controller、Service、Mapper
2.1、Mapper文件
java
package com.example.ruiji_demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ruiji_demo.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}
2.2、定义实现接口
employeeServiece 接口文件
java
package com.example.ruiji_demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.ruiji_demo.entity.Employee;
public interface EmployeeService extends IService<Employee> {
}
impl文件
java
package com.example.ruiji_demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ruiji_demo.entity.Employee;
import com.example.ruiji_demo.mapper.EmployeeMapper;
import com.example.ruiji_demo.service.EmployeeService;
import org.springframework.stereotype.Service;
/**
* @author jitwxs
* @date 2024年03月11日 23:37
*/
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
2.3、创建通用方法,封装服务器端响应数据
java
package com.example.ruiji_demo.common;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
/**
* @author jitwxs
* @date 2024年03月11日 23:46
*/
@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.code = 0;
return r;
}
public R<T>add(String key,Object value){
this.map.put(key, value);
return this;
}
}
2.3、Controller文件
写方法文件
java
package com.example.ruiji_demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.example.ruiji_demo.common.R;
import com.example.ruiji_demo.entity.Employee;
import com.example.ruiji_demo.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
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;
/**
* @author jitwxs
* @date 2024年03月11日 23:40
*/
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
/**
* 员工登录
* @param request
* @param employee
* @return
*/
@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 = employeeService.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);
}
}