超详细超实用!!!零基础java开发之云风笔记登录、注册接口开发(六)

云风网
云风笔记
云风知识库

之前我们实现了java如何链接数据库,接下来就开始尝试进行业务逻辑接口开发,主要进行登录和注册接口的开发

一、改造实体类user/UserInfo

注册接口和登录接口需要用到的属性就是用户名 username、密码 password、手机号 phone。在原有逻辑上加入phone电话属性字段

package com.example.study.user;

public class UserInfo {
    public static String username;
    public static String password;
    public static String phone;
    public UserInfo(String username, String password, String phone) {
        UserInfo.username = username;
        UserInfo.password = password;
        UserInfo.phone = phone;
    }
    public static void setName(String username){
        UserInfo.username = username;
    }
    public static void setPassword(String password){
        UserInfo.password = password;
    }
    public static void setPhone(String phone){
        UserInfo.phone = phone;
    }
    public String getPassword(){
        return  password;
    }
    public String getUsername(){
        return  username;
    }
    public String getPhone(){
        return  phone;
    }
}

二、改造实体类user/Response

新增接口状态字段success,这样集成了接口的基本三字段code/msg/success,用来返回接口调用信息描述。

package com.example.study.user;

public class Response {
    private static String msg;
    private static int code;
    private static Boolean success;

    public Response(Boolean success,String msg, int code) {
        Response.msg = msg;
        Response.code = code;
        Response.success = success;
    }
    public static void setMsg(String msg) {
        Response.msg = msg;
    }
    public static void setCode(int code) {
        Response.code = code;
    }
    public static void setSuccess(Boolean success) {
        Response.success = success;
    }
    public int getCode() {return code;}
    public String getMsg() {return msg;}
    public Boolean getSuccess() {return success;}
}

三、改造服务类 service /UserApi

package com.example.study.service;
import com.example.study.user.UserInfo;
import java.util.List;

public interface UserApi {
    int addUser(String username,String password,String phone);//注册时,添加用户
    List<UserInfo> queryByUsername(String username);//登陆时,验证用户
}

四、改造服务类 service /impl /UserServiceImpl

package com.example.study.service.impl;
import com.example.study.user.UserInfo;
import com.example.study.mapper.UserMapper;
import com.example.study.service.UserApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserApi {
    @Autowired
    UserMapper userMapper;
    public int addUser(String username,String password,String phone){
        UserInfo user = new UserInfo(username,password,phone);
        return userMapper.addUser(user);
    }
    public List <UserInfo> queryByUsername(String username){
        return userMapper.queryByUsername(username);
    }
}

五、改造map类mapper/UserMapper

package com.example.study.mapper;
import com.example.study.user.UserInfo;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface UserMapper {
    int addUser(UserInfo user);
    List<UserInfo> queryByUsername(String username);
}

六、改造sql逻辑resources/mapper/UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.study.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.example.study.user.UserInfo">
        <result column="username" jdbcType="VARCHAR" property="username" />
        <result column="password" jdbcType="VARCHAR" property="password" />
        <result column="phone" jdbcType="VARCHAR" property="phone" />
    </resultMap>
    <insert id="addUser" parameterType="com.example.study.user.UserInfo">
        INSERT INTO `user` VALUES(#{username},#{password},#{phone})
    </insert>
    <select id="queryByUsername" resultType="com.example.study.user.UserInfo">
        SELECT * FROM `user` WHERE username = #{username}
    </select >
</mapper>

七、改造控制类UserController调用

package com.example.study.controller;
import com.example.study.user.Response;
import com.example.study.user.UserInfo;
import com.example.study.service.UserApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import java.util.List;
import java.util.Map;

@RestController
@EnableWebMvc
public class UserController {
    @Autowired
    public UserApi service;
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Response register(@RequestBody Map<String, String> person){
        System.out.println(person);
        String username = person.get("username");
        String password = person.get("password");
        String phone = person.get("phone");
        //1.判断用户名、密码、手机号是否为空
        if(username != null && password != null && phone != null){
            List<UserInfo> users =  service.queryByUsername(username);
            //2.判断是否有重复用户名
            if(users!=null && !users.isEmpty()){
                return new Response(true,"注册失败,用户名重复,请更换",400);
            }else {
                int count = service.addUser(username,password,phone);
                if(count>0){
                    //3.没有重复用户名,注册成功
                    return new Response(true,"注册成功",200);
                }else {
                    return new Response(true,"注册失败",400);
                }
            }
        }else{
            return new Response(true,"注册失败,请检查用户名、密码、手机号是否为空",400);
        }
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public Response login(@RequestBody Map<String, String> person){
        String username = person.get("username");
        String password =person.get("password");
        //1. 判断用户名、密码是否为空
        if(username != null && password != null ){
            List<UserInfo> users =  service.queryByUsername(username);
            //2. 判断用户名是否存在
            if(users!=null && !users.isEmpty()){
                UserInfo user = users.getFirst();
                //3. 判断密码是否正确
                if(password.equals(user.getPassword())){
                    //4. 密码正确,登陆成功
                    return new Response(true,"登陆成功",200);
                }else {
                    return new Response(false,"登陆失败,密码错误",400);
                }
            }else {
                return new Response(true,"登陆失败,用户名不存在",400);
            }
        }else {
            return new Response(true,"登陆失败,请检查用户名、密码是否为空",400);
        }
    }

}



八、运行项目并测试接口

在这里插入图片描述

1、注册接口请求成功

在这里插入图片描述
在这里插入图片描述

2、登录接口请求成功

在这里插入图片描述

3、登录接口用户名错误,正常请求,正确提示

在这里插入图片描述

4、登录接口密码错误,正常请求,正确提示

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

niech_cn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值