Home > Java > javaTutorial > How springboot implements the user name search function

How springboot implements the user name search function

王林
Release: 2023-05-13 08:58:05
forward
1250 people have browsed it

In order to implement the function of querying users based on user names, we need to write several classes in the spring boot framework:

1. UserEnetity class

It is an entity class based on the database table. Used to encapsulate the user's basic information. In this table, user-related attributes need to be defined and getter and setter methods provided.

public class UserEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
 
    private String name;
 
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    private String username;
 
 
    private int role;
    private String avatar;
    private int status;
    private String password;
 
 
    public UserEntity(int id, String name, int role, String avatar, int status, String password,String username,String token) {
        this.id = id;
        this.name = name;
        this.role = role;
        this.avatar = avatar;
        this.status = status;
        this.password = password;
        this.username = username;
    }
 
    public UserEntity() {
 
    }
 
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
 
 
    public int getRole() {
        return role;
    }
 
    public void setRole(int role) {
        this.role = role;
    }
 
    public String getAvatar() {
        return avatar;
    }
 
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
 
    public int getStatus() {
        return status;
    }
 
    public void setStatus(int status) {
        this.status = status;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
}
Copy after login

2. UserMapper class

It is an interface based on MyBatis and is used to define methods for database operations, such as querying users, adding users, updating users, etc.

@Mapper
public interface UserMapper extends BaseMapper {
    List<UserEntity> findAllUser();
 
    UserDTO getUserByUsername(String subject);
}
Copy after login

3. UserService class

It is the business logic layer, which is mainly responsible for coordinating the relationship between the UserMapper class and the User Entity class and implementing business logic processing. In this class, you need to define a method to query users based on their username, and call the method of the UserMapper class to implement the query operation.

   //用户名查询用户
    public ResultResponse findByUsername(String name) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        UserEntity user = (UserEntity) userMapper.selectOne(queryWrapper);
        if (user != null) {
            return ResultResponse.returnToken(ResultResponse.success("查询成功", user));
        } else {
            return ResultResponse.returnToken(ResultResponse.fail("查询失败,该用户不存在"));
        }
    }
Copy after login

4. UserController class

It is the controller layer, used to process user requests and return corresponding results. In this class, you need to define request processing methods, such as methods to query users based on user names and return data in JSON format.

   //根据用户名查询用户--查询结果分每页十条显示
    @GetMapping("user/findByUsername")
    public ResultResponse<IPage<UserEntity>> findByUsername(@RequestParam String username,
                                                            @RequestParam Integer pageNumber,
                                                            @RequestParam Integer pageSize) {
        Page<UserEntity> userPage = new Page<>(pageNumber, pageSize);
//        IPage<UserEntity> user = (IPage<UserEntity>) userService.selectByUsername(userPage, username);
        UserEntity user = userService.selectByUsername(userPage,username);
 
        if (user == null) {
            return ResultResponse.error("没有找到匹配的用户");
        }
        return ResultResponse.ok("查询成功",user);
    }
Copy after login

In short, these four classes need to be written, and the relationship between them also needs to be designed. Among them, the User Entity class and UserMapper class have a close relationship with the database. The UserService class is the core of the business logic layer, and the UserController class is the core of the controller layer, responsible for processing user requests and returning response results.

5. postman test results

How springboot implements the user name search function

The above is the detailed content of How springboot implements the user name search function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template