当前位置:网站首页>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);
    }

}

原网站

版权声明
本文为[ajjjjjjjjjtty]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/196/202207130602186624.html