SpringBoot—实现一个登录注册(超级详细)
文章目录
前言
注:学spring boot 的新手或还没有入门的,可以看一下,适合小白的登录接口
提示:以下是本篇文章正文内容,下面案例可供参考
一、创建Springboot项目
二、创建完项目后在pom.xml导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gzh</groupId>
<artifactId>spring_boot_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_boot_demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mysql的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<!-- Mybatis的依赖 -->
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.1在application.properties
三、数据库中创建t_user表
四、编写实体类User
package com.gzh.spring_boot_demo.com.gzh.entity;
import java.util.Objects;
/**
* @Author {高哲浩}
* @Date: 2022/06/15/ 16:29
* @Description
*/
public class User {
private Integer uid;
private String uname;
private String pwd;
public User() {
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User(Integer uid, String uname, String pwd) {
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(uid, user.uid) &&
Objects.equals(uname, user.uname) &&
Objects.equals(pwd, user.pwd);
}
@Override
public int hashCode() {
return Objects.hash(uid, uname, pwd);
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", uname='" + uname + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
五、编写UserMapper接口
package com.gzh.spring_boot_demo.com.gzh.mapper;
import com.gzh.spring_boot_demo.com.gzh.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;
import java.beans.Transient;
/**
* @Author {高哲浩}
* @Date: 2022/06/15/ 18:26
* @Description
*/
@Mapper
public interface UserMapper {
//登录
@Select("select * from t_user where uname=#{uname} and pwd=#{pwd}")
User login(@Param("uname")String uname,@Param("pwd") String pwd);
//注册
@Update("insert into t_user values(default,#{uname},#{pwd})")
@Transactional
void register(User user);
//注册时判断用户是否存在
@Select("select* from t_user where uname=#{uname}")
@Transactional
User registerByName(String name);
}
六、编写UserController
package com.gzh.spring_boot_demo.com.gzh.controller;
import com.gzh.spring_boot_demo.com.gzh.entity.User;
import com.gzh.spring_boot_demo.com.gzh.mapper.UserMapper;
import com.gzh.spring_boot_demo.com.gzh.service.UploadMangeService;
import com.gzh.spring_boot_demo.com.gzh.util.Response;
import com.gzh.spring_boot_demo.com.gzh.util.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @Author {高哲浩}
* @Date: 2022/06/15/ 16:08
* @Description
*/
@RestController
@RequestMapping("/api")
public class UserController {
@Resource
UserMapper userMapper;
/**
* 登录
*
* @param user
* @return
*/
@PostMapping("/login")
public Response loginUser(@RequestBody User user) {
String uname = user.getUname();
String pwd = user.getPwd();
System.out.println("uname=" + uname + "pwd" + pwd);
if (uname.equals("") || pwd.equals("")) {
return new Response(100, "用户名或密码不能为空", "");
} else {
if (userMapper.login(uname, pwd) != null) {
return new Response(200, "操作成功", "");
} else {
return new Response(500, "用户不存在/密码错误", "");
}
}
}
@PostMapping("/register")
public Response register(@RequestBody User user) {
String uname = user.getUname();
String pwd = user.getPwd();
if (uname.equals("") || pwd.equals("")) {
return new Response(100, "用户名或密码不能为空", "");
} else {
if (userMapper.registerByName(user.getUname()) == null) {
userMapper.register(user);
return new Response(200, "操作成功", "");
} else {
return new Response(500, "注册失败", "用户已存在");
}
}
}
}
七、编写Response
package com.gzh.spring_boot_demo.com.gzh.util;
import java.util.Date;
import java.util.Objects;
/**
* @Author {高哲浩}
* @Date: 2022/06/15/ 19:22
* @Description
*/
public class Response {
private Integer code;
private String msg;
private String token;
public Response(Integer code, String msg, String token) {
this.code = code;
this.msg = msg;
this.token = token;
}
@Override
public String toString() {
return "Response{" +
"code=" + code +
", msg='" + msg + '\'' +
", token='" + token + '\'' +
'}';
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response response = (Response) o;
return Objects.equals(code, response.code) &&
Objects.equals(msg, response.msg) &&
Objects.equals(token, response.token);
}
@Override
public int hashCode() {
return Objects.hash(code, msg, token);
}
}
八、运行项目,在postman中进行测试
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。