search
HomeJavajavaTutorialHow springboot implements the user name search function

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

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

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("查询失败,该用户不存在"));
        }
    }

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

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Java compilation error: How do package declaration and access permissions change after moving the class file?Java compilation error: How do package declaration and access permissions change after moving the class file?Apr 19, 2025 pm 07:12 PM

Packages and Directories in Java: The logic behind compiler errors In Java development, you often encounter problems with packages and directories. This article will explore Java in depth...

Is JWT suitable for dynamic permission change scenarios?Is JWT suitable for dynamic permission change scenarios?Apr 19, 2025 pm 07:06 PM

JWT and Session Choice: Tradeoffs under Dynamic Permission Changes Many Beginners on JWT and Session...

How to properly configure apple-app-site-association file in pagoda nginx to avoid 404 errors?How to properly configure apple-app-site-association file in pagoda nginx to avoid 404 errors?Apr 19, 2025 pm 07:03 PM

How to correctly configure apple-app-site-association file in Baota nginx? Recently, the company's iOS department sent an apple-app-site-association file and...

What are the differences in the classification and implementation methods of the two consistency consensus algorithms?What are the differences in the classification and implementation methods of the two consistency consensus algorithms?Apr 19, 2025 pm 07:00 PM

How to understand the classification and implementation methods of two consistency consensus algorithms? At the protocol level, there has been no new members in the selection of consistency algorithms for many years. ...

What is the difference between IS TRUE and =True query conditions in MySQL?What is the difference between IS TRUE and =True query conditions in MySQL?Apr 19, 2025 pm 06:54 PM

The difference between ISTRUE and =True query conditions in MySQL In MySQL database, when processing Boolean values ​​(Booleans), ISTRUE and =TRUE...

How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling?How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling?Apr 19, 2025 pm 06:51 PM

How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling? Using EasyExcel for Excel...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment