springboot3+vue3(二)注册接口

 

为了方便实体类操作,这里添加一下lombok的依赖,添加好以后右键重新加载maven

lombok注解含义大全:https://www.jianshu.com/p/41c4a226e955

<!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

 

 编写接口顺序

Controller(控制器) => Service(接口层) => impl(实现层) => Mapper(与数据库交互层)

写好Controller后可通过 alt+enter 创建 Service

同理可自动创建 impl 和 Mapper

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public Result register(String username,String password)
    {
        //查重用户
        User u = userService.findByUserName(username);
        if(u==null)
        {
            //没有占用,注册
            userService.register(username,password);
            return Result.success();
        }
        else
        {
            //用户名已被占用
            return Result.error("用户名已被占用!");
        }

    }
}
Controller
package com.example.bigevent.pojo;


//统一响应结果

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

@NoArgsConstructor//无参构造
@AllArgsConstructor//全参构造
@Data//geter seter
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);
    }
}
pojo Result返回实体
@Service
public interface UserService {

    //根据用户名查询用户
    User findByUserName(String username);
    //注册
    void register(String username, String password);
}
Service
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username){
        User u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    public void register(String username, String password){
        //加密
        String md5password = Md5Util.getMD5String(password);
        //添加
        userMapper.register(username,md5password);
    }
}
impl
package com.example.bigevent.utils;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {
    /**
     * 默认的密码字符串组合,用来将字节转换成 16 进制表示的字符,apache校验下载的文件的正确性用的就是默认的这个组合
     */
    protected static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    protected static MessageDigest messagedigest = null;

    static {
        try {
            messagedigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException nsaex) {
            System.err.println(Md5Util.class.getName() + "初始化失败,MessageDigest不支持MD5Util。");
            nsaex.printStackTrace();
        }
    }

    /**
     * 生成字符串的md5校验值
     *
     * @param s
     * @return
     */
    public static String getMD5String(String s) {
        return getMD5String(s.getBytes());
    }

    /**
     * 判断字符串的md5校验码是否与一个已知的md5码相匹配
     *
     * @param password  要校验的字符串
     * @param md5PwdStr 已知的md5校验码
     * @return
     */
    public static boolean checkPassword(String password, String md5PwdStr) {
        String s = getMD5String(password);
        return s.equals(md5PwdStr);
    }


    public static String getMD5String(byte[] bytes) {
        messagedigest.update(bytes);
        return bufferToHex(messagedigest.digest());
    }

    private static String bufferToHex(byte bytes[]) {
        return bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte bytes[], int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;
        for (int l = m; l < k; l++) {
            appendHexPair(bytes[l], stringbuffer);
        }
        return stringbuffer.toString();
    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字节中高 4 位的数字转换, >>>
        // 为逻辑右移,将符号位一起右移,此处未发现两种符号有何不同
        char c1 = hexDigits[bt & 0xf];// 取字节中低 4 位的数字转换
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }

}
utils下的md5工具类
@Mapper
public interface UserMapper {
    @Select("select * from user where username=#{username}")
    User findByUserName(String username);

    @Insert("insert into user(username,password,create_time,update_time)" +
            " values (#{username},#{password},now(),now())")
    void register(String username, String password);
}
Mapper

 

运行:

 此报错是因为springboot版本和mybatis冲突问题导致  将springboot版本改为3.1.5即可

 改好后重新加载maven

 

 

 

posted @ 2024-03-04 14:32  一介桃白白  阅读(152)  评论(0编辑  收藏  举报