常德网站开发,企业网站开发文档,充电宝关键词优化,如何查到网站建设登录功能的开发 前端1、创建实体类Employee和employee表进行映射,可以直接导入资料中提供的实体类1.1、字段名称对应上#xff0c;有下划线的使用驼峰对应#xff0c;因为在配置文件中进行了配置1.2、employee 文件 2、创建Controller、Service、Mapper2.1、Mapper文件2.2、定… 登录功能的开发 前端1、创建实体类Employee和employee表进行映射,可以直接导入资料中提供的实体类1.1、字段名称对应上有下划线的使用驼峰对应因为在配置文件中进行了配置1.2、employee 文件 2、创建Controller、Service、Mapper2.1、Mapper文件2.2、定义实现接口2.3、创建通用方法封装服务器端响应数据2.3、Controller文件 3、测试登录接口 前端
本篇文章讲解springboot mybatis plus来进行实现登录功能
1、创建实体类Employee和employee表进行映射,可以直接导入资料中提供的实体类
1.1、字段名称对应上有下划线的使用驼峰对应因为在配置文件中进行了配置 1.2、employee 文件
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文件 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 BaseMapperEmployee {
}
2.2、定义实现接口 employeeServiece 接口文件
package com.example.ruiji_demo.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.example.ruiji_demo.entity.Employee;public interface EmployeeService extends IServiceEmployee {
}
impl文件
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 ServiceImplEmployeeMapper, Employee implements EmployeeService {
}
2.3、创建通用方法封装服务器端响应数据 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 RT {private Integer code; //编码1成功 0和其他数字失败private String msg; //错误信息private T data; // 数据private Map map new HashMap(); //动态数据public staticTRTsuccess(T object){RT r new RT();r.data object;r.code 1;return r;}public staticTRTerror(String msg){R r new R();r.code 0;return r;}public RTadd(String key,Object value){this.map.put(key, value);return this;}}
2.3、Controller文件
写方法文件
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 {Autowiredprivate EmployeeService employeeService;/*** 员工登录* param request* param employee* return*/PostMapping(/login)public REmployee login(HttpServletRequest request, RequestBody Employee employee){
// 1、将页面提交的密码password进行md5加密String password employee.getPassword();password DigestUtils.md5DigestAsHex(password.getBytes());// 2、根据页面提交的用户名username查询数据库LambdaQueryWrapperEmployee 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);}
}
3、测试登录接口