User data check function

 

table of Contents

Interface Description

sing

Controller

Service

Mapper


 

Interface Description

 

Implementing user data validation, mainly including: phone number, user name uniqueness check.

Interface path:

GET /check/{data}/{type}

Parameter Description:

parameter Explanation Do you have to type of data Defaults
data To verify the data Yes String no
type To verify the type of data: 1, the user name; 2, mobile phones; no Integer 1

Return result:

Return boolean results:

  • true: Available

  • false: Not available

status code:

  • 200: check the success

  • 400: The parameter is incorrect

  • 500: Server inner exception

 

 

sing

 

@Table(name = "tb_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String username;// 用户名

    @JsonIgnore  //对象序列化为json字符串时,忽略该属性
    private String password;// 密码

    private String phone;// 电话

    private Date created;// 创建时间

    @JsonIgnore
    private String salt;// 密码的盐值

 

 

Controller

 

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    //对用户名和手机号进行数据校验
    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUser(@PathVariable("data")String data, @PathVariable("type")Integer type) {
        Boolean bool = this.userService.checkUser(data, type);
        if(bool == null) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(bool);
    }
}

 

 

Service

 

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 校验数据是否可用
     * @param data
     * @param type
     * @return
     */
    public Boolean checkUser(String data, Integer type) {
        User record = new User();
        if (type == 1) {
            record.setUsername(data);
        } else if (type == 2) {
            record.setPhone(data);
        } else {
            return null;
        }
        return this.userMapper.selectCount(record) == 0;
    }
}

 

 

Mapper

 

public interface UserMapper extends Mapper<User> {
}

 

Published 343 original articles · won praise 162 · Views 140,000 +

Guess you like

Origin blog.csdn.net/Delicious_Life/article/details/104425734