【第1章】SpringBoot实战篇之注册接口

文章目录


前言

下面介绍用户注册接口。


一、代码部分

1. User

java 复制代码
package org.example.springboot3.bigevent.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.time.LocalDateTime;

@Getter
@Setter
@ToString
public class User {
    @TableId
    private Integer id;//主键ID
    private String username;//用户名
    private String password;//密码
    private String nickname;//昵称
    private String email;//邮箱
    private String userPic;//用户头像地址
    private LocalDateTime createTime;//创建时间
    private LocalDateTime updateTime;//更新时间
}

2.UserMapper1

java 复制代码
package org.example.springboot3.bigevent.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.springboot3.bigevent.entity.User;

/**
 * Create by zjg on 2024/5/22
 */
@Mapper
public interface UserMapper1 extends BaseMapper<User> {
}

3. UserSerivce

java 复制代码
package org.example.springboot3.bigevent.service;

import org.example.springboot3.bigevent.entity.User;

/**
 * Create by zjg on 2024/5/22
 */
public interface UserSerivce {
    User findUserByName(String username);

    int addUser(String username, String password);
}
java 复制代码
package org.example.springboot3.bigevent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.springboot3.bigevent.mapper.UserMapper1;
import org.example.springboot3.bigevent.entity.User;
import org.example.springboot3.bigevent.service.UserSerivce;
import org.example.springboot3.bigevent.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

/**
 * Create by zjg on 2024/5/22
 */
@Service
public class UserSerivceImpl implements UserSerivce {
    @Autowired
    UserMapper1 userMapper1;
    @Override
    public User findUserByName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("username", username);
        return userMapper1.selectOne(queryWrapper);
    }

    @Override
    public int addUser(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(Md5Util.getMD5String(password));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        return userMapper1.insert(user);
    }
}

4. UserController1

java 复制代码
package org.example.springboot3.bigevent.controller;

import org.example.springboot3.bigevent.entity.Result;
import org.example.springboot3.bigevent.entity.User;
import org.example.springboot3.bigevent.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create by zjg on 2024/5/22
 */
@RequestMapping("/user/")
@RestController
public class UserController1 {
    @Autowired
    UserSerivce userSerivce;
    @RequestMapping("register")
    public Result register(String username, String password){
        User user=userSerivce.findUserByName(username);
        if(user==null){//用户不存在,可以注册
            int i=userSerivce.addUser(username,password);
            if(i!=1){
                return Result.error("失败注册,请稍后重新注册!");
            }
        }else{
            return Result.error("该用户已存在,请重新注册!");
        }
        return Result.success();
    }
}

5. Result

java 复制代码
package org.example.springboot3.bigevent.entity;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

//统一响应结果
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private Integer code;//业务状态码  0-成功  1-失败
    private String message;//提示信息
    private T data;//响应数据

    //快速返回操作成功响应结果(带响应数据)
    public static <E> Result<E> success(E data) {
        return new Result<>(0, "操作成功", data);
    }

    //快速返回操作成功响应结果
    public static Result success() {
        return new Result(0, "操作成功", null);
    }

    public static Result error(String message) {
        return new Result(1, message, null);
    }
}

二、测试

1.注册


2.再次注册


总结

回到顶部

相关推荐
爬山算法3 分钟前
Hibernate(88)如何在负载测试中使用Hibernate?
java·后端·hibernate
独断万古他化21 分钟前
【Spring 原理】Bean 的作用域与生命周期
java·后端·spring
我爱加班、、31 分钟前
Websocket能携带token过去后端吗
前端·后端·websocket
一 乐1 小时前
校园二手交易|基于springboot + vue校园二手交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
80530单词突击赢1 小时前
SpringBoot整合SpringMVC全解析
java·spring boot·后端
vx1_Biye_Design1 小时前
基于Spring Boot+Vue的学生管理系统设计与实现-计算机毕业设计源码46223
java·vue.js·spring boot·spring·eclipse·tomcat·maven
vx_Biye_Design1 小时前
基于Spring Boot+vue的湖北旅游景点门票预约平台的设计--毕设附源码29593
java·vue.js·spring boot·spring cloud·servlet·eclipse·课程设计
qq5_8115175151 小时前
web城乡居民基本医疗信息管理系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
hdsoft_huge1 小时前
1panel面板中部署SpringBoot和Vue前后端分离系统 【图文教程】
vue.js·spring boot·后端
Hx_Ma162 小时前
SpringBoot数据源自动管理
java·spring boot·spring