【第1章】SpringBoot實戰篇之注冊接口

文章目錄

  • 前言
  • 一、代碼部分
    • 1. User
    • 2.UserMapper1
    • 3. UserSerivce
    • 4. UserController1
    • 5. Result
  • 二、測試
    • 1.注冊
    • 2.再次注冊
  • 總結


前言

下面介紹用戶注冊接口。


一、代碼部分

1. User

package org.example.springboot3.bigevent.entity;import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.time.LocalDateTime;@Getter
@Setter
@ToString
public class User {@TableIdprivate Integer id;//主鍵IDprivate String username;//用戶名private String password;//密碼private String nickname;//昵稱private String email;//郵箱private String userPic;//用戶頭像地址private LocalDateTime createTime;//創建時間private LocalDateTime updateTime;//更新時間
}

2.UserMapper1

package org.example.springboot3.bigevent.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.example.springboot3.bigevent.entity.User;/*** Create by zjg on 2024/5/22*/
@Mapper
public interface UserMapper1 extends BaseMapper<User> {
}

3. UserSerivce

package org.example.springboot3.bigevent.service;import org.example.springboot3.bigevent.entity.User;/*** Create by zjg on 2024/5/22*/
public interface UserSerivce {User findUserByName(String username);int addUser(String username, String password);
}
package org.example.springboot3.bigevent.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.springboot3.bigevent.mapper.UserMapper1;
import org.example.springboot3.bigevent.entity.User;
import org.example.springboot3.bigevent.service.UserSerivce;
import org.example.springboot3.bigevent.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;/*** Create by zjg on 2024/5/22*/
@Service
public class UserSerivceImpl implements UserSerivce {@AutowiredUserMapper1 userMapper1;@Overridepublic User findUserByName(String username) {QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.ne("username", username);return userMapper1.selectOne(queryWrapper);}@Overridepublic int addUser(String username, String password) {User user = new User();user.setUsername(username);user.setPassword(Md5Util.getMD5String(password));user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());return userMapper1.insert(user);}
}

4. UserController1

package org.example.springboot3.bigevent.controller;import org.example.springboot3.bigevent.entity.Result;
import org.example.springboot3.bigevent.entity.User;
import org.example.springboot3.bigevent.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** Create by zjg on 2024/5/22*/
@RequestMapping("/user/")
@RestController
public class UserController1 {@AutowiredUserSerivce userSerivce;@RequestMapping("register")public Result register(String username, String password){User user=userSerivce.findUserByName(username);if(user==null){//用戶不存在,可以注冊int i=userSerivce.addUser(username,password);if(i!=1){return Result.error("失敗注冊,請稍后重新注冊!");}}else{return Result.error("該用戶已存在,請重新注冊!");}return Result.success();}
}

5. Result

package org.example.springboot3.bigevent.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;//統一響應結果
@Data
@NoArgsConstructor
@AllArgsConstructor
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);}
}

二、測試

1.注冊

在這里插入圖片描述
在這里插入圖片描述

2.再次注冊

在這里插入圖片描述


總結

回到頂部

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/20651.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/20651.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/20651.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

開發板uboot與virtualbox虛擬機、windows11網絡互通

環境&#xff1a;virtualbox中ubuntu22.04.4&#xff0c;開發板通過網線再經過拓展塢usb網卡跟windows11連接。連接如下&#xff1a; 1、關閉windows防火墻(重要) 2、先在VirtualBox的工具選項創建兩個網絡【僅主機(Host-Only)網絡】和【NAT網絡】 僅主機(Host-Only)網絡的ip:…

Linux下Qt Creator無法輸入中文(已解決)

1. 首先確保安裝了搜狗輸入法&#xff0c;且能正常運行。 2.克隆源碼到本地。 git clone https://gitcode.com/fcitx/fcitx-qt5.git 3.檢查Qt Creator版本&#xff0c;如下圖所示&#xff0c;為基于Qt6的。 4. 進入源碼目錄&#xff0c;建立build文件夾&#xff0c;修改CMak…

并發任務的進化之旅

An evolutionary journey of multitasking 多重任務的進化之旅 In the beginning, computers had one CPU that executed a set of instructions written by a programmer one by one. No operating system (OS), no scheduling, no threads, no multitasking. This was how …

js實現基礎購物車的制作

功能需求&#xff1a; 1.點擊添加商品按鈕會出現三個輸入框&#xff08;名稱&#xff0c;價格&#xff0c;數量那三格&#xff0c;以及確認和取消按鈕&#xff09;。 2.點擊確認后所輸入的值會自動形成一行添加到表格中 3.點擊編輯按鈕時&#xff0…

css動態導航欄鼠標懸停特效

charset "utf-8"; /*科e互聯特效基本框架CSS*/ body, ul, dl, dd, dt, ol, li, p, h1, h2, h3, h4, h5, h6, textarea, form, select, fieldset, table, td, div, input {margin:0;padding:0;-webkit-text-size-adjust: none} h1, h2, h3, h4, h5, h6{font-size:12px…

8、資源操作 Resource

目錄 8.1、Spring Resources概述補充&#xff1a;什么是 low-level 資源&#xff1f;1. 文件系統資源2. 類路徑資源3. URL資源4. 內嵌資源5. InputStream資源6. ServletContext資源示例代碼結論 8.2、Resource接口8.3、Resource的實現類8.3.1、UrlResource訪問網絡資源1&#x…

LIO-EKF: 運行數據UrbanNav與mid360設備詳細教程

一、代碼連接 代碼下載連接&#xff1a; YibinWu/LIO-EKF: Maybe the simplest LiDAR-inertial odometry that one can have. (github.com) 編譯步驟&#xff1a; cd srcgit clone gitgithub.com:YibinWu/LIO-EKF.gitcatkin_makesource devel/setup.bash 運行步驟&#xff1a; …

為什么要保持方差為1

1.數值穩定性&#xff1a; 在機器學習和深度學習中&#xff0c;維持激活函數輸入的方差在一個合理范圍內&#xff08;如1&#xff09;是很重要的&#xff0c;這有助于防止在訓練過程中發生梯度消失或梯度爆炸的問題。如果方差過大或過小&#xff0c;經過多層網絡后輸出結果的方…

java并發處理機制

在Java中&#xff0c;并發處理機制主要是通過線程來實現的。Java提供了豐富的類和接口來支持多線程編程&#xff0c;主要集中在 java.util.concurrent 包中。以下是一些關鍵的并發處理機制&#xff1a; 1.線程創建&#xff1a;可以通過繼承 Thread 類或實現 Runnable 接口來創建…

公園【百度之星】/圖論+dijkstra

公園 圖論dijkstra #include<bits/stdc.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pii; vector<ll> v[40005]; //a、b、c分別是小度、度度熊、終點到各個點的最短距離 ll a[40005],b[40005],c[40005],dist[40005],st[40005]; void…

原碼、反碼和真值都不存在!

文章目錄 補碼的理解十進制計算二進制計算 補碼和真值換算數制轉換負數補碼轉真值負數真值轉補碼 注&#xff1a;均來自 做而論道 答主的理解。 補碼的理解 在計算機系統中&#xff0c;根本就沒有原碼和反碼&#xff0c;真值也是不存在的。在計算機系統中&#xff0c;并不使用…

java 遠程調試

1.遠程啟動時 jdk1.8-32\jre\bin\java.exe -Dfile.encodingUTF-8 -Djava.library.pathlib -agentlib:jdwptransportdt_socket,servery,suspendn,address5005 -jar local-com.yuetai.service-0.0.1-SNAPSHOT.jar --spring.config.locationapplication.yml 2.本地調試項目連接遠…

2024-06-01 Win 11 升級 TPM 2 問題

點擊 Windows 更新&#xff0c;遇到報錯&#xff0c;說是不支持 CPU 和 TPM 等&#xff0c;先是朋友給了一個鏈接文章&#xff0c;說是可以繞過&#xff0c;嘗試后&#xff0c;只是少了 CPU 的報錯&#xff0c;但 TPM 2 過不了。 后來在網上找到這篇文章&#xff0c; 先試了幾…

JCR一區級 | Matlab實現TCN-BiGRU-MATT時間卷積雙向門控循環單元多特征分類預測

JCR一區級 | Matlab實現TCN-BiGRU-MATT時間卷積雙向門控循環單元多特征分類預測 目錄 JCR一區級 | Matlab實現TCN-BiGRU-MATT時間卷積雙向門控循環單元多特征分類預測分類效果基本介紹程序設計參考資料 分類效果 基本介紹 1.Matlab實現TCN-BiGRU-MATT時間卷積雙向門控循環單元多…

一維時間序列信號的小波模極大值分解與重建(matlab R2018A)

數學上稱無限次可導函數是光滑的或沒有奇異性&#xff0c;若函數在某處有間斷或某階導數不連續&#xff0c;則稱函數在此處有奇異性&#xff0c;該點就是奇異點。奇異性反映了信號的不規則程度&#xff0c;因為信號的奇異點和突變部分往往攜帶者重要信息&#xff0c;因此信號的…

JDK1.8新特性1

JDK1.8新特性1 JDK1.8新特性&#xff1a;Lambda表達式&#xff1a;使用&#xff1a;無參數無返回值&#xff1a;單參數無返回值&#xff1a;多參數無返回值&#xff1a;多參數有返回值&#xff1a; 案例&#xff1a;案例1&#xff1a;案例2&#xff1a;案例3&#xff1a; 函數式…

代碼隨想錄訓練營Day 42|力扣62.不同路徑、63. 不同路徑 II

1.不同路徑 代碼隨想錄 視頻講解&#xff1a;動態規劃中如何初始化很重要&#xff01;| LeetCode&#xff1a;62.不同路徑_嗶哩嗶哩_bilibili 代碼&#xff1a; class Solution { public:int uniquePaths(int m, int n) {// dp[i][j] 表示從起點走到坐標為i&#xff0c;j的地方…

全自動打包封箱機:解析其在產品質量與安全保障方面的作用

在當今快節奏的生產環境中&#xff0c;全自動打包封箱機以其高效、精準的特點&#xff0c;正逐漸成為生產線上的得力助手。它不僅提升了生產效率&#xff0c;更在產品質量與安全保障方面發揮著舉足輕重的作用。星派將詳細解析全自動打包封箱機在產品質量與安全保障方面的作用。…

css簡單介紹

1.css介紹 css指的是層疊樣式(Cascadingstyle sheets)&#xff0c;是用來給HTML標簽添加樣式的語言。他可以設置HTML頁面中 文字大小&#xff0c;顏色&#xff0c;對齊方式及元素的 寬高&#xff0c; 位置 等樣式。 一個完整的網頁是由HTML、CSS、Javascript三部分組成。HT…

CLIP--Learning Transferable Visual Models From Natural Language Supervision

參考&#xff1a;CLIP論文筆記--《Learning Transferable Visual Models From Natural Language Supervision》_visual n-grams模型-CSDN博客 openAI&#xff0c;2021&#xff0c;將圖片和文字聯系在一起&#xff0c;----->得到一個能非常好表達圖片和文字的模型主題&#…