欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java实战(二)用户注册、登录接口

程序员文章站 2023-12-22 18:59:40
...

目录结构:

Java实战(二)用户注册、登录接口

1. 创建User实体类和接口调用信息描述实体类Response

package com.yxf.demo.entity;

/**
 * 抽象用户信息
 */

public class User {
    public int id;
    public String username;
    public String password;
    public String phone;

    public User() {
    }

    public User(String username, String password, String phone) {
        this.username = username;
        this.password = password;
        this.phone = phone;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String name) {
        this.username = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}
package com.yxf.demo.entity;

/**
 * 用来返回接口调用信息描述
 */

public class Response {
    String msg;
    int code;
    Boolean isTure = true;

    public Response() {
    }

    public Response(String msg, int code, Boolean isTure) {
        this.msg = msg;
        this.code = code;
        this.isTure = isTure;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Boolean getTure() {
        return isTure;
    }

    public void setTure(Boolean ture) {
        isTure = ture;
    }
}

2.创建dao层。

package com.yxf.demo.dao;

import com.yxf.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface IUserDao {
    int addUser(User user);
    List<User> queryByName(String username);
}

3..创建userService接口,实例化接口

package com.yxf.demo.service;

import com.yxf.demo.entity.User;

import java.util.List;

/**
 * 根据业务关系,编写相关业务逻辑。
 */

public interface IUserService {
    int addUser(String username,String password,String phone);
    List<User>  queryByName(String username);
}
package com.yxf.demo.service.impl;

import com.yxf.demo.entity.User;
import com.yxf.demo.dao.IUserDao;
import com.yxf.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    IUserDao userDao;

    @Override
    public int addUser(String username,String password,String phone) {
        User user = new User(username, password, phone);
        int isadd = userDao.addUser(user);
        return isadd;
    }

    @Override
    public List<User> queryByName(String username){
        List<User> userList = userDao.queryByName(username);
        return userList;
    }
}

4.创建controller

package com.yxf.demo.controller;

import com.yxf.demo.entity.Response;
import com.yxf.demo.entity.User;
import com.yxf.demo.service.IUserService;
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 java.util.List;
import java.util.Map;


@RestController
public class UserController {
    @Autowired
    private IUserService userService;
    //注册
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public Response register(@RequestBody Map<String,String> person){
        String username = person.get("username");
        String password = person.get("password");
        String phone = person.get("phone");

        if (username != null && password != null && phone != null){
            List<User> user = userService.queryByName(username);
            if(user.size() == 0){
                int isadd = userService.addUser(username,password,phone);
                if (isadd > 0){
                    return new Response("注册成功", 1, true);
                }else {
                    return new Response("注册失败", -1, false);
                }
            }else {
                return new Response("注册失败:用户名已存在",-1,false);
            }
        }else {
            return new Response("注册失败:用户名、密码、手机号不能为空",-1,false);
        }
    }

    //登录
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public Response login(@RequestBody Map<String, String> person) {
        String username = person.get("username");
        String password = person.get("password");

        if(username != null && password != null){
            List<User> user = userService.queryByName(username);
            if(user.size() == 0){
                return new Response("登录失败:用户名不存在", -1, false);
            }else {
                if (user.get(0).getPassword().equals(password)){
                    return new Response("登录成功", 1, true);
                }else {
                    return new Response("登录失败:密码错误", -1, false);
                }
            }
        }else {
            return new Response("登录失败:用户名密码不能为空",-1,false);
        }
    }

}

5.创建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.yxf.demo.dao.IUserDao">
    <resultMap id="BaseResultMap" type="com.yxf.demo.entity.User">
        <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.yxf.demo.entity.User">
        INSERT INTO user(username,password,phone) VALUES(#{username},#{password},#{phone})
    </insert>

    <select id="queryByName" resultType="com.yxf.demo.entity.User">
        SELECT * FROM user WHERE username = #{username}
    </select>
</mapper>

 

 

 

相关标签: 注册 登录

上一篇:

下一篇: