The first chapter we talk about project creation, structure analysis, mysql configuration, login function, here directly said the implementation of the registration method

First look at the login controller code

    @PostMapping("login")
    public BaseResult login(@RequestParam(value = "user", defaultValue = "") String user,
                            @RequestParam(value = "pass", defaultValue = "") String pass) {
        if (user.equals("")) return new BaseResult(400."Account must be passed!"."");
        if (pass.equals("")) return new BaseResult(400."Password must be passed!"."");
        User user1 = mapper.login(user, pass);
        if (user1 == null) return new BaseResult(500."Incorrect account password!"."");
        user1.setPass("");
        return new BaseResult(200."", user1);
    }
Copy the code

We found that it was too complicated to write one parameter and accept one variable. If the registry needed to pass more than 10 or even 20 parameters, the controller would seem very stupid. Therefore, we introduced a new method of accepting parameters to simplify the above code and realize the registration function

Write a mapeer file to implement registration, because still operate user table, so still operate in UserMapper

    @Insert("insert into user (user, pass) values (#{user}, #{pass})")
    int register(User bean);
Copy the code

Add, modify, delete operations, return all int, return the number of affected rows, if =0, the operation failed

Write the Controller file to register, and since you’re still operating on the User table, you’re still operating on the UserController

@PostMapping
    public BaseResult register(User user) {
        if (user == null || user.getUser() == null || user.getPass() == null) return new BaseResult(400."Parameters are not correct"."");

        if(mapper.findByUser(user.getUser()) ! =null) return new BaseResult(500."Account has been registered"."");// Since the login account is unique, check whether it exists. If so, block the registration and give an error message

        if (mapper.register(user) == 0) return new BaseResult(500."Registration failed"."");
        return new BaseResult(200.""."Registration successful!");
    }
Copy the code

As you can see, my method receives a user object, but does not add @requestParam (value = “user”, defaultValue = “”) annotation, which is the new method to receive parameters

When the foreground uses form-data mode to pass parameters, the background can use the entity class to receive, automatically transfer the parameters passed in the foreground to the entity class, do not receive the redundant parameters, this method can simplify the problem of too many parameters, we can also use json string to receive

@PostMapping("login")
@PostMapping
Copy the code

But you find that my controller now has two methods, one login and one registration. Why is my registration less than login?

The reason is that when the foreground accesses a controller, if there is only one request method in the controller, for example, now the foreground requests the POST address as:http://localhost:8080/user

The backend will automatically find all post methods in the UserController when it receives a request. Since we didn’t specify which method to request, @postmapping is requested by default instead of @postmapping (“login”).

We know from the above that in a controller, there is no need to specify an alias when the request method has only one method

All right, let’s see what happens

Implementation effect demonstration

At the end of this chapter, complete controller and Mapper codes are posted below

Complete code


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.td0f7.cn.springboot.springboot.base.BaseResult;
import www.td0f7.cn.springboot.springboot.entity.User;
import www.td0f7.cn.springboot.springboot.mapper.UserMapper;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired(required = false)
    private UserMapper mapper;

    @PostMapping("login")
    public BaseResult login(@RequestParam(value = "user", defaultValue = "") String user,
                            @RequestParam(value = "pass", defaultValue = "") String pass) {
        if (user.equals("")) return new BaseResult(400."Account must be passed!"."");
        if (pass.equals("")) return new BaseResult(400."Password must be passed!"."");
        User user1 = mapper.login(user, pass);
        if (user1 == null) return new BaseResult(500."Incorrect account password!"."");
        user1.setPass("");
        return new BaseResult(200."", user1);
    }


    @PostMapping
    public BaseResult register(User user) {
        if (user == null || user.getUser() == null || user.getPass() == null) return new BaseResult(400."Parameters are not correct"."");

        if(mapper.findByUser(user.getUser()) ! =null) return new BaseResult(500."Account has been registered"."");

        if (mapper.register(user) == 0) return new BaseResult(500."Registration failed"."");
        return new BaseResult(200.""."Registration successful!"); }}Copy the code

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import www.td0f7.cn.springboot.springboot.entity.User;

@Mapper
public interface UserMapper {

    @Select("select * from user where user = #{user} and pass = #{pass}")
    User login(String user, String pass);

    @Select("select id from user where user = #{user}")
    User findByUser(String user);

    @Insert("insert into user (user, pass) values (#{user}, #{pass})")
    int register(User bean);
}
Copy the code