springboot登录注册接口

登录

注意:后台结果返回类参考https://blog.csdn.net/m0_58467275/article/details/126006167?spm=1001.2014.3001.5501

Entity层

package com.example.demo.controller.dto;

import lombok.Data;

/**
 * @Author Alva
 * @Date 2022/7/27 9:02
 * @PackageName:com.example.demo.controller.dto
 * @ClassName: UserDTO
 * @Description: 登录注册参数实体类
 */
@Data
public class UserDTO {
    private Integer id;
    private String username;
    private String password;
}

 Controller层

@Resource
private IUserService userService;
        //登录
        @PostMapping("/login")
        public Result login(@RequestBody UserDTO userDTO) {
                String username = userDTO.getUsername();
                String password = userDTO.getPassword();
                if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
                        return Result.error(Constants.CODE_400, "参数错误");
                }
                UserDTO dto = userService.login(userDTO);
                return Result.success(dto);
        }

Service层

接口

public interface IUserService extends IService<User> {

    UserDTO login(UserDTO userDTO);
}

实现类

package com.example.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.config.Result.Constants;
import com.example.demo.config.Result.ServiceException;
import com.example.demo.controller.dto.UserDTO;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author alva
 * @since 2022-07-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    //Log--日志工具类
    private static final Log LOG = Log.get();
    @Override
    public UserDTO login(UserDTO userDTO) {
        //        在增删改查时添加条件,前端传过来数据,后端去查找
        User ones = getUserInfo(userDTO);//这里User类是表对应的类,因为参数不需要那么多,所以前端带的参数我们另外增加一个UserDTO类
        if(ones !=null){
            //查询有结果
            BeanUtil.copyProperties(ones, userDTO, true);
            return userDTO;
        }else {
            throw new ServiceException(Constants.CODE_600, "用户名或密码错误");
        }
    }

    private User getUserInfo(UserDTO userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userDTO.getUsername());
        queryWrapper.eq("password", userDTO.getPassword());
        User one;
        try {
//            从数据库查询信息
            one = getOne(queryWrapper);
        } catch (Exception e) {
//            打印错误日志
            LOG.error(e);
//            脏数据,查到两条一样的
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return one;
    }
}

注册

Controller层

     //注册
        @PostMapping("/register")
        public Result register(@RequestBody UserDTO userDTO) {
                String username = userDTO.getUsername();
                String password = userDTO.getPassword();
                if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
                        return Result.error(Constants.CODE_400, "参数错误");
                }
                return Result.success(userService.register(userDTO));
        }

 实现类

 @Override
    public User register(UserDTO userDTO) {
        User one = getUserInfo(userDTO);
        if (one == null){
            one = new User();
            BeanUtil.copyProperties(userDTO, one, true);
            save(one);  // 把 copy完之后的用户对象存储到数据库
        }else {
            throw new ServiceException(Constants.CODE_600, "用户已存在");
        }
        return one;
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值