接口文檔:通俗的講,接口文檔能告訴開發者接口能返回的數據,以及為了獲取這些數據,開發者
需要輸入什么樣的數據,請求哪個接口(即規范)
為什么使用接口文檔:
1、項目開發過程中前后端工程師有一個統一的文件進行溝通交流開發
2、項目維護中或者項目人員更迭,方便后期人員查看、維護
3、測試人員驗證檢查接口是否正確
環境搭建:
1.準備數據庫(big_event)
2.創建SpringBoot工程,引入依賴(web、mybatis、mysql驅動、lombok)
3.創建包結構,準備實體類
注冊:
1.Usercontroller
?
package com.rzl.controller;
import com.rzl.pojo.Result;
import com.rzl.pojo.User;
import com.rzl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;@PostMapping("/register")public Result register(String username, String password) {//查詢對象User u = userService.findByUserName(username);if (u == null) {//沒有占用//注冊userService.register(username, password);return Result.success();} else {//占用return Result.error("用戶名已被占用");}}
}?
2.User實體類
package com.rzl.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {private Integer id;//主鍵IDprivate String username;//用戶名private String password;//密碼private String nickname;//昵稱private String email;//郵箱private String userPic;//用戶頭像地址private LocalDateTime createTime;//創建時間private LocalDateTime updateTime;//更新時間
}
3.UserService接口
package com.rzl.service;
import com.rzl.pojo.User;
public interface UserService {//根據用戶名查詢用戶User findByUserName(String username);//注冊void register(String username, String password);
}
4.UserServiceImpl實現類
package com.rzl.service.impl;
import com.rzl.mapper.UserMapper;
import com.rzl.pojo.User;
import com.rzl.service.UserService;
import com.rzl.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic User findByUserName(String username) {User u = userMapper.findByName(username);return u;}@Overridepublic void register(String username, String password) {//密碼加密String md5String = Md5Util.getMD5String(password);//添加userMapper.add(username,md5String);}
}
5.UserMapper接口
package com.rzl.mapper;
import com.rzl.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper {//根據用戶名查詢用戶@Select("select *from user where username = #{username}")User findByName(String username);//添加@Insert("insert into user(username,password,create_time,update_time)" +" values(#{username},#{md5String},now(),now())")void add(String username, String md5String);
}
6.數據庫
-- 創建數據庫
create database big_event;-- 使用數據庫
use big_event;用戶表
create table user (id int unsigned primary key auto_increment comment 'ID',username varchar(20) not null unique comment '用戶名',password varchar(32) comment '密碼',nickname varchar(10) default '' comment '昵稱',email varchar(128) default '' comment '郵箱',user_pic varchar(128) default '' comment '頭像',create_time datetime not null comment '創建時間',update_time datetime not null comment '修改時間'
) comment '用戶表';
7.使用postman,可得結果
登錄:
Usercontroller
@PostMapping("/login")public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password) {// 根據用戶名查詢用戶User loginUser = userService.findByUserName(username);//判斷該用戶是否存在if (loginUser == null) {return Result.error("用戶名錯誤");}//判斷密碼是否正確 loginUser對象中的password是密文if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {//登陸成功return Result.success("jwt token令牌..");}return Result.error("密碼錯誤");}
使用postman
登錄成功:
用戶名錯誤:
密碼錯誤: