1. 程式人生 > >springboot 寫一個使用者登入註冊的demo

springboot 寫一個使用者登入註冊的demo

用idea自動生成了專案之後,就可以直接訪問專案了,但是目前的專案只能訪問靜態頁面,無法實現相關功能。本篇以構建登入註冊使用者資訊管理為demo,來講解如何使用springboot。

首先,在資料庫中建立一個數據表,然後使用idea自動生成持久化類,
點選file,選擇專案結構,然後加入jpa到專案中
這裡寫圖片描述
點選左下角的Persistence視窗,右鍵專案
這裡寫圖片描述

這裡寫圖片描述
選擇之後看到
這裡寫圖片描述

填寫完之後就可以了。我在mysql中建立瞭如下的表:

USE `sbdemo`;

-- 匯出  表 sbdemo.users 結構
CREATE TABLE IF NOT EXISTS `users`
( `uid` int(10) unsigned NOT NULL AUTO_INCREMENT, `userphone` bigint(20) DEFAULT NULL, `username` varchar(50) DEFAULT NULL, `nickname` varchar(50) DEFAULT NULL, `sex` int(11) DEFAULT NULL, PRIMARY KEY (`uid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

生成的結果如下:

import javax.persistence.Basic;
import
javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import java.util.Objects; @Entity @Table(name = "users", schema = "sbdemo", catalog = "") public class UsersEntity { private int uid; private Long userphone; private String username; private String nickname; private
Integer sex; @Basic @Column(name = "uid") public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } @Basic @Column(name = "userphone") public Long getUserphone() { return userphone; } public void setUserphone(Long userphone) { this.userphone = userphone; } @Basic @Column(name = "username") public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Basic @Column(name = "nickname") public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @Basic @Column(name = "sex") public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UsersEntity that = (UsersEntity) o; return uid == that.uid && Objects.equals(userphone, that.userphone) && Objects.equals(username, that.username) && Objects.equals(nickname, that.nickname) && Objects.equals(sex, that.sex); } @Override public int hashCode() { return Objects.hash(uid, userphone, username, nickname, sex); } }

然後到去建立一個倉庫類

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface LoginRepository extends JpaRepository<UsersEntity,Integer> {
}

用於對資料庫進行CRUD操作
然後寫一個控制器,來處理相關的邏輯

@RestController
public class LoginController {
    @Autowired
    LoginRepository loginRepository;

    //寫邏輯
}

通過@RestController表明這是一個控制器類,@Autowired用來進行自動依賴注入,將剛才定義的倉庫注入到控制器中。
要寫一個介面,首先得定義返回值型別和接收的引數。設計接收的引數為userphone和username,分別表示使用者名稱和密碼,使用者名稱為電話號碼。返回的引數為code表示成功或者失敗,info代表錯誤提示資訊。

@JsonInclude(JsonInclude.Include.NON_NULL)
public class LoginResultVo {
    private int code;
    private String info;
    public int getCode(){
        return code;
    }
    public String getInfo(){return info;}
    public void setInfo(int code){
        this.code = code;
    }
    public void setInfo(String info){
        this.info = info;
    }
}

接收的引數vo如下:

public class LoginRequestVo {
    private int userphone;
    private String username;
    public int getUserphone() {
        return userphone;
    }
    public void setUserphone(int userphone) {
        this.userphone = userphone;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
}

其中,@JsonInclude(JsonInclude.Include.NON_NULL) 是為了過濾不正常的輸出(當輸出為空的時候不輸出)
一個簡單的例子是這樣的

@RequestMapping(value = "/login",method = { RequestMethod.POST})
    public LoginResultVo loginRequest(@RequestParam("data") String data){
        LoginResultVo loginResultVo = new LoginResultVo();
        try {
            Gson gson = new Gson();
            LoginRequestVo loginRequestVo = gson.fromJson(data,LoginRequestVo.class);


        }catch (Exception e){
            e.printStackTrace();
        }
        return loginResultVo;
    }

最後完整的程式碼如下:

 @RequestMapping(value = "/login",method = { RequestMethod.POST})
    public LoginResultVo loginRequest(@RequestParam("data") String data){
        LoginResultVo loginResultVo = new LoginResultVo();
        try {
            Gson gson = new Gson();
            LoginRequestVo loginRequestVo = gson.fromJson(data,LoginRequestVo.class);
            int userphone = loginRequestVo.getUserphone();
            String username = loginRequestVo.getUsername();
            UsersEntity usersEntity=loginRepository.findByUserphoneAndUsername(userphone);
            String password = usersEntity.getUsername();
            if (username.equals(password)){
                loginResultVo.setCode(200);
                loginResultVo.setInfo("密碼正確,登入成功");
            }else{
                loginResultVo.setCode(400);
                loginResultVo.setInfo("密碼錯誤,登入失敗");
            }
            return loginResultVo;
        }catch (Exception e){
            e.printStackTrace();
        }
        return loginResultVo;
    }