SpringBoot+ MybatisPlus 实战——智慧校园管理系统
这个项目只有后端,没有前端部分,只适合用来复习SpringBoot后端知识和体系。
B站视频:https://www.bilibili.com/video/BV1AY4y1v7Xe/
https://pan.baidu.com/s/1efT4eY-dLBtNI-OTSUaWgA 提取码:yyds
一、项目简介
1.1 简介
智慧校园管理系统:主要是以年级、班级为单位,进行老师和学生信息记录和统计功能。项目采用前后端分离架构思想,前端采用 HTML+CSS+VUE来实现页面效果展示,后端采用 SpringBoot+MybatisPlus 框架实现数据存储等服务。存储层使用高性能的 MySQL,服务器使用 SpringBoot 内置的Tomcat9.x,项目构建工具使用Maven来管理jar包和项目构建。
1.2 项目模块
1.3 使用的技术
-
Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地new
一个对象,而是让Spring框架帮你来完成这一切。 -
SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。 -
mybatis-plus
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。MyBatis-plus
就是在MyBatis的基础上,为Mapper接口,Service层提供一些比较全面的CURD的业务逻辑功能,使程序员可以减少在Mapper和Service层的代码编写 -
MVC项目架构
页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据。
-
Swagger
Swagger 是一个规范且完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。 Swagger 的目标是对 REST API 定义一个标准且和语言无关的接口,可以让人和计算机拥有无须访问源码、文档或网络流量监测就可以发现和理解服务的能力。当通过 Swagger 进行正确定义,用户可以理解远程服务并使用最少实现逻辑与远程服务进行交互。与为底层编程所实现的接口类似,Swagger 消除了调用服务时可能会有的猜测。
Swagger 的优势:支持 API 自动生成同步的在线文档:使用 Swagger 后可以直接通过代码生成文档,不再需要自己手动编写接口文档了,对程序员来说非常方便,可以节约写文档的时间去学习新技术。提供 Web 页面在线测试 API:光有文档还不够,Swagger 生成的文档还支持在线测试。参数和格式都定好了,直接在界面上输入参数对应的值即可在线测试接口。
1.4 软件环境
二、环境搭建
2.1 使用IDEA的SpringBoot脚手架创建一个Maven项目
设置好包名及存放位置即可,无需添加其他功能,后面会对 pom.xml 大改,其他功能通过 maven 添加即可
2.2 在 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>
<!-- 继承方式使用SpringBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.atguigu</groupId>
<artifactId>myzhxy</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--单元测试启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- thymeleaf支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!-- 简化POJO实体类开发 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--swagger2 增强版接口文档-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
<!--开发者工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
<!-- JWT生成Token-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--spring boot maven插件 , 可以将项目运行依赖的jar包打到我们的项目中-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3 准备项目结构
-
java目录下
- config : 项目的配置类
- controller: 控制层
- mapper : 持久层接口
- pojo : 实体类
- service: 服务层
- util: 工具类包
- ZhxyApplication : 启动类
-
resources目录下
- mapper :持久层映射文件
- public/upload:文件上传目录
- static: 静态资源目录
- application.yml :SpringBoot核心配置文件
2.4 配置 application.yaml
server:
port: 8080 #指定当前项目启动的端口号
spring:
#解决SpringBoot2.6.0与swagger冲突问题【原因是在springboot2.6.0中将SpringMVC 默认路径匹配策略从AntPathMatcher 更改为PathPatternParser,导致出错,解决办法是切换回原先的AntPathMatcher】
mvc:
pathmatch:
matching-strategy: ant_path_matcher
#配置数据源
datasource:
#配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
#配置数据库连接属性
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/zhxy_db?characterEncoding=utf-8&useSSL=false #&serverTimezone=GMT%2B8
username: root
password: zyj123
hikari:
connection-test-query: SELECT 1
connection-timeout: 60000
idle-timeout: 500000
max-lifetime: 540000
maximum-pool-size: 12
minimum-idle: 10
pool-name: GuliHikariPool
thymeleaf:
#模板的模式,支持 HTML, XML TEXT JAVASCRIPT
mode: HTML5
#编码 可不用配置
encoding: UTF-8
#开发配置为false,避免修改模板还要重启服务器
cache: false
#配置模板路径,默认是templates,可以不用配置
prefix: classpath:/static/
jackson:
# 时间格式
date-format: yyyy-MM-dd HH:mm:ss
# 时区
time-zone: GMT+8
servlet:
#设置文件上传上限
multipart:
# 单个文件最大大小
max-file-size: 10MB
# 多个文件总共最大大小
max-request-size: 100MB
mybatis-plus:
configuration:
#添加日志支持
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:/mapper/**/*.xml
2.5 准备分页插件的配置类
源自mybatisplus
package com.atguigu.zhxy.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.zyj.myzhxy.mapper")
public class MyConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
//paginationInterceptor.setLimit(你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制);
return paginationInterceptor;
}
}
2.6 准备 swagger2 的配置类
若想查看,需访问 localhost:端口号/swagger-ui.html
只有有 Api 相关的注解才会生成
@Api 标注在类上,示例:@Api(tags = "年级控制器")
@ApiOperation 标注在方法上,示例:@ApiOperation("(批量)删除年级信息")
@ApiParam 标注在方法参数上,示例:@ApiParam("要删除的年级信息的id的JSON集合") @RequestBody List<Integer> ids
package com.atguigu.zhxy.config;
import com.google.common.base.Predicates;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* Swagger2配置信息
*/
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket webApiConfig(){
//添加head参数start
List<Parameter> pars = new ArrayList<>();
ParameterBuilder tokenPar = new ParameterBuilder();
tokenPar.name("userId")
.description("用户ID")
.defaultValue("1")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
pars.add(tokenPar.build());
ParameterBuilder tmpPar = new ParameterBuilder();
tmpPar.name("userTempId")
.description("临时用户ID")
.defaultValue("1")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
pars.add(tmpPar.build());
//添加head参数end
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
//可以测试请求头中:输入token
.apis(RequestHandlerSelectors.withClassAnnotation(ApiOperation.class))
//过滤掉admin路径下的所有页面
//.paths(Predicates.and(PathSelectors.regex("/sms/.*")))
//过滤掉所有error或error.*页面
//.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build()
.globalOperationParameters(pars);
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("atguigu", "http://atguigu.com", "512111559@qq.com"))
.build();
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("后台管理系统-API文档")
.description("本文档描述了后台管理系统微服务接口定义")
.version("1.0")
.contact(new Contact("atguigu", "http://atguigu.com", "512111559@qq.com"))
.build();
}
}
2.7、准备 util 下的工具类
验证码图片工具类
package com.atguigu.zhxy.util;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* @project: ssm_sms
* @description: 绘制验证码图片
*/
public class CreateVerifiCodeImage {
private static int WIDTH = 90;
private static int HEIGHT = 35;
private static int FONT_SIZE = 20; //字符大小
private static char[] verifiCode; //验证码
private static BufferedImage verifiCodeImage; //验证码图片
/**
* @description: 获取验证码图片
* @param: no
* @return: java.awt.image.BufferedImage
*/
public static BufferedImage getVerifiCodeImage() {
verifiCodeImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);// create a image
Graphics graphics = verifiCodeImage.getGraphics();
verifiCode = generateCheckCode();
drawBackground(graphics);
drawRands(graphics, verifiCode);
graphics.dispose();
return verifiCodeImage;
}
/**
* @description: 获取验证码
* @param: no
* @return: char[]
*/
public static char[] getVerifiCode() {
return verifiCode;
}
/**
* @description: 随机生成验证码
* @param: no
* @return: char[]
*/
private static char[] generateCheckCode() {
String chars = "0123456789abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] rands = new char[4];
for (int i = 0; i < 4; i++) {
int rand = (int) (Math.random() * (10 + 26 * 2));
rands[i] = chars.charAt(rand);
}
return rands;
}
/**
* @description: 绘制验证码
* @param: g
* @param: rands
* @return: void
*/
private static void drawRands(Graphics g, char[] rands) {
g.setFont(new Font("Console", Font.BOLD, FONT_SIZE));
for (int i = 0; i < rands.length; i++) {
g.setColor(getRandomColor());
g.drawString("" + rands[i], i * FONT_SIZE + 10, 25);
}
}
/**
* @description: 绘制验证码图片背景
* @param: g
* @return: void
*/
private static void drawBackground(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, WIDTH, HEIGHT);
// 绘制验证码干扰点
for (int i = 0; i < 200; i++) {
int x = (int) (Math.random() * WIDTH);
int y = (int) (Math.random() * HEIGHT);
g.setColor(getRandomColor());
g.drawOval(x, y, 1, 1);
}
}
/**
* @description: 获取随机颜色
* @param: no
* @return: java.awt.Color
*/
private static Color getRandomColor() {
Random ran = new Random();
return new Color(ran.nextInt(220), ran.nextInt(220), ran.nextInt(220));
}
}
token口令生成工具 JwtHelper
package com.atguigu.zhxy.util;
import io.jsonwebtoken.*;
import org.springframework.util.StringUtils;
import java.util.Date;
public class JwtHelper {
// 过期时间1天
private static long tokenExpiration = 24*60*60*1000;
// token信息后缀
private static String tokenSignKey = "123456";
//生成token字符串
public static String createToken(Long userId, Integer userType) {
String token = Jwts.builder()
.setSubject("YYGH-USER")
.setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
.claim("userId", userId)
// .claim("userName", userName)
.claim("userType", userType)
.signWith(SignatureAlgorithm.HS512, tokenSignKey)
.compressWith(CompressionCodecs.GZIP)
.compact();
return token;
}
//从token字符串获取userid
public static Long getUserId(String token) {
if(StringUtils.isEmpty(token)) return null;
Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
Claims claims = claimsJws.getBody();
Integer userId = (Integer)claims.get("userId");
return userId.longValue();
}
//从token字符串获取userType
public static Integer getUserType(String token) {
if(StringUtils.isEmpty(token)) return null;
Jws<Claims> claimsJws
= Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
Claims claims = claimsJws.getBody();
return (Integer)(claims.get("userType"));
}
//从token字符串获取userName
public static String getUserName(String token) {
if(StringUtils.isEmpty(token)) return "";
Jws<Claims> claimsJws
= Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
Claims claims = claimsJws.getBody();
return (String)claims.get("userName");
}
//判断token是否有效
public static boolean isExpiration(String token){
try {
boolean isExpire = Jwts.parser()
.setSigningKey(tokenSignKey)
.parseClaimsJws(token)
.getBody()
.getExpiration().before(new Date());
//没有过期,有效,返回false
return isExpire;
}catch(Exception e) {
//过期出现异常,返回true
return true;
}
}
/**
* 刷新Token
* @param token
* @return
*/
public String refreshToken(String token) {
String refreshedToken;
try {
final Claims claims = Jwts.parser()
.setSigningKey(tokenSignKey)
.parseClaimsJws(token)
.getBody();
refreshedToken = JwtHelper.createToken(getUserId(token), getUserType(token));
} catch (Exception e) {
refreshedToken = null;
}
return refreshedToken;
}
// 测试方法
// public static void main(String[] args) {
// String token = JwtHelper.createToken(1L, "lucy");
// System.out.println(token);
// System.out.println(JwtHelper.getUserId(token));
// System.out.println(JwtHelper.getUserName(token));
// }
}
解析request请求中的 token口令的工具AuthContextHolder
对JwtHelper进一步封装
package com.atguigu.zhxy.util;
import javax.servlet.http.HttpServletRequest;
public class AuthContextHolder {
//从请求头token获取userid
public static Long getUserIdToken(HttpServletRequest request) {
//从请求头token
String token = request.getHeader("token");
//调用工具类
Long userId = JwtHelper.getUserId(token);
return userId;
}
//从请求头token获取name
public static String getUserName(HttpServletRequest request) {
//从header获取token
String token = request.getHeader("token");
//jwt从token获取username
String userName = JwtHelper.getUserName(token);
return userName;
}
}
MD5加密工具类
明文转密文,进行加密
登录输入的是明文,数据库存储的是密文
package com.atguigu.zhxy.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public final class MD5 {
public static String encrypt(String strSrc) {
try {
char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f' };
byte[] bytes = strSrc.getBytes();
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(bytes);
bytes = md.digest();
int j = bytes.length;
char[] chars = new char[j * 2];
int k = 0;
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
chars[k++] = hexChars[b >>> 4 & 0xf];
chars[k++] = hexChars[b & 0xf];
}
return new String(chars);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException("MD5加密出错!!+" + e);
}
}
}
JSON响应结果格式封装类Result
后端在相应数据时,如果数据没有同一格式,将会非常混乱。
所以要定义一个公共规范,后端不论返回任何数据都要装到Result中,再去返回。
package com.atguigu.zhxy.util;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* 全局统一返回结果类
*
*/
@Data
@ApiModel(value = "全局统一返回结果")
public class Result<T> {
@ApiModelProperty(value = "返回码")
private Integer code;
@ApiModelProperty(value = "返回消息")
private String message;
@ApiModelProperty(value = "返回数据")
private T data;
public Result(){}
// 返回数据
protected static <T> Result<T> build(T data) {
Result<T> result = new Result<T>();
if (data != null)
result.setData(data);
return result;
}
// ResultCodeEnum 响应状态码的枚举
public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
Result<T> result = build(body);
result.setCode(resultCodeEnum.getCode());
result.setMessage(resultCodeEnum.getMessage());
return result;
}
public static<T> Result<T> ok(){
return Result.ok(null);
}
/**
* 操作成功
* @param data
* @param <T>
* @return
*/
public static<T> Result<T> ok(T data){
Result<T> result = build(data);
return build(data, ResultCodeEnum.SUCCESS);
}
public static<T> Result<T> fail(){
return Result.fail(null);
}
/**
* 操作失败
* @param data
* @param <T>
* @return
*/
public static<T> Result<T> fail(T data){
Result<T> result = build(data);
return build(data, ResultCodeEnum.FAIL);
}
public Result<T> message(String msg){
this.setMessage(msg);
return this;
}
public Result<T> code(Integer code){
this.setCode(code);
return this;
}
public boolean isOk() {
if(this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()) {
return true;
}
return false;
}
}
响应结果类型码枚举
package com.atguigu.zhxy.util;
import lombok.Getter;
/**
* 统一返回结果状态信息类
*
*/
@Getter
public enum ResultCodeEnum {
SUCCESS(200,"成功"),
FAIL(201, "失败"),
SERVICE_ERROR(2012, "服务异常"),
ILLEGAL_REQUEST( 204, "非法请求"),
PAY_RUN(205, "支付中"),
ARGUMENT_VALID_ERROR(206, "参数校验错误"),
LOGIN_ERROR(207, "用户名或密码错误"),
LOGIN_AUTH(208, "未登陆"),
PERMISSION(209, "没有权限"),
SECKILL_NO_START(210, "秒杀还没开始"),
SECKILL_RUN(211, "正在排队中"),
SECKILL_NO_PAY_ORDER(212, "您有未支付的订单"),
SECKILL_FINISH(213, "已售罄"),
SECKILL_END(214, "秒杀已结束"),
SECKILL_SUCCESS(215, "抢单成功"),
SECKILL_FAIL(216, "抢单失败"),
SECKILL_ILLEGAL(217, "请求不合法"),
SECKILL_ORDER_SUCCESS(218, "下单成功"),
COUPON_GET(220, "优惠券已经领取"),
COUPON_LIMIT_GET(221, "优惠券已发放完毕"),
//2022-02-22
LOGIN_CODE(222,"长时间未操作,会话已失效,请刷新页面后重试!"),
CODE_ERROR(223,"验证码错误!"),
TOKEN_ERROR(224,"Token无效!")
;
private Integer code;
private String message;
private ResultCodeEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}
文件上传工具类
package com.atguigu.zhxy.util;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* @project: zhxy
* @description: 上传文件的工具类
*/
public class UploadFile {
//存储文件上传失败的错误信息
private static Map<String, Object> error_result = new HashMap<>();
//存储头像的上传结果信息
private static Map<String, Object> upload_result = new HashMap<>();
/**
* @description: 效验所上传图片的大小及格式等信息...
* @param: photo
* @param: path
* @return: java.util.Map<java.lang.String, java.lang.Object>
*/
private static Map<String, Object> uploadPhoto(MultipartFile photo, String path) {
//限制头像大小(20M)
int MAX_SIZE = 20971520;
//获取图片的原始名称
String orginalName = photo.getOriginalFilename();
//如果保存文件的路径不存在,则创建该目录
File filePath = new File(path);
if (!filePath.exists()) {
filePath.mkdirs();
}
//限制上传文件的大小
if (photo.getSize() > MAX_SIZE) {
error_result.put("success", false);
error_result.put("msg", "上传的图片大小不能超过20M哟!");
return error_result;
}
// 限制上传的文件类型
String[] suffixs = new String[]{".png", ".PNG", ".jpg", ".JPG", ".jpeg", ".JPEG", ".gif", ".GIF", ".bmp", ".BMP"};
SuffixFileFilter suffixFileFilter = new SuffixFileFilter(suffixs);
if (!suffixFileFilter.accept(new File(path + orginalName))) {
error_result.put("success", false);
error_result.put("msg", "禁止上传此类型文件! 请上传图片哟!");
return error_result;
}
return null;
}
/**
* @description: (提取公共代码 : 提高代码的可重用性)获取头像的上传结果信息
* @param: photo
* @param: dirPaht
* @param: portraitPath
* @return: java.util.Map<java.lang.String, java.lang.Object>
*/
public static Map<String, Object> getUploadResult(MultipartFile photo, String dirPaht, String portraitPath) {
if (!photo.isEmpty() && photo.getSize() > 0) {
//获取图片的原始名称
String orginalName = photo.getOriginalFilename();
//上传图片,error_result:存储头像上传失败的错误信息
Map<String, Object> error_result = UploadFile.uploadPhoto(photo, dirPaht);
if (error_result != null) {
return error_result;
}
//使用UUID重命名图片名称(uuid__原始图片名称)
String newPhotoName = UUID.randomUUID() + "__" + orginalName;
//将上传的文件保存到目标目录下
try {
photo.transferTo(new File(dirPaht + newPhotoName));
upload_result.put("success", true);
upload_result.put("portrait_path", portraitPath + newPhotoName);//将存储头像的项目路径返回给页面
} catch (IOException e) {
e.printStackTrace();
upload_result.put("success", false);
upload_result.put("msg", "上传文件失败! 服务器端发生异常!");
return upload_result;
}
} else {
upload_result.put("success", false);
upload_result.put("msg", "头像上传失败! 未找到指定图片!");
}
return upload_result;
}
}
2.8 创建数据表及数据
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tb_admin
-- ----------------------------
DROP TABLE IF EXISTS `tb_admin`;
CREATE TABLE `tb_admin` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`gender` CHAR(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`email` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`telephone` VARCHAR(12) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`address` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`portrait_path` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = INNODB AUTO_INCREMENT = 157 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Records of tb_admin
-- ----------------------------
INSERT INTO `tb_admin` VALUES (101, 'admin', '女', '21232f297a57a5a743894a0e4a801fc3', '111111@qq.com', '13260161111', '昌平', 'upload/default.jpg');
INSERT INTO `tb_admin` VALUES (102, 'admin1', '男', '21232f297a57a5a743894a0e4a801fc3', '512111559@qq.com', '13260166090', '北京', 'upload/default.jpg');
INSERT INTO `tb_admin` VALUES (103, 'admin2', '男', '21232f297a57a5a743894a0e4a801fc3', '512111559@qq.com', '13260166090', '北京', 'upload/default.jpg');
INSERT INTO `tb_admin` VALUES (104, 'admin3', '男', '21232f297a57a5a743894a0e4a801fc3', '123456@qq.com', '13666666666', '宏福苑', 'upload/default.jpg');
-- ----------------------------
-- Table structure for tb_clazz
-- ----------------------------
DROP TABLE IF EXISTS `tb_clazz`;
CREATE TABLE `tb_clazz` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`number` INT(3) NULL DEFAULT NULL,
`introducation` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`headmaster` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`email` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`telephone` VARCHAR(12) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`grade_name` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = INNODB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Records of tb_clazz
-- ----------------------------
INSERT INTO `tb_clazz` VALUES (1, '一年一班', 30, '大圣的一年一班好', '大圣', 'dasheng@163.com', '13260166090', '一年级');
INSERT INTO `tb_clazz` VALUES (2, '一年二班', 28, '小张的一年二班好', '小张', 'xiaozhang@163.com', '13260166090', '一年级');
INSERT INTO `tb_clazz` VALUES (3, '二年一班', 35, '小韩的二年一班好', '小韩', 'xiaohan@163.com', '13260166090', '二年级');
INSERT INTO `tb_clazz` VALUES (4, '二年二班', 30, '小强的二年二班好', '小强', 'xiaoqiang@163.com', '13260166090', '二年级');
INSERT INTO `tb_clazz` VALUES (5, '三年一班', 30, '小花的三年一班好', '小花', 'xiaohua@163.com', '13260166090', '三年级');
INSERT INTO `tb_clazz` VALUES (6, '三年二班', 30, '小赵的三年二班好', '小赵', 'xiaozhao@163.com', '13260166090', '三年级');
INSERT INTO `tb_clazz` VALUES (7, '四年一班', 30, '小赵的三年二班好', '小飞', 'xiaofei@163.com', '13260166090', '四年级');
-- ----------------------------
-- Table structure for tb_grade
-- ----------------------------
DROP TABLE IF EXISTS `tb_grade`;
CREATE TABLE `tb_grade` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
`manager` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`email` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`telephone` VARCHAR(12) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`introducation` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`, `name`) USING BTREE
) ENGINE = INNODB AUTO_INCREMENT = 12 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Records of tb_grade
-- ----------------------------
INSERT INTO `tb_grade` VALUES (1, '一年级', '大圣', 'dasheng@163.com', '13260166090', '大学一年级');
INSERT INTO `tb_grade` VALUES (2, '二年级', '小魏', 'xiaowei@163.com', '13260166090', '大学二年级');
INSERT INTO `tb_grade` VALUES (3, '三年级', '小李', 'xiaoli@163.com', '13666666666', '三年级,这个班级的孩子们很有才艺');
INSERT INTO `tb_grade` VALUES (4, '五年级', '小丽', 'li@123.com', '13666666666', '这个年级的同学多才多活力');
INSERT INTO `tb_grade` VALUES (5, '六年级', '小明', 'xiaoming@666.com', '13666666666', '这个年级的主任是小明');
-- ----------------------------
-- Table structure for tb_student
-- ----------------------------
DROP TABLE IF EXISTS `tb_student`;
CREATE TABLE `tb_student` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`sno` VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`name` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`gender` CHAR(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`email` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`telephone` VARCHAR(12) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`address` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`introducation` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`portrait_path` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`clazz_name` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = INNODB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Records of tb_student
-- ----------------------------
INSERT INTO `tb_student` VALUES (1, '1001', '张小明', '男', 'e10adc3949ba59abbe56e057f20f883e', 'yinyufei@163.com', '13260166090', '北京天通苑', '这个学生学习好', 'upload/default.jpg', '一年一班');
INSERT INTO `tb_student` VALUES (2, '1002', '郭建超', '男', 'e10adc3949ba59abbe56e057f20f883e', 'guojianchao@163.com', '13260166090', '北京昌平', '这个学生会功夫', 'upload/default.jpg', '一年一班');
INSERT INTO `tb_student` VALUES (3, '1003', '史汶鑫', '男', 'e10adc3949ba59abbe56e057f20f883e', 'shiwenxin@163.com', '13260166090', '北京昌平', '这个学生酒量好', 'upload/default.jpg', '二年一班');
INSERT INTO `tb_student` VALUES (4, '1004', '高建军', '男', 'e10adc3949ba59abbe56e057f20f883e', 'gaojianjun@163.com', '13260166090', '北京昌平', '这个学生会做饭', 'upload/default.jpg', '二年一班');
INSERT INTO `tb_student` VALUES (5, '1005', '邹伟斌', '男', 'e10adc3949ba59abbe56e057f20f883e', 'zouweibin@163.com', '13260166090', '北京昌平', '这个学生能吃辣', 'upload/default.jpg', '三年一班');
INSERT INTO `tb_student` VALUES (6, '1006', '刘路', '男', 'e10adc3949ba59abbe56e057f20f883e', 'liulu@163.com', '13260166090', '北京昌平', '这个学生是学霸', 'upload/default.jpg', '三年二班');
INSERT INTO `tb_student` VALUES (7, '1007', '庞家仨', '女', 'e10adc3949ba59abbe56e057f20f883e', 'pangjiasan@163.com', '13260166090', '北京昌平', '这个学生海拔高', 'upload/default.jpg', '三年二班');
INSERT INTO `tb_student` VALUES (8, '1008', '谭帅', '男', 'e10adc3949ba59abbe56e057f20f883e', 'tanshuai@163.com', '13260166090', '北京昌平', '这个学生想考研', 'upload/default.jpg', '四年一班');
-- ----------------------------
-- Table structure for tb_teacher
-- ----------------------------
DROP TABLE IF EXISTS `tb_teacher`;
CREATE TABLE `tb_teacher` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`tno` VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`name` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`gender` CHAR(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`email` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`telephone` VARCHAR(12) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`address` VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`portrait_path` VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`clazz_name` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = INNODB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Records of tb_teacher
-- ----------------------------
INSERT INTO `tb_teacher` VALUES (1, '101', '大圣', '女', '123456', 'dasheng@163.com', '13260166090', '北京昌平', 'upload/default.jpg', '一年一班');
INSERT INTO `tb_teacher` VALUES (2, '102', '小张', '男', 'e10adc3949ba59abbe56e057f20f883e', 'xiaozhang@163.com', '13260166090', '北京海淀', 'upload/default.jpg', '一年二班');
INSERT INTO `tb_teacher` VALUES (3, '103', '小韩', '男', 'e10adc3949ba59abbe56e057f20f883e', 'xiaohan@163.com', '13260166090', '北京朝阳', 'upload/default.jpg', '二年一班');
INSERT INTO `tb_teacher` VALUES (4, '104', '小强', '男', 'e10adc3949ba59abbe56e057f20f883e', 'xiaoqiang@163.com', '13260166090', '北京通州', 'upload/default.jpg', '二年二班');
INSERT INTO `tb_teacher` VALUES (5, '105', '小花', '男', 'e10adc3949ba59abbe56e057f20f883e', 'xiaohua@163.com', '13260166090', '北京顺义', 'upload/default.jpg', '三年一班');
INSERT INTO `tb_teacher` VALUES (6, '106', '小赵', '男', 'e10adc3949ba59abbe56e057f20f883e', 'xiaozhao@163.com', '13260166090', '北京东城', 'upload/default.jpg', '三年二班');
INSERT INTO `tb_teacher` VALUES (7, '107', '小飞', '男', 'e10adc3949ba59abbe56e057f20f883e', 'xiaofei@163.com', '13260166090', '北京西城', 'upload/default.jpg', '四年一班');
INSERT INTO `tb_teacher` VALUES (8, '108', '秀秀', '女', 'e10adc3949ba59abbe56e057f20f883e', '123456@123.com', '13855555555', '海淀', 'upload/12fe07bf8b6d493b96294f1ef0552339default.jpg', '一年一班');
SET FOREIGN_KEY_CHECKS = 1;
2.9 准备各层基础代码
这里只写 admin,其他类似
这里只是演示一个框架,具体实现内容参见后面内容
pojo
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("tb_admin")
public class Admin {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private Character gender;
private String password;
private String email;
private String telephone;
private String address;
private String portraitPath; // 头像图片路径
}
mapper
@Mapper
public interface AdminMapper extends BaseMapper<Admin> {
}
service
接口
public interface AdminService extends IService<Admin> {
}
接口实现类
@Service("adminServiceImpl")
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
}
controller
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {
}
三、登录功能实现
3.1 登录的过程
登录需要实现两个请求处理
第一次需要处理表单的内容,校验登录信息
第二次请求,会发送第一次请求的 token,需要处理这个 token,从而从登录页跳转到首页
3.2 验证码功能实现
在 SystemController 添加相应的方法
@Api(tags = "系统控制器")
@RestController
@RequestMapping("/sms/system")
public class SystemController {
//获取验证码图片
@GetMapping("/getVerifiCodeImage")
public void getVerifiCodeImage(HttpServletRequest request, HttpServletResponse response){
// 获取图片
BufferedImage verifiCodeImage = CreateVerifiCodeImage.getVerifiCodeImage();
// 获取图片上的验证码
char[] verifiCodeChars = CreateVerifiCodeImage.getVerifiCode();
String verifiCode = new String(verifiCodeChars);
// 将验证码文本放入session域,为下一次验证做准备
HttpSession session = request.getSession();
session.setAttribute("verifiCode", verifiCode);
try {
// 将验证码图片响应给浏览器
ServletOutputStream outputStream = response.getOutputStream();
// 将verifiCodeImage以JPEG的格式写到outputStream流中
ImageIO.write(verifiCodeImage,"JPEG",outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.3 登录校验功能实现
在 service 及其实现类添加登录验证方法
AdminService
public interface AdminService extends IService<Admin> {
/**
* 登录校验,查询提交的表单是否有此人
* @param loginForm 登录页提交的表单
* @return
*/
Admin login(LoginForm loginForm);
}
AdminServiceImpl
@Service("adminServiceImpl")
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
/**
* 登录验证,查询提交的表单是否有此人
* @param loginForm 登录页提交的表单
* @return
*/
@Override
public Admin login(LoginForm loginForm) {
QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name",loginForm.getUsername())
// 注意将密码转为密文
.eq("password", MD5.encrypt(loginForm.getPassword()));
Admin admin = baseMapper.selectOne(queryWrapper);
return admin;
}
}
StudentService
public interface StudentService extends IService<Student> {
/**
* 登录校验,查询提交的表单是否有此人
* @param loginForm 登录页提交的表单
* @return
*/
Student login(LoginForm loginForm);
}
StudentServiceImpl
@Service("stuService")
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
/**
* 登录校验,查询提交的表单是否有此人
* @param loginForm 登录页提交的表单
* @return
*/
@Override
public Student login(LoginForm loginForm) {
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", loginForm.getUsername())
// 注意将密码转为密文
.eq("password", MD5.encrypt(loginForm.getPassword()));
Student student = baseMapper.selectOne(queryWrapper);
return student;
}
}
TeacherService
public interface TeacherService extends IService<Teacher> {
/**
* 登录校验,查询提交的表单是否有此人
* @param loginForm 登录页提交的表单
* @return
*/
Teacher login(LoginForm loginForm);
}
TeacherServiceImpl
@Service("teaService")
@Transactional
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
/**
* 登录校验,查询提交的表单是否有此人
* @param loginForm 登录页提交的表单
* @return
*/
@Override
public Teacher login(LoginForm loginForm) {
QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", loginForm.getUsername())
// 注意将密码转为密文
.eq("password", MD5.encrypt(loginForm.getPassword()));
Teacher teacher = baseMapper.selectOne(queryWrapper);
return teacher;
}
}
controller 中的方法
@RestController
@RequestMapping("/sms/system")
public class SystemController {
@Autowired
private AdminService adminService;
@Autowired
private StudentService studentService;
@Autowired
private TeacherService teacherService;
/**
* 验证码功能的实现
*
* @param request
* @param response
*/
@GetMapping("/getVerifiCodeImage")
public void getVerifiCodeImage(HttpServletRequest request, HttpServletResponse response) {
// 获取图片
BufferedImage verifiCodeImage = CreateVerifiCodeImage.getVerifiCodeImage();
// 获取图片上的验证码
char[] verifiCodeChars = CreateVerifiCodeImage.getVerifiCode();
String verifiCode = new String(verifiCodeChars);
// 将验证码文本放入session域,为下一次验证做准备
HttpSession session = request.getSession();
session.setAttribute("verifiCode", verifiCode);
try {
// 将验证码图片响应给浏览器
ServletOutputStream outputStream = response.getOutputStream();
// 将verifiCodeImage以JPEG的格式写到outputStream流中
ImageIO.write(verifiCodeImage, "JPEG", outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 登录验证,查询提交的表单是否有效
* @param loginForm 登录页提交的表单
* @param request
* @return
*/
@PostMapping("/login")
public Result login(@RequestBody LoginForm loginForm, HttpServletRequest request) {
// 验证码校验
HttpSession session = request.getSession();
String sessionVerifiCode = (String) session.getAttribute("verifiCode");
String loginVerifiCode = loginForm.getVerifiCode();
if ("".equals(sessionVerifiCode) || null == sessionVerifiCode) {
return Result.fail().message("验证码失效,请刷新后重试");
}
if (!sessionVerifiCode.equalsIgnoreCase(loginVerifiCode)) {
return Result.fail().message("验证码错误");
}
// 从session中移除现有验证码记录
session.removeAttribute("verifiCode");
// 对用户类型进行校验
Map<String, Object> map = new LinkedHashMap<>(); // 用于存放响应的数据
switch (loginForm.getUserType()) {
case 1:
try {
Admin admin = adminService.login(loginForm);
if (null != admin) {
// 在数据库找到该用户,将用户类型和用户id转换为密文,以token向客户端返回信息
map.put("token", JwtHelper.createToken(admin.getId().longValue(), 1));
} else {
throw new RuntimeException("用户名或密码错误");
}
// 若没有异常,说明可以登录,返回结果
return Result.ok(map);
} catch (RuntimeException e) {
e.printStackTrace();
// 产生异常,返回出错信息
return Result.fail().message(e.getMessage());
}
case 2:
try {
Student student = studentService.login(loginForm);
if (null != student) {
// 在数据库找到该用户,将用户类型和用户id转换为密文,以token向客户端返回信息
map.put("token", JwtHelper.createToken(student.getId().longValue(), 2));
} else {
throw new RuntimeException("用户名或密码错误");
}
// 若没有异常,说明可以登录,返回结果
return Result.ok(map);
} catch (RuntimeException e) {
e.printStackTrace();
// 产生异常,返回出错信息
return Result.fail().message(e.getMessage());
}
case 3:
try {
Teacher teacher = teacherService.login(loginForm);
if (null != teacher) {
// 在数据库找到该用户,将用户类型和用户id转换为密文,以token向客户端返回信息
map.put("token", JwtHelper.createToken(teacher.getId().longValue(), 3));
} else {
throw new RuntimeException("用户名或密码错误");
}
// 若没有异常,说明可以登录,返回结果
return Result.ok(map);
} catch (RuntimeException e) {
e.printStackTrace();
// 产生异常,返回出错信息
return Result.fail().message(e.getMessage());
}
}
return Result.fail().message("查无此用户");
}
}
3.4 登录校验后从登录页跳转到首页
当验证通过后,前端会产生第二个请求,用以获取用户的类型,然后根据用户的类型来展现不同的页面,所以后端要有一个根据token解析用户类型并做出结果响应的控制层
完成 service 方法及其实现类
AdminService
/**
* 根据用户id查找用户
* @param userId
* @return
*/
Admin getAdminById(Long userId);
AdminServiceImpl
/**
* 根据用户id查找用户
* @param userId
* @return
*/
@Override
public Admin getAdminById(Long userId) {
Admin admin = baseMapper.selectById(userId);
return admin;
}
StudentService
/**
* 根据用户id查找用户
* @param userId
* @return
*/
Student getStdentById(Long userId);
StudentServiceImpl
/**
* 根据用户id查找用户
* @param userId
* @return
*/
@Override
public Student getStdentById(Long userId) {
Student student = baseMapper.selectById(userId);
return student;
}
TeacherService
/**
* 根据用户id查找用户
* @param userId
* @return
*/
Teacher getTeacherById(Long userId);
TeacherServiceImpl
/**
* 根据用户id查找用户
* @param userId
* @return
*/
@Override
public Teacher getTeacherById(Long userId) {
Teacher teacher = baseMapper.selectById(userId);
return teacher;
}
完成 controller 方法
@Api(tags = "系统控制器")
@RestController
@RequestMapping("/sms/system")
public class SystemController {
@Autowired
private AdminService adService;
@Autowired
private StudentService studentService;
@Autowired
private TeacherService teacherService;
@ApiOperation("通过token获取用户信息")
@GetMapping("/getInfo")
public Result getUserInfoByToken(HttpServletRequest request, @RequestHeader("token")String token){
// 获取用户中请求的token
// 检查token 是否过期 20H
boolean isEx = JwtHelper.isExpiration(token);
if (isEx) {
return Result.build(null, ResultCodeEnum.TOKEN_ERROR);
}
// 解析token,获取用户id和用户类型
Long userId =JwtHelper.getUserId(token);
Integer userType =JwtHelper.getUserType(token);
// 准备一个Map集合用于存响应的数据
Map<String,Object> map=new HashMap<>();
switch (userType){
case 1:
Admin admin = adService.getAdminById(userId.intValue());
map.put("user",admin);
map.put("userType",1);
break;
case 2:
Student student = studentService.getStudentById(userId.intValue());
map.put("user",student);
map.put("userType",1);
break;
case 3:
Teacher teacher = teacherService.getTeacherById(userId.intValue());
map.put("user",teacher);
map.put("userType",1);
break;
}
return Result.ok(map);
}
}
四 、年级管理功能实现
4.1 查询年级信息(分页带条件)
Controller层代码
@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {
@Autowired
private GradeService gradeService;
@ApiOperation("查询年级信息,分页带条件")
@GetMapping("/getGrades/{pageNo}/{pageSize}")
public Result getGradeByOpr(
@ApiParam("分页查询页码数") @PathVariable(value = "pageNo") Integer pageNo, // 页码数
@ApiParam("分页查询页大小") @PathVariable(value = "pageSize")Integer pageSize, // 页大小
@ApiParam("分页查询模糊匹配班级名") String gradeName)// 模糊查询条件
{
// 设置分页信息
Page<Grade> page =new Page<>(pageNo,pageSize);
// 调用服务层方法,传入分页信息,和查询的条件
IPage<Grade> pageRs = gradeService.getGradeByOpr(page, gradeName);
return Result.ok(pageRs);
}
}
Service层代码
@Service
@Transactional
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {
@Override
public IPage<Grade> getGradeByOpr(Page<Grade> pageParam, String gradeName) {
// 设置查询条件
QueryWrapper queryWrapper = new QueryWrapper();
// 判断gradeName是否非空
if(!StringUtils.isEmpty(gradeName)){
queryWrapper.like("name",gradeName);
}
// 设置排序规则
queryWrapper.orderByDesc("id");
queryWrapper.orderByAsc("name");
// 分页查询数据
Page page = baseMapper.selectPage(pageParam, queryWrapper);
return page;
}
}
持久层代码
// MyBatisPlus 的BaseMapper已经封装好分页带条件查询方法
<E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
4.2 添加和修改年级信息
Controller层代码
@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {
@Autowired
private GradeService gradeService;
@ApiOperation("添加或者修改年级信息")
@PostMapping("/saveOrUpdateGrade")
public Result saveOrUpdateGrade(
@ApiParam("JSON的grade对象转换后台数据模型")@RequestBody Grade grade
){
// 调用服务层方法,实现添加或者修改年级信息
gradeService.saveOrUpdate(grade);
return Result.ok();
}
}
service层代码
由于 mybatisplus 提供的 IService 有 saveOrUpdate 方法,所以不用写 service 层的方法
mybatisplus 似乎入侵到了 控制层和服务层???
// MyBatisPlus源码可见
public boolean saveOrUpdate(T entity) {
if (null == entity) {
return false;
} else {
Class<?> cls = entity.getClass();
TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!", new Object[0]);
String keyProperty = tableInfo.getKeyProperty();
Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!", new Object[0]);
Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
return !StringUtils.checkValNull(idVal) && !Objects.isNull(this.getById((Serializable)idVal)) ? this.updateById(entity) : this.save(entity);
}
}
持久层代码
略
4.3 删除和批量删除年级信息
Controller层代码
@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {
@Autowired
private GradeService gradeService;
@ApiOperation("删除一个或者多个grade信息")
@DeleteMapping("/deleteGrade")
public Result deleteGradeById(
@ApiParam("JSON的年级id集合,映射为后台List<Integer>")@RequestBody List<Integer> ids
)
{
gradeService.removeByIds(ids);
return Result.ok();
}
}
由于 mybatisplus 提供的 IService 有 removeByIds 方法,所以不用写 service 层的方法
五 、班级管理功能实现
5.1 回显搜索条件中的班级选项
Controller层代码
@Api(tags = "班级控制器")
@RestController
@RequestMapping("/sms/clazzController")
public class ClazzController {
@Autowired
private ClazzService clazzService;
@ApiOperation("获取所有班级的JSON")
@GetMapping("/getClazzs")
public Result getClazzs(){
List<Clazz> clazzList = clazzService.getClazzs();
return Result.ok(clazzList);
}
}
service层代码
@Override
public List<Clazz> getClazzs() {
return baseMapper.selectList(null);
}
5.2 查询班级信息(分页带条件)
请求路径为:/sms/clazzController/getClazzsByOpr/{pageNo}/{pageSize}?gradeName=xxx&name=xxx
Controller层代码
@Api(tags = "班级控制器")
@RestController
@RequestMapping("/sms/clazzController")
public class ClazzController {
@Autowired
private ClazzService clazzService;
@ApiOperation("查询班级信息,分页带条件")
@GetMapping("/getClazzsByOpr/{pageNo}/{pageSize}")
public Result getClazzsByOpr(
@ApiParam("页码数") @PathVariable("pageNo") Integer pageNo,
@ApiParam("页大小") @PathVariable("pageSize") Integer pageSize,
@ApiParam("查询条件") Clazz clazz
){
//设置分页信息
Page<Clazz> page =new Page<>(pageNo,pageSize);
IPage<Clazz> iPage = clazzService.getClazzsByOpr(page, clazz);
return Result.ok(iPage);
}
}
service层代码
@Service
@Transactional
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
/**
* 分页查询所有班级信息【带条件】
* @param clazz
* @return
*/
@Override
public IPage<Clazz> getClazzsByOpr(Page<Clazz> pageParam,Clazz clazz) {
QueryWrapper queryWrapper = new QueryWrapper();
if(clazz != null){
//年级名称条件
String gradeName = clazz.getGradeName();
if(!StringUtils.isEmpty(gradeName)){
queryWrapper.eq("grade_name",gradeName);
}
//班级名称条件
String clazzName = clazz.getName();
if(!StringUtils.isEmpty(clazzName)){
queryWrapper.like("name",clazzName);
}
queryWrapper.orderByDesc("id");
queryWrapper.orderByAsc("name");
}
Page page = baseMapper.selectPage(pageParam, queryWrapper);
return page;
}
}
5.3 添加和修改班级信息
Controller层代码
@Api(tags = "班级控制器")
@RestController
@RequestMapping("/sms/clazzController")
public class ClazzController {
@Autowired
private ClazzService clazzService;
@ApiOperation("保存或者修改班级信息")
@PostMapping("/saveOrUpdateClazz")
public Result saveOrUpdateClazz(
@ApiParam("JSON转换后端Clazz数据模型") @RequestBody Clazz clazz
){
clazzService.saveOrUpdate(clazz);
return Result.ok();
}
}
service层代码
由于 mybatisplus 提供的 IService 有 saveOrUpdate 方法,所以不用写 service 层的方法
5.4 删除和批量删除班级信息
Controller层代码
@Api(tags = "班级控制器")
@RestController
@RequestMapping("/sms/clazzController")
public class ClazzController {
@Autowired
private ClazzService clazzService;
@ApiOperation("删除一个或者多个班级信息")
@DeleteMapping("/deleteClazz")
public Result deleteClazzByIds(
@ApiParam("多个班级id的JSON") @RequestBody List<Integer> ids
){
clazzService.removeByIds(ids);
return Result.ok();
}
}
由于 mybatisplus 提供的 IService 有 removeByIds 方法,所以不用写 service 层的方法
六、学生管理功能实现
6.1 回显搜索条件中的班级选项
Controller层代码
@Api(tags = "班级控制器")
@RestController
@RequestMapping("/sms/clazzController")
public class ClazzController {
@Autowired
private ClazzService clazzService;
@ApiOperation("获取所有班级的JSON")
@GetMapping("/getClazzs")
public Result getClazzs(){
List<Clazz> clazzList = clazzService.getClazzs();
return Result.ok(clazzList);
}
}
6.2 查询学生信息(分页带条件)
Controller层代码
@Api(tags = "学生控制器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {
@Autowired
private StudentService studentService;
@ApiOperation("查询学生信息,分页带条件")
@GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
public Result getStudentsByOpr(
@ApiParam("页码数") @PathVariable("pageNo")Integer pageNo,
@ApiParam("页大小") @PathVariable("pageSize")Integer pageSize,
@ApiParam("查询条件转换后端数据模型") Student student
){
// 准备分页信息封装的page对象
Page<Student> page =new Page<>(pageNo,pageSize);
// 获取分页的学生信息
IPage<Student> iPage = studentService.getStudentByOpr(page, student);
// 返回学生信息
return Result.ok(iPage);
}
}
service层代码
@Service("stuService")
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
/**
* 按条件查询学生信息【带分页】
*/
public IPage<Student> getStudentByOpr(Page<Student> pageParam,Student student){
QueryWrapper<Student> queryWrapper = null;
if(student != null) {
queryWrapper = new QueryWrapper<>();
if (student.getClazzName() != null) {
queryWrapper.eq("clazz_name", student.getClazzName());
}
if (student.getName() != null) {
queryWrapper.like("name", student.getName());
}
queryWrapper.orderByDesc("id");
queryWrapper.orderByAsc("name");
}
//创建分页对象
IPage<Student> pages = baseMapper.selectPage(pageParam, queryWrapper);
return pages;
}
6.3 异步图片上传处理头像
@Api(tags = "系统控制器")
@RestController
@RequestMapping("/sms/system")
public class SystemController {
@ApiOperation("头像上传统一入口")
@P("/headerImgUpload")
public Result headerImgUpload(
@ApiParam("文件二进制数据") @RequestPart("multipartFile") MultipartFile multipartFile
){
//使用UUID随机生成文件名
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
//生成新的文件名字
String filename = uuid.concat(multipartFile.getOriginalFilename());
//生成文件的保存路径(实际生产环境这里会使用真正的文件存储服务器)
String portraitPath ="c:/code/zhxy/target/classes/public/upload/".concat(filename);
//保存文件
try {
multipartFile.transferTo(new File(portraitPath));
} catch (IOException e) {
e.printStackTrace();
}
String headerImg ="upload/"+filename;
return Result.ok(headerImg);
}
}
6.4 添加和修改学生信息
Controller层代码
@Api(tags = "学生控制器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {
@Autowired
private StudentService studentService;
@ApiOperation("增加学生信息")
@PostMapping("/addOrUpdateStudent")
public Result addOrUpdateStudent(@RequestBody Student student){
//对学生的密码进行加密
if (!Strings.isEmpty(student.getPassword())) {
student.setPassword(MD5.encrypt(student.getPassword()));
}
//保存学生信息进入数据库
studentService.saveOrUpdate(student);
return Result.ok();
}
}
6.5 删除和批量删除学生信息
Controller层代码
@Api(tags = "学生控制器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {
@Autowired
private StudentService studentService;
@ApiOperation("增加学生信息")
@PostMapping("/addOrUpdateStudent")
public Result addOrUpdateStudent(@RequestBody Student student){
//对学生的密码进行加密
if (!Strings.isEmpty(student.getPassword())) {
student.setPassword(MD5.encrypt(student.getPassword()));
}
//保存学生信息进入数据库
studentService.saveOrUpdate(student);
return Result.ok();
}
}
七、教师管理功能实现
7.1 查询教师信息(分页带条件)
Controller层代码
@Api(tags = "教师信息管理控制器")
@RestController
@RequestMapping("/sms/teacherController")
public class TeacherController {
@Autowired
private TeacherService teacherService;
@ApiOperation("获取教师信息,分页带条件")
@GetMapping("/getTeachers/{pageNo}/{pageSize}")
public Result getTeachers(
@PathVariable("pageNo") Integer pageNo,
@PathVariable("pageSize") Integer pageSize,
Teacher teacher
){
Page<Teacher> pageParam = new Page<>(pageNo,pageSize);
IPage<Teacher> page = teacherService.getTeachersByOpr(pageParam, teacher);
return Result.ok(page);
}
}
service层代码
@Override
public IPage<Teacher> getTeachersByOpr(Page<Teacher> pageParam, Teacher teacher) {
QueryWrapper queryWrapper = new QueryWrapper();
if(teacher != null){
//班级名称条件
String clazzName = teacher.getClazzName();
if (!StringUtils.isEmpty(clazzName)) {
queryWrapper.eq("clazz_name",clazzName);
}
//教师名称条件
String teacherName = teacher.getName();
if(!StringUtils.isEmpty(teacherName)){
queryWrapper.like("name",teacherName);
}
queryWrapper.orderByDesc("id");
queryWrapper.orderByAsc("name");
}
IPage<Teacher> page = baseMapper.selectPage(pageParam, queryWrapper);
return page;
}
7.2 添加和修改教师信息
Controller层代码
@ApiOperation("添加和修改教师信息")
@PostMapping("/saveOrUpdateTeacher")
public Result saveOrUpdateTeacher(
@RequestBody Teacher teacher
){
teacherService.saveOrUpdate(teacher);
return Result.ok();
}
7.3 删除和批量删除教师信息
Controller层代码
@ApiOperation("删除一个或者多个教师信息")
@DeleteMapping("/deleteTeacher")
public Result deleteTeacher(
@RequestBody List<Integer> ids
){
teacherService.removeByIds(ids);
return Result.ok();
}
八 、管理员管理功能实现
8.1 查询管理员信息(分页带条件)
Request URL: /sms/adminController/getAllAdmin/{pageNo}/{pageSize}?adminName=%E4%B8%89
Request Method: GET
Controller层代码
@Api(tags = "系统管理员控制器")
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {
@Autowired
private AdminService adService;
@ApiOperation("分页获取所有Admin信息【带条件】")
@GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
public Result getAllAdmin(@PathVariable Integer pageNo,
@PathVariable Integer pageSize,
String adminName){
Page<Admin> pageParam = new Page<>(pageNo,pageSize);
IPage<Admin> page = adService.getAdmins(pageParam, adminName);
return Result.ok(page);
}
}
service层代码
@Override
public IPage<Admin> getAdmins(Page<Admin> pageParam,String adminName) {
QueryWrapper queryWrapper = new QueryWrapper();
if(!StringUtils.isEmpty(adminName)){
queryWrapper.like("name",adminName);
}
queryWrapper.orderByDesc("id");
queryWrapper.orderByAsc("name");
Page page = baseMapper.selectPage(pageParam, queryWrapper);
return page;
}
8.2 添加和修改管理员信息
Controller层代码
@ApiOperation("添加或修改Admin信息")
@PostMapping("/saveOrUpdateAdmin")
public Result saveOrUpdateAdmin(@RequestBody Admin admin){
if (!Strings.isEmpty(admin.getPassword())) {
admin.setPassword(MD5.encrypt(admin.getPassword()));
}
adService.saveOrUpdate(admin);
return Result.ok();
}
8.3 删除和批量删除管理员信息
Request URL: /sms/adminController/deleteAdmin
Request Method: DELETE
Controller层代码
@ApiOperation("删除Admin信息")
@DeleteMapping("/deleteAdmin")
public Result deleteAdmin(@RequestBody List<Integer> ids){
adService.removeByIds(ids);
return Result.ok();
}
8.4 管理员修改自己的密码
Request URL: sms/system/updatePwd/{oldPwd}/{newPwd}
Request Method: POST
Controller层代码
@Api(tags = "系统控制器")
@RestController
@RequestMapping("/sms/system")
public class SystemController {
@ApiOperation("修改密码")
@PostMapping("/updatePwd/{oldPwd}/{newPwd}")
public Result updatePwd(@RequestHeader("token") String token,
@PathVariable("oldPwd") String oldPwd,
@PathVariable("newPwd") String newPwd){
boolean yOn = JwtHelper.isExpiration(token);
if(yOn){
//token过期
return Result.fail().message("token失效!");
}
//通过token获取当前登录的用户id
Long userId = JwtHelper.getUserId(token);
//通过token获取当前登录的用户类型
Integer userType = JwtHelper.getUserType(token);
// 将明文密码转换为暗文
oldPwd=MD5.encrypt(oldPwd);
newPwd= MD5.encrypt(newPwd);
if(userType == 1){
QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("id",userId.intValue()).eq("password",oldPwd);
Admin admin = adService.getOne(queryWrapper);
if (null!=admin) {
admin.setPassword(newPwd);
adService.saveOrUpdate(admin);
}else{
return Result.fail().message("原密码输入有误!");
}
}else if(userType == 2){
QueryWrapper<Student> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("id",userId.intValue()).eq("password",oldPwd);
Student student = studentService.getOne(queryWrapper);
if (null!=student) {
student.setPassword(newPwd);
studentService.saveOrUpdate(student);
}else{
return Result.fail().message("原密码输入有误!");
}
}
else if(userType == 3){
QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("id",userId.intValue()).eq("password",oldPwd);
Teacher teacher = teacherService.getOne(queryWrapper);
if (null!=teacher) {
teacher.setPassword(newPwd);
teacherService.saveOrUpdate(teacher);
}else{
return Result.fail().message("原密码输入有误!");
}
}
return Result.ok();
}
}