目录
3.找到com.example.demo 目录,在该目录下创建 annotation common controller mapper model Utils 目录
5.找到application.properties 配置成自己数据库的信息
8.model目录下新建Vo目录,Vo目录下新建LoginVo.java
9.mapper目录下新建AuthMapper.java文件
10.controller目录下新建AuthController.java文件
一、 环境及软件
1.环境和软件
环境:
JDK 1.8.0
maven项目
MySQL
软件:
IntelliJ IDEA 2022.3.1(专业版)
MyAQL Workbench(可视化数据库设计软件)
Navicat(链接数据库软件)
Postman(接口测试)
2.软件的安装
IntelliJ IDEA 2022.3.1安装
官网:IntelliJ IDEA – the Leading Java and Kotlin IDE (jetbrains.com)
破解方法:Intellij IDEA最新激活码,支持2023版本(全年更新) (yuque.com)
MyAQL Workbench 安装及如何实现汉化
实现汉化的步骤:
1.下载汉化工具
汉化工具:https://pan.baidu.com/s/1PLc-5LivnLvV0iusp2mo4g?pwd=m7kd
提取码: m7kd
下载工具参考:(2条消息) mysql workbench怎么设置为中文?(仅菜单项汉化)附带main_menu文件_mysql workbench中文设置_大大。的博客-CSDN博客
2.找到自己MySQL Workbench的文件目录
3. 打开data文件夹,找到main_menu.xml ,用刚才下载的文件替代(建议关闭软件替换)
Navicat下载
官网:
Navicat | 支持 MySQL、MariaDB、MongoDB、SQL Server、SQLite、Oracle 和 PostgreSQL 的数据库管理
postman 下载
官网:Postman API Platform | Sign Up for Free
二、使用IDEA 搭建Springboot 项目
1. 新建项目
将服务器改为阿里云镜像: https://start.aliyun.com
2.选择springboot版本和依赖项
3.找到com.example.demo 目录,在该目录下创建 annotation common controller mapper model Utils 目录
4.新建数据库
1.新建一个数据库
点击Apply成功创建数据库
2. 在数据库中创建一张user表(使用到Navicat)
与数据库创建连接
选中数据库创建表格,添加字段,最后保存将表名命名为user
参考博客:MySQL Workbench 使用教程 - 如何使用 Workbench 操作 MySQL / MariaDB 数据库中文指南_mysql workbranch咋用_蒋川_卡拉云的博客-CSDN博客
5.找到application.properties 配置成自己数据库的信息
更改配置信息 :
代码:
#应用名称
spring.application.name=demo
#应用服务WEB访问端口
server.port=8080
#数据库
#数据库驱动源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据源名称
spring.datasource.name=defaultDataSource
#数据库链接地址
spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot?\
useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&allow PublicKeyRetrieval=true
#用户名和密码
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:mappers/*xml
mybatis.type-aliases-package=com.example.demo.mybatis.entity
补:
JDBC:JDBC是Java Database Connectivity的缩写,是标准的Java API,是一套客户端程序与数据库交互的规范
6. Utils目录下新建ResultUtil.java
统一响应式封装类
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultUtil<T> {
private int status;
private String message;
private String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
private T data;
private Boolean success;
public void success(int status, String message) {
this.status = status;
this.message = message;
this.success = true;
}
public void success(int status, String message, T data) {
this.status = status;
this.message = message;
this.success = true;
this.data = data;
}
public void error(int status, String message, HttpServletResponse response) {
this.status = status;
this.message = message;
this.success = false;
response.setStatus(status);
response.setHeader("Content-Type", "application/json;charset=utf8");
}
public void error(int status, String message) {
this.status = status;
this.message = message;
this.success = false;
}
public void dataValidation422(String message, HttpServletResponse response) {
this.status = 422;
this.message = message;
this.success = false;
response.setStatus(422);
response.setHeader("Content-Type", "application/json;charset=utf8");
}
public void noAuthorization401(HttpServletResponse response) {
this.status = 401;
this.message = "未登录/未授权";
this.success = false;
response.setStatus(401);
response.setHeader("Content-Type", "application/json;charset=utf8");
}
}
7.model目录下新建UserModel.java
user表映射
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserModel {
private int id;
private String username;
private String password;
}
8.model目录下新建Vo目录,Vo目录下新建LoginVo.java
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import javax.validation.constraints.NotBlank;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class LoginVo {
@NotBlank(message = "用户名不能为空")
private String username;
@NotBlank(message = "密码不能为空")
private String password;
}
9.mapper目录下新建AuthMapper.java文件
import com.example.demo.model.UserModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface AuthMapper {
@Select("SELECT * FROM `user` WHERE `username` = #{username}")
UserModel getUserByUsername(String username);
}
10.controller目录下新建AuthController.java文件
#import com.alibaba.fastjson.JSONObject;
import com.example.demo.Utils.ResultUtil;
import com.example.demo.mapper.AuthMapper;
import com.example.demo.model.UserModel;
import com.example.demo.model.Vo.LoginVo;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
@RestController
public class AuthController {
@Resource
AuthMapper authMapper;
@RequestMapping("login")
public Object login(@RequestBody @Valid LoginVo loginVo, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) {
ResultUtil resultUtil = new ResultUtil();
if (bindingResult.hasErrors()) {
resultUtil.dataValidation422(bindingResult.getFieldError().getDefaultMessage(), response);
return resultUtil;
}
UserModel userInfo = authMapper.getUserByUsername(loginVo.getUsername());
if (userInfo != null) {
if (userInfo.getPassword().equals(loginVo.getPassword())) {
userInfo.setPassword(null);
resultUtil.success(200, "登录成功", userInfo);
} else {
resultUtil.success(400, "登录失败,密码错误");
}
} else {
resultUtil.success(404, "登录失败,用户不存在");
}
return resultUtil;
}
}
11.DemoApplication.java文件修改
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
12.运行
13.用Postman新建请求(测试接口)
先创建新连接,再创建请求
新建请求
写入数据,将数据类型选为JSON
点击发送获得响应数据
数据库里没数据,所以失败
向user表中添加用户,再重新发送请求