当前位置:网站首页>Ruiji takeout - background login function development
Ruiji takeout - background login function development
2022-07-28 22:19:00 【ajjjjjjjjjtty】
Development of background login function
Demand analysis
Code development
A functional test
One 、 Demand analysis
Sign in
The login page shows ( Reggie takeout management end )
Two 、 Code development ( establish controller,service,mapper, Entity class , Import the general return result class )
Create modules , Perfect function
Screenshot of function module :

1. Entity class entity:
The demand of this part is that background employees log in , So the entity class here is Employee
The following is the code of the entity class
/**
* Employee entity class
*/
@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;// identity zheng number Hump nomenclature
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.Mapper
Alias : dao layer
purpose : Data persistence operation on the database , His method statement is directly aimed at database operation , It mainly implements some operations of adding, deleting, modifying and checking , stay mybatis The method in is mainly related to xxx.xml One by one .
But the database in this project uses mybatis-plus, There are many methods of adding, deleting, modifying and checking , Here you only need to inherit BaseMapper that will do
*** stay Mapper It's a bag , establish EmployeeMapper Interface , Inherit BaseMapper, take @mapper Annotations are added to the interface
The code is as follows :
package com.itheima.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {
}3.service layer
purpose : to controller The class of layer provides interface for calling . Generally, it's a method that you write and encapsulate , Make a statement , The concrete realization is serviceImpl in .
Create... Here EmployeeService Interface , And inheritance IService, To create a impl Create under package EmployeeServiceImpl Method implementation interface EmployeeService, And inheritance ServiceImpl class
The code is as follows :
EmployeeService Interface :
package com.itheima.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.itheima.entity.Employee;
public interface EmployeeService extends IService<Employee> {
}EmployeeServiceImpl Method :
package com.itheima.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.Employee;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import org.springframework.stereotype.Service;
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}4. General return result class
stay reggie Create a sub package under the package as common, And then common Create a R class
package com.itheima.reggie.common;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
/**
* General return result , The data responded by the server will eventually be encapsulated into this object
* @param <T>
*/
@Data
public class R<T> {
private Integer code; // code :1 success ,0 And other numbers are failures
private String msg; // error message
private T data; // data
private Map map = new HashMap(); // Dynamic data
// How to login successfully
public static <T> R<T> success(T object) {
R<T> r = new R<T>();
r.data = object;
r.code = 1;
return r;
}
// How to login failed
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;
}
}
5. The control layer is Controller:
purpose : Responsible for business process control of specific modules
call service To control the business process . because service The method in is what we use ,controller By receiving the front end H5 perhaps App The parameters passed in are used for business operation , Then the processing results are returned to the front end .
establish EmployeeController class , to EmployeeController Class add a login Method
- @RequestBody It is mainly used to receive the information passed from the front end to the back end json character string ( Data in the request body )
- HttpServletRequest request effect : If login is successful , Associate employees with id Deposit in session One copy , In this way, you can get a copy of the login user's information at any time

package com.itheima.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
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 {
// By means of automatic assembly service Interface injection
@Autowired
private EmployeeService employeeService;
/*
How employees log in
*/
@PostMapping("/login")
public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
// 1. Change the password submitted on the page MD5 Encryption processing Pass in the password and turn it into getBytes Array
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
// 2. According to the user name submitted on the page username Query the database
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername,employee.getUsername());
Employee emp = employeeService.getOne(queryWrapper);
// 3. If there is no query, the login failure result will be returned
if(emp == null){
return R.error(" Login failed ");
}
// 4. Password comparison , If not, the login result will be returned
if(!emp.getPassword().equals(password)){
return R.error(" Login failed ");
}
// 5. View employee status , If it is disabled , The disabled employee result is returned
if(emp.getStatus() == 0){
return R.error(" Account has been disabled ");
}
// 6. Login successful , Staff id Deposit in session And return the login success result
request.getSession().setAttribute("employee", emp.getId());
return R.success(emp);
}
}
边栏推荐
- 阿里云CDN实践
- From Web3 to web2.5, is it backward or another way?
- Principle of object. Prototype. ToString. Call()
- 如何在 Web3中建立一个去中心化社区
- Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
- HYDAC overflow valve db08a-01-c-n-500v
- LVS+KeepAlived高可用部署实战应用
- SQL注入 Less34(POST型宽字节注入+布尔盲注)
- 成立不到一年!MIT衍生量子计算公司完成900万美元融资
- 第三方软件测试机构提供哪些测试服务?软件测试报告收费标准
猜你喜欢

HCIP(8)

Kubevera plug-in addons download address

Getting started with Oracle

Practice and exploration of overseas site Seata of ant group

Hcip seventh experiment

PCB材料简单介绍

Jmeter 安装第三方插件 Plugins Manager

DHCP and PPPoE protocols and packet capture analysis

腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实

Desai wisdom number - line chart (stacking area chart): ranking of deposits of different occupational groups in the proportion of monthly income in 2022
随机推荐
II. Explanation of the sequence and deserialization mechanism of redistemplate
array_ diff_ The method of not comparing array values when Assoc element is an array
Bugku, Web: all filtered
C语言编程规范学习笔记和总结
HCIP(12)
Practice and exploration of overseas site Seata of ant group
openresty 请求鉴权
数据可视化新闻,不一样的新闻报道形式
Esp8266 Arduino programming example - deep sleep and wake up
Byte side: can TCP and UDP use the same port?
行内元素和块级元素有什么区别?语义化作用
Differences of display values
静态路由和缺省路由实验
AimBetter洞察您的数据库,DPM 和 APM 解决方案
04.toRef 默认值
How to establish a decentralized community in Web3
腾讯云数据库负责人林晓斌借一亿元炒股?知情人士:金额不实
HCIP(13)
Form validation and cascading drop-down lists (multiple implementations)
HCIP(15)