Java back-end development function module ideas


foreword

For students who are learning Java back-end development, there must be an overall clear process for the development process and ideas of Java back-end functional modules. In order to ensure a smoother development process. This article just takes the development of a simple login module as an example to explain how to write the code of the functional module on the premise that the front end has been written. And how to write and write ideas to explain. Make the module development in the future more smooth.


1. Find interface and parameter information

There is a special interface document for formal development here, but there is no interface document for students who practice their own projects. We have to learn to find the corresponding interface and the returned parameters through the code without providing the interface document.
The interface document is shown in the figure below:
insert image description here
given the interface document, we don’t need to pay attention to how the front-end code is written. We only need to write the mapper, service, and controller layer codes according to the document, but most of them are not company codes, just learn by ourselves There is no such interface document for the project. We need to understand the front-end code, and find the corresponding access path, request parameters, and returned results from the front-end code.
Let's take the login module as an example to do it.

1.1 Find the access path

First start the project and open the login page on the front end.
Click f12 to check, click the login button, find the network -> all -> request URL.
As shown in the figure below: We can see that the requested URL is the content to be written in the Mapping (“/”) we want to write.
insert image description here

1.2 Parameters and returned result information

The parameters of the request are the username and password, but we need to look at the returned results as shown in the figure below: you can
insert image description here
see the code status code, data data, map, and msg prompts. These are pre-written classes in the result class returned by the server. The code looks like this:

import lombok.Data;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回结果的封装类
 * @param <T>
 */
@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.msg = msg;
        r.code = 0;
        return r;
    }

    public R<T> add(String key, Object value) {
    
    
        this.map.put(key, value);
        return this;
    }

}

It can be seen from the code that when we write a function module, the return R. method is fine, and it is a general return data class.

1.3 Write function module functions

  • Through the above analysis, we can write the basic structure of the module, and the returned result is R<generic is the employee information we return>, the first parameter is the request used when obtaining the session, if the login is successful, the user will be The information is stored in the session, and the second parameter is the json data transmitted by the employee at the foreground and encapsulated into an employee object. The @RequestBody annotation in the following code is required for json conversion
@PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
    
    
        
        //逻辑处理
        return R.success(emp);
    }

2. Code Design Ideas

After writing the above content, it is necessary to start writing the logic inside the function and the idea of ​​​​the implementation. Before writing, we need to list the logic to judge whether the login is successful or failed. The procedure is divided into the following 6 steps.
//1. Encrypt the password password submitted by the page with md5
//2. Query the database according to the user name username submitted by the page
//3. If no query is found, the login failure result will be returned
//4. Password comparison, if inconsistent Then return the login failure result
//5. Check the employee status, if it is disabled, return the employee is disabled
//6. Login is successful, store the employee id in the Session and return the login success result
Then we write the code according to the idea:

@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);
    }

In this way, the design of the login function module is completed.

3. Summary

To sum up, the process of developing the module is divided into three parts.
One is to find the corresponding content of the interface document, look at the return value, the incoming parameters, and the path of the request.
The second is the writing of design ideas, writing the design ideas into comments, representing the code to be written in each step.
The third is to start writing the code for each step.
The content of this time is suitable for students who have finished learning SpringBoot and MP and are working on projects to check and understand. For the back-end students, it is not required to be able to write the front-end code, but they must understand the meaning of the code, so that when they find the jump of these codes, the connection of modules will be smoother, and it will be useful for development.
Of course, it will be easier to develop if there are interface documents. If you are in the learning stage, it is recommended that the back-end students should also learn the knowledge points of the front-end.


Guess you like

Origin blog.csdn.net/weixin_52258054/article/details/128976264
Recommended