文章目录
前端:Freemarker、jQuery
后端:SpringBoot、MyBatisPlus、Lombok
中间件:Redis

用户表:t_user
订单项表:t_order_item
数据项表:t_dict_data

商品表:t_goods
订单表:t_order

数据源表:t_dict_type



- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.3.9.RELEASEversion>
- <relativePath/>
- parent>

导入pom依赖时,报红杠杠,是网络问题,删除重新导入或者换一个网络重新导入
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-freemarkerartifactId>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <scope>runtimescope>
- <version>5.1.44version>
- dependency>
-
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- <exclusions>
- <exclusion>
- <groupId>org.junit.vintagegroupId>
- <artifactId>junit-vintage-engineartifactId>
- exclusion>
- exclusions>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.4.0version>
- dependency>
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-generatorartifactId>
- <version>3.4.0version>
- dependency>
-
-
- <dependency>
- <groupId>com.zaxxergroupId>
- <artifactId>HikariCPartifactId>
- dependency>
-
-
- <dependency>
- <groupId>commons-codecgroupId>
- <artifactId>commons-codecartifactId>
- dependency>
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-lang3artifactId>
- <version>3.6version>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-validationartifactId>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.commonsgroupId>
- <artifactId>commons-pool2artifactId>
- dependency>
-
-
-
- <dependency>
- <groupId>com.alipay.sdkgroupId>
- <artifactId>alipay-easysdkartifactId>
- <version>2.0.1version>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
没有下载这个插件的话,需要下载,下载后,重新启动idea哦,这应该是常识了叭~~~~~~~~~~~

转成yml

再将下面的配置,拷贝进去
- server:
- port: 8081
- servlet:
- context-path: /
- spring:
- datasource:
- url: jdbc:mysql://localhost:3306/spbootpro?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&characterEncoding=UTF8
- driver-class-name: com.mysql.jdbc.Driver
- password: 123456
- username: root
- hikari:
- # 最小空闲连接数量
- minimum-idle: 5
- # 空闲连接存活最大时间,默认600000(10分钟)
- idle-timeout: 180000
- # 连接池最大连接数,默认是10
- maximum-pool-size: 10
- # 此属性控制从池返回的连接的默认自动提交行为,默认值:true
- auto-commit: true
- # 连接池名称
- pool-name: MyHikariCP
- # 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
- max-lifetime: 1800000
- # 数据库连接超时时间,默认30秒,即30000
- connection-timeout: 30000
- freemarker:
- #设置编码格式
- charset: UTF-8
- #后缀
- suffix:
- #文档类型
- content-type: text/html
- #模板前端
- template-loader-path: classpath:/templates/
- #启用模板
- enabled: true
- mvc:
- static-path-pattern: /static/**
- redis:
- #服务端IP
- host: 192.168.141.128
- #端口
- port: 6379
- #密码
- password: 123456
- #选择数据库
- database: 0
- #超时时间
- timeout: 10000ms
- #Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问
- #Lettuce线程安全,Jedis线程非安全
- lettuce:
- pool:
- #最大连接数,默认8
- max-active: 8
- #最大连接阻塞等待时间,默认-1
- max-wait: 10000ms
- #最大空闲连接,默认8
- max-idle: 200
- #最小空闲连接,默认0
- min-idle: 5
- #mybatis-plus配置
- mybatis-plus:
- #所对应的 XML 文件位置
- mapper-locations: classpath*:/mapper/*Mapper.xml
- #别名包扫描路径
- type-aliases-package: com.xiaokun.spbootpro.model
- configuration:
- #驼峰命名规则
- map-underscore-to-camel-case: true
- #日志配置
- logging:
- level:
- com.xiaokun.spbootpro.mapper: debug
配置中该修该的修改一下,比如服务端IP,数据库名称什么的。

这个后缀不用修改也不用添加,空的方便后续开发
编写controller层,访问首页界面

样式,依赖都已经调整好了,导入前端页面及页面对应的js/css/images文件
前端静态页面,导入你手上现有的就ok



在浏览器中输入访问地址 http://localhost:8081/
访问成功,就说明你的前端搭建好了

将这边的静态数据变为动态数据,什么是动态数据呢?就是数据库中的数据👀


如果它的数据量大于6条时,只取前6条数据

导入工具类与代码生成器
导入之前需要下载一个插件--Lombok ⬇⬇⬇

- package com.xiaokun.spbootpro.config;
-
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
-
- @Configuration
- public class RedisConfig {
-
- @Bean
- public RedisTemplate
redisTemplate(RedisConnectionFactory connectionFactory){ - RedisTemplate
redisTemplate=new RedisTemplate<>(); - redisTemplate.setStringSerializer(new StringRedisSerializer());
- redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
- redisTemplate.setHashKeySerializer(new StringRedisSerializer());
- redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
- redisTemplate.setConnectionFactory(connectionFactory);
- redisTemplate.afterPropertiesSet();
- return redisTemplate;
- }
- }

- package com.xiaokun.spbootpro.exception;
-
- import com.xiaokun.spbootpro.utils.JsonResponseStatus;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class BusinessException extends RuntimeException {
- private JsonResponseStatus jsonResponseStatus;
- }
- package com.xiaokun.spbootpro.exception;
-
- import com.xiaokun.spbootpro.utils.JsonResponseBody;
- import com.xiaokun.spbootpro.utils.JsonResponseStatus;
- import org.springframework.validation.BindException;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
-
- @RestControllerAdvice
- public class GlobalExceptionHandler {
-
- @ExceptionHandler
- public JsonResponseBody> exceptionHandler(Exception e){
- JsonResponseBody> jsonResponseBody=null;
- e.printStackTrace();
- if(e instanceof BusinessException){
- BusinessException ex= (BusinessException) e;
- jsonResponseBody=new JsonResponseBody<>(ex.getJsonResponseStatus());
- }else if(e instanceof BindException){
- BindException ex= (BindException) e;
- String msg = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
- jsonResponseBody=new JsonResponseBody<>(JsonResponseStatus.BIND_ERROR);
- jsonResponseBody.setMsg(msg);
- }else{
- System.out.println("aaaaaa");
- }
- return jsonResponseBody;
- }
- }

- package com.xiaokun.spbootpro.generator;
-
- import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
- import com.baomidou.mybatisplus.core.toolkit.StringPool;
- import com.baomidou.mybatisplus.core.toolkit.StringUtils;
- import com.baomidou.mybatisplus.generator.AutoGenerator;
- import com.baomidou.mybatisplus.generator.InjectionConfig;
- import com.baomidou.mybatisplus.generator.config.*;
- import com.baomidou.mybatisplus.generator.config.po.TableInfo;
- import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
- import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
-
- public class CodeGenerator {
-
- /**
- *
- * 读取控制台内容
- *
- */
- public static String scanner(String tip) {
- Scanner scanner = new Scanner(System.in);
- StringBuilder help = new StringBuilder();
- help.append("请输入" + tip + ":");
- System.out.println(help.toString());
- if (scanner.hasNext()) {
- String ipt = scanner.next();
- if (StringUtils.isNotBlank(ipt)) {
- return ipt;
- }
- }
- throw new MybatisPlusException("请输入正确的" + tip + "!");
- }
-
- public static void main(String[] args) {
- // 代码生成器
- AutoGenerator mpg = new AutoGenerator();
-
- // 全局配置
- GlobalConfig gc = new GlobalConfig();
- String projectPath = System.getProperty("user.dir") + "/spbootpro";
- gc.setOutputDir(projectPath + "/src/main/java");
- gc.setAuthor("xiaokun");
- gc.setOpen(false);
- gc.setBaseColumnList(true);
- gc.setBaseResultMap(true);
- // gc.setSwagger2(true); 实体属性 Swagger2 注解
- mpg.setGlobalConfig(gc);
-
- // 数据源配置
- DataSourceConfig dsc = new DataSourceConfig();
- dsc.setUrl("jdbc:mysql://localhost:3306/spbootpro?useUnicode=true&useSSL=false&characterEncoding=utf8");
- // dsc.setSchemaName("public");
- dsc.setDriverName("com.mysql.jdbc.Driver");
- dsc.setUsername("root");
- dsc.setPassword("123456");
- mpg.setDataSource(dsc);
-
- // 包配置
- PackageConfig pc = new PackageConfig();
- //pc.setModuleName(scanner("模块名"));
- pc.setParent("com.xiaokun.spbootpro");
- //设置包名
- pc.setEntity("model");
- mpg.setPackageInfo(pc);
-
- // 自定义配置
- InjectionConfig cfg = new InjectionConfig() {
- @Override
- public void initMap() {
- // to do nothing
- }
- };
-
- // 如果模板引擎是 freemarker
- String templatePath = "/templates/mybatis-generator/mapper2.xml.ftl";
- // 如果模板引擎是 velocity
- // String templatePath = "/templates/mapper.xml.vm";
-
- // 自定义输出配置
- List
focList = new ArrayList<>(); - // 自定义配置会被优先输出
- focList.add(new FileOutConfig(templatePath) {
- @Override
- public String outputFile(TableInfo tableInfo) {
- // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
- return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
- + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
- }
- });
- /*
- cfg.setFileCreate(new IFileCreate() {
- @Override
- public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
- // 判断自定义文件夹是否需要创建
- checkDir("调用默认方法创建的目录,自定义目录用");
- if (fileType == FileType.MAPPER) {
- // 已经生成 mapper 文件判断存在,不想重新生成返回 false
- return !new File(filePath).exists();
- }
- // 允许生成模板文件
- return true;
- }
- });
- */
- cfg.setFileOutConfigList(focList);
- mpg.setCfg(cfg);
-
- // 配置模板
- TemplateConfig templateConfig = new TemplateConfig();
-
- // 配置自定义输出模板
- //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
- templateConfig.setMapper("templates/mybatis-generator/mapper2.java");
- templateConfig.setEntity("templates/mybatis-generator/entity2.java");
- templateConfig.setService("templates/mybatis-generator/service2.java");
- templateConfig.setServiceImpl("templates/mybatis-generator/serviceImpl2.java");
- templateConfig.setController("templates/mybatis-generator/controller2.java");
- templateConfig.setXml(null);
- mpg.setTemplate(templateConfig);
-
- // 策略配置
- StrategyConfig strategy = new StrategyConfig();
- strategy.setNaming(NamingStrategy.underline_to_camel);
- strategy.setColumnNaming(NamingStrategy.underline_to_camel);
- //strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
- strategy.setEntityLombokModel(true);
- strategy.setRestControllerStyle(true);
- strategy.setEntitySerialVersionUID(false);
- // 公共父类
- //strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
- // 写于父类中的公共字段
- strategy.setSuperEntityColumns("id");
- strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
- strategy.setControllerMappingHyphenStyle(true);
- strategy.setTablePrefix("t_");
- mpg.setStrategy(strategy);
- mpg.setTemplateEngine(new FreemarkerTemplateEngine());
- mpg.execute();
- }
- }
- package com.xiaokun.spbootpro.utils;
-
- import lombok.extern.slf4j.Slf4j;
-
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.UnsupportedEncodingException;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
-
- @Slf4j
- public class CookieUtils {
-
- /**
- *
- * @Description: 得到Cookie的值, 不编码
- * @param request
- * @param cookieName
- * @return
- */
- public static String getCookieValue(HttpServletRequest request, String cookieName) {
- return getCookieValue(request, cookieName, false);
- }
-
- /**
- *
- * @Description: 得到Cookie的值
- * @param request
- * @param cookieName
- * @param isDecoder
- * @return
- */
- public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) {
- Cookie[] cookieList = request.getCookies();
- if (cookieList == null || cookieName == null) {
- return null;
- }
- String retValue = null;
- try {
- for (int i = 0; i < cookieList.length; i++) {
- if (cookieList[i].getName().equals(cookieName)) {
- if (isDecoder) {
- retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8");
- } else {
- retValue = cookieList[i].getValue();
- }
- break;
- }
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return retValue;
- }
-
- /**
- *
- * @Description: 得到Cookie的值
- * @param request
- * @param cookieName
- * @param encodeString
- * @return
- */
- public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {
- Cookie[] cookieList = request.getCookies();
- if (cookieList == null || cookieName == null) {
- return null;
- }
- String retValue = null;
- try {
- for (int i = 0; i < cookieList.length; i++) {
- if (cookieList[i].getName().equals(cookieName)) {
- retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString);
- break;
- }
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return retValue;
- }
-
- /**
- *
- * @Description: 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码
- * @param request
- * @param response
- * @param cookieName
- * @param cookieValue
- */
- public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
- String cookieValue) {
- setCookie(request, response, cookieName, cookieValue, -1);
- }
-
- /**
- *
- * @Description: 设置Cookie的值 在指定时间内生效,但不编码
- * @param request
- * @param response
- * @param cookieName
- * @param cookieValue
- * @param cookieMaxage
- */
- public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
- String cookieValue, int cookieMaxage) {
- setCookie(request, response, cookieName, cookieValue, cookieMaxage, false);
- }
-
- /**
- *
- * @Description: 设置Cookie的值 不设置生效时间,但编码
- * 在服务器被创建,返回给客户端,并且保存客户端
- * 如果设置了SETMAXAGE(int seconds),会把cookie保存在客户端的硬盘中
- * 如果没有设置,会默认把cookie保存在浏览器的内存中
- * 一旦设置setPath():只能通过设置的路径才能获取到当前的cookie信息
- * @param request
- * @param response
- * @param cookieName
- * @param cookieValue
- * @param isEncode
- */
- public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
- String cookieValue, boolean isEncode) {
- setCookie(request, response, cookieName, cookieValue, -1, isEncode);
- }
-
- /**
- *
- * @Description: 设置Cookie的值 在指定时间内生效, 编码参数
- * @param request
- * @param response
- * @param cookieName
- * @param cookieValue
- * @param cookieMaxage
- * @param isEncode
- */
- public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
- String cookieValue, int cookieMaxage, boolean isEncode) {
- doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);
- }
-
- /**
- *
- * @Description: 设置Cookie的值 在指定时间内生效, 编码参数(指定编码)
- * @param request
- * @param response
- * @param cookieName
- * @param cookieValue
- * @param cookieMaxage
- * @param encodeString
- */
- public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
- String cookieValue, int cookieMaxage, String encodeString) {
- doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);
- }
-
- /**
- *
- * @Description: 删除Cookie带cookie域名
- * @param request
- * @param response
- * @param cookieName
- */
- public static void deleteCookie(HttpServletRequest request, HttpServletResponse response,
- String cookieName) {
- doSetCookie(request, response, cookieName, null, -1, false);
- }
-
-
- /**
- *
- * @Description: 设置Cookie的值,并使其在指定时间内生效
- * @param request
- * @param response
- * @param cookieName
- * @param cookieValue
- * @param cookieMaxage cookie生效的最大秒数
- * @param isEncode
- */
- private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
- String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {
- try {
- if (cookieValue == null) {
- cookieValue = "";
- } else if (isEncode) {
- cookieValue = URLEncoder.encode(cookieValue, "utf-8");
- }
- Cookie cookie = new Cookie(cookieName, cookieValue);
- if (cookieMaxage > 0)
- cookie.setMaxAge(cookieMaxage);
- if (null != request) {// 设置域名的cookie
- String domainName = getDomainName(request);
- log.info("========== domainName: {} ==========", domainName);
- if (!"localhost".equals(domainName)) {
- cookie.setDomain(domainName);
- }
- }
- cookie.setPath("/");
- response.addCookie(cookie);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- *
- * @Description: 设置Cookie的值,并使其在指定时间内生效
- * @param request
- * @param response
- * @param cookieName
- * @param cookieValue
- * @param cookieMaxage cookie生效的最大秒数
- * @param encodeString
- */
- private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
- String cookieName, String cookieValue, int cookieMaxage, String encodeString) {
- try {
- if (cookieValue == null) {
- cookieValue = "";
- } else {
- cookieValue = URLEncoder.encode(cookieValue, encodeString);
- }
- Cookie cookie = new Cookie(cookieName, cookieValue);
- if (cookieMaxage > 0)
- cookie.setMaxAge(cookieMaxage);
- if (null != request) {// 设置域名的cookie
- String domainName = getDomainName(request);
- log.info("========== domainName: {} ==========", domainName);
- if (!"localhost".equals(domainName)) {
- cookie.setDomain(domainName);
- }
- }
- cookie.setPath("/");
- response.addCookie(cookie);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- /**
- *
- * @Description: 得到cookie的域名
- * @return
- */
- private static final String getDomainName(HttpServletRequest request) {
- String domainName = null;
-
- String serverName = request.getRequestURL().toString();
- if (serverName == null || serverName.equals("")) {
- domainName = "";
- } else {
- serverName = serverName.toLowerCase();
- serverName = serverName.substring(7);
- final int end = serverName.indexOf("/");
- serverName = serverName.substring(0, end);
- if (serverName.indexOf(":") > 0) {
- String[] ary = serverName.split("\\:");
- serverName = ary[0];
- }
-
- final String[] domains = serverName.split("\\.");
- int len = domains.length;
- if (len > 3 && !isIp(serverName)) {
- // www.xxx.com.cn
- domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];
- } else if (len <= 3 && len > 1) {
- // xxx.com or xxx.cn
- domainName = "." + domains[len - 2] + "." + domains[len - 1];
- } else {
- domainName = serverName;
- }
- }
- return domainName;
- }
-
- public static String trimSpaces(String IP){//去掉IP字符串前后所有的空格
- while(IP.startsWith(" ")){
- IP= IP.substring(1,IP.length()).trim();
- }
- while(IP.endsWith(" ")){
- IP= IP.substring(0,IP.length()-1).trim();
- }
- return IP;
- }
-
- public static boolean isIp(String IP){//判断是否是一个IP
- boolean b = false;
- IP = trimSpaces(IP);
- if(IP.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){
- String s[] = IP.split("\\.");
- if(Integer.parseInt(s[0])<255)
- if(Integer.parseInt(s[1])<255)
- if(Integer.parseInt(s[2])<255)
- if(Integer.parseInt(s[3])<255)
- b = true;
- }
- return b;
- }
- }
- package com.xiaokun.spbootpro.utils;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- public class DataUtils
{ - /**
- * 转换方法,基于商品的排版情况(一行三列、一行四列等等)进行数据分行处理
- * @param cols 一行显示几列
- * @param goods 需要筛选的数据集
- * @return
- */
- public Map
> transfor(int cols, List goods) { - Map
> data=new HashMap<>(); - List
rs=new ArrayList<>(); - int len=goods.size();
- for (int i = 0; i < len; i++) {
- rs.add(goods.get(i));
- if((i+1)%cols==0){
- data.put("goods"+(i+1),rs);
- if(i==len-1)
- break;
- rs=new ArrayList<>();
- continue;
- }
- if(i==len-1){
- data.put("goods"+(i+1),rs);
- }
- }
- return data;
- }
- }
- package com.xiaokun.spbootpro.utils;
-
- import lombok.Data;
-
- import java.io.Serializable;
-
- /**
- * 响应封装类
- */
- @Data
- public class JsonResponseBody
implements Serializable { -
- private String msg="OK";
- private T data;
- private Integer code;
- private Integer total;
-
- public JsonResponseBody(){
- this.data=null;
- this.code=JsonResponseStatus.SUCCESS.getCode();
- }
-
- public JsonResponseBody(T data){
- this.data=data;
- this.code=JsonResponseStatus.SUCCESS.getCode();
- }
-
- public JsonResponseBody(T data,Integer total){
- this.data=data;
- this.total=total;
- this.code=JsonResponseStatus.SUCCESS.getCode();
- }
-
- public JsonResponseBody(JsonResponseStatus jsonResponseStatus){
- this.msg=jsonResponseStatus.getMsg();
- this.code=jsonResponseStatus.getCode();
- }
-
- public JsonResponseBody(JsonResponseStatus jsonResponseStatus,T data){
- this.data=data;
- this.msg=jsonResponseStatus.getMsg();
- this.code=jsonResponseStatus.getCode();
- }
- }
- package com.xiaokun.spbootpro.utils;
-
- import lombok.AllArgsConstructor;
- import lombok.Getter;
- import lombok.ToString;
-
- @Getter
- @ToString
- @AllArgsConstructor
- public enum JsonResponseStatus {
-
- SUCCESS(200,"OK"),
- ERROR(500,"内部错误"),
-
- USER_LOGIN_ERROR(100101,"用户名或者密码错误"),
- USER_MOBILE_ERROR(100102,"手机号码格式错误"),
- USER_PASSWORD_ERROR(100103,"用户密码错误"),
- USER_USERNAME_ERROR(100104,"账号不存在"),
-
- BIND_ERROR(200101,"参数校验异常"),
- TOKEN_EEROR(200102,"token令牌失效或已过期")
- ;
-
- private final Integer code;
- private final String msg;
- }
- package com.xiaokun.spbootpro.utils;
-
- import org.apache.commons.codec.digest.DigestUtils;
- import org.springframework.stereotype.Component;
-
- import java.util.UUID;
-
- /**
- * MD5加密
- * 用户端:password=MD5(明文+固定Salt)
- * 服务端:password=MD5(用户输入+随机Salt)
- * 用户端MD5加密是为了防止用户密码在网络中明文传输,服务端MD5加密是为了提高密码安全性,双重保险。
- */
- @Component
- public class MD5Utils {
-
- //加密盐,与前端一致
- private static String salt="f1g2h3j4";
-
- /**
- * md5加密
- * @param src
- * @return
- */
- public static String md5(String src){
- return DigestUtils.md5Hex(src);
- }
-
- /**
- * 获取加密的盐
- * @return
- */
- public static String createSalt(){
- return UUID.randomUUID().toString().replace("-","");
- }
-
- /**
- * 将前端的明文密码通过MD5加密方式加密成后端服务所需密码
- * 注意:该步骤实际是在前端完成!!!
- * @param inputPass 明文密码
- * @return
- */
- public static String inputPassToFormpass(String inputPass){
- //混淆固定盐salt,安全性更可靠
- String str=salt.charAt(1)+""+salt.charAt(5)+inputPass+salt.charAt(0)+""+salt.charAt(3);
- return md5(str);
- }
-
- /**
- * 将后端密文密码+随机salt生成数据库的密码
- * @param formPass
- * @param salt
- * @return
- */
- public static String formPassToDbPass(String formPass,String salt){
- //混淆固定盐salt,安全性更可靠
- String str=salt.charAt(7)+""+salt.charAt(9)+formPass+salt.charAt(1)+""+salt.charAt(5);
- return md5(str);
- }
-
- /**
- * 将用户输入的密码转换成数据库的密码
- * @param inputPass 明文密码
- * @param salt 盐
- * @return
- */
- public static String inputPassToDbPass(String inputPass,String salt){
- String formPass = inputPassToFormpass(inputPass);
- String dbPass = formPassToDbPass(formPass, salt);
- return dbPass;
- }
-
- public static void main(String[] args) {
- //d7aaa28e3b8e6c88352bd5e7c23829f9
- //5512a78a188b318c074a15f9b056a712
- String formPass = inputPassToFormpass("123456");
- System.out.println("前端加密密码:"+formPass);
- String salt = createSalt();
- System.out.println("后端加密随机盐:"+salt);
- String dbPass = formPassToDbPass(formPass, salt);
- System.out.println("后端加密密码:"+dbPass);
- System.out.println("-------------------------------------------");
- String dbPass1 = inputPassToDbPass("123456", salt);
- System.out.println("最终加密密码:"+dbPass1);
- }
- }
- package com.xiaokun.spbootpro.utils;
-
- import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
- import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
-
- //Spring boot方式
- @EnableTransactionManagement
- @Configuration
- @MapperScan("com.zking.shoppingpro.service.*.mapper*")
- public class MybatisPlusConfig {
-
- @Bean
- public PaginationInterceptor paginationInterceptor() {
- PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
- // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
- // paginationInterceptor.setOverflow(false);
- // 设置最大单页限制数量,默认 500 条,-1 不受限制
- // paginationInterceptor.setLimit(500);
- // 开启 count 的 join 优化,只针对部分 left join
- paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
- return paginationInterceptor;
- }
- }
- package com.xiaokun.spbootpro.utils;
-
- import javax.servlet.http.HttpServletRequest;
- import java.io.Serializable;
- import java.util.Map;
-
- public class PageBean implements Serializable {
-
- //页码
- private int page=1;
- //每页显示条数
- private int rows=10;
- //总记录数
- private int total=0;
- //是否分页标记
- private boolean pagination=true;
- //上一次请求的路径
- private String url;
- //请求参数Map集合
- private Map
map; -
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public Map
getMap() { - return map;
- }
- public void setMap(Map
map) { - this.map = map;
- }
- public int getPage() {
- return page;
- }
- public void setPage(int page) {
- this.page = page;
- }
- public int getRows() {
- return rows;
- }
- public void setRows(int rows) {
- this.rows = rows;
- }
- public int getTotal() {
- return total;
- }
- public void setTotal(int total) {
- this.total = total;
- }
- public boolean isPagination() {
- return pagination;
- }
- public void setPagination(boolean pagination) {
- this.pagination = pagination;
- }
-
- //重载setPage/setRows/setPagination方法
- public void setPage(String page) {
- if(null!=page&&!"".equals(page))
- this.page=Integer.parseInt(page);
- }
- public void setRows(String rows) {
- if(null!=rows&&!"".equals(rows))
- this.rows=Integer.parseInt(rows);
- }
- public void setPagination(String pagination) {
- if(null!=pagination&&!"".equals(pagination))
- this.pagination=Boolean.parseBoolean(pagination);
- }
-
- public void setRequest(HttpServletRequest req) {
- //获取前端提交的page/rows/pagination参数
- String page=req.getParameter("page");
- String rows=req.getParameter("rows");
- String pagination=req.getParameter("pagination");
-
- //设置属性
- this.setPage(page);
- this.setPagination(pagination);
- this.setRows(rows);
-
- //设置上一次请求的路径
- //第一次请求:
- //http://localhost:8080/项目名/请求路径.action?page=1
- //第二次请求:下一页 page=2
- //项目名+请求路径.action
- //req.getContextPath()+req.getServletPath()==req.getRequestURI()
- this.url=req.getRequestURI();
-
- //获取请求参数集合
- // checkbox name='hobby'
- // Map
hobby==key value==new String[]{"篮球","足球",..} - this.map=req.getParameterMap();
- }
-
- /**
- * 获取分页的起始位置
- * @return
- */
- public int getStartIndex() {
- //第1页:(1-1)*10 ==0 limit 0,10
- //第2页:(2-1)*10 ==10 limit 10,10
- //..
- return (this.page-1)*this.rows;
- }
-
- //获取末页、上一页、下一页
- /**
- * 获取末页
- * @return
- */
- public int getMaxPager() {
- int maxPager=this.total/this.rows;
- if(this.total%this.rows!=0)
- maxPager++;
- return maxPager;
- }
-
- /**
- * 获取上一页
- * @return
- */
- public int getPreviousPager() {
- int previousPager=this.page-1;
- if(previousPager<=1)
- previousPager=1;
- return previousPager;
- }
-
- /**
- * 获取下一页
- * @return
- */
- public int getNextPager() {
- int nextPager=this.page+1;
- if(nextPager>getMaxPager())
- nextPager=getMaxPager();
- return nextPager;
- }
- @Override
- public String toString() {
- return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination
- + ", url=" + url + ", map=" + map + "]";
- }
-
- }
- package com.xiaokun.spbootpro.utils;
-
- import org.apache.commons.lang3.StringUtils;
-
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * 正则校验工具类
- * @author 刘开宇
- */
- public class ValidatorUtils {
-
- private static final Pattern mobile_pattern=Pattern.compile("[1]([0-9])[0-9]{9}$");
-
- public static boolean isMobile(String mobile){
- if(StringUtils.isEmpty(mobile))
- return false;
- Matcher matcher = mobile_pattern.matcher(mobile);
- return matcher.matches();
- }
- }

- package com.xiaokun.spbootpro.validator;
-
- import javax.validation.Constraint;
- import javax.validation.Payload;
- import java.lang.annotation.*;
-
- @Documented
- @Constraint(
- validatedBy = {MobileValidator.class}
- )
- @Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
- @Retention(RetentionPolicy.RUNTIME)
- public @interface IsMobile {
-
- boolean required() default true;
-
- String message() default "手机号码格式错误!";
-
- Class>[] groups() default {};
-
- Class extends Payload>[] payload() default {};
-
- }
- package com.xiaokun.spbootpro.validator;
-
-
- import com.xiaokun.spbootpro.utils.ValidatorUtils;
-
- import javax.validation.ConstraintValidator;
- import javax.validation.ConstraintValidatorContext;
-
- public class MobileValidator implements ConstraintValidator
{ -
- private boolean required=false;
-
- @Override
- public void initialize(IsMobile constraintAnnotation) {
- this.required=constraintAnnotation.required();
- }
-
- @Override
- public boolean isValid(String mobile, ConstraintValidatorContext constraintValidatorContext) {
- if(!this.required)
- return false;
- return ValidatorUtils.isMobile(mobile);
- }
- }
导入模板生成类
将默认生成的class文件清空,再重新生成一遍

生成一波

看到文件生成完成字样是不是以为生成成功了??不,idea虚晃一枪,吓唬你的!!
怎么判断是否生成成功?? 
看出区别了吗~~~~~
没有生成成功,把生成失败的空的mapper,model等文件删除重新运行就好了。

生成成功案例~~~~~~
@Autowired 标注接口,注入该接口的实现类


IndexController.java
- package com.xiaokun.spbootpro.controller;
-
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.xiaokun.spbootpro.model.Goods;
- import com.xiaokun.spbootpro.service.IGoodsService;
- import com.xiaokun.spbootpro.utils.DataUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import java.util.List;
- import java.util.Map;
-
- /**
- * @author 小坤
- * @create 2022-11-07-17:09
- */
- @Controller
- public class IndexController {
- @Autowired
- private IGoodsService goodsService;
-
- @RequestMapping("/")
- public String index(Model model){
- //摆件花艺
- List
goods01 = goodsService.list(new QueryWrapper() - .eq("goods_type", "01")
- .last("limit 6"));
- //壁挂北欧
- List
goods07 = goodsService.list(new QueryWrapper() - .eq("goods_type", "07")
- .last("limit 12"));
-
- //以下代码为了方便首页数据展示,方便摆放
- DataUtils
dataUtils=new DataUtils<>(); - Map
> gt01=dataUtils.transfor(3,goods01); - Map
> gt07=dataUtils.transfor(4,goods07); - model.addAttribute("gt01",gt01);
- model.addAttribute("gt07",gt07);
-
- return "index.html";
- }
- }
解释一下代码中的cols:3 / 4是什么意思



使用debug调式,看一下有没有拿到理想的展示的数据


理想展示效果👀
小编在写这一步时,前端代码调不出,手敲的代码不变色以及会报红,不知道你们会不会有这个问题,但这个问题很好解决加入html就好了⬇⬇
语法:
1) list集合判空
<#if gt01?? && gt01?size gt 0>2) 遍历map集合,获取所有的keys
<#list gt01?keys as key>3) 根据key获取对应value值gt01[key]
<#list gt01[key] as g>


现在点击首页的登录按钮,是没有登录界面的,所以⬇⬇
写一个公共的 controller ,先解决跳转登录的问题

- package com.xiaokun.spbootpro.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- /**
- * @author 小坤
- * @create 2022-11-08-14:31
- */
- @Controller
- public class PageController {
-
- @RequestMapping("/page/{page}")
- public String page(@PathVariable("page") String page){
-
- return page;
- }
-
- @RequestMapping("/page/{dir}/{page}")
- public String dir(@PathVariable("dir") String dir,@PathVariable("page") String page){
-
- return dir + "/" +page;
- }
-
-
- }

跳转没问题喽~~~~~其他页面的跳转都ok了,因为这个类是公共的
现在可以去编写登录界面的方法一些什么的,创建一个文件包。

验证是否可以使用

刷新一手,页面弹1就没问题,这样就可以放心写代码了
login.js ⬇⬇
- $(function () {
- alert(1);
-
- //登录向后台发送ajax请求
- $("#login").click(function () {
- let mobile = $("#mobile").val();
- let password = $("#password").val();
-
- $.post("/user/toLogin", {
- mobile: mobile,
- password: password
- },function (res) {
- alert(res.msg)
- },"json");
- });
- });

- package com.xiaokun.spbootpro.model.dto;
-
- import lombok.Data;
-
- import javax.validation.constraints.NotBlank;
-
- @Data
- public class UserDto {
- @NotBlank(message = "手机号码不能为空!")
- // @IsMobile
- private String mobile;
- @NotBlank(message = "密码不能为空!")
- private String password;
- }
编写 UserController.java 处理login.js发送的请求
- package com.xiaokun.spbootpro.controller;
-
-
- import com.xiaokun.spbootpro.model.dto.UserDto;
- import com.xiaokun.spbootpro.service.IUserService;
- import com.xiaokun.spbootpro.utils.JsonResponseBody;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- /**
- *
- * 用户信息表 前端控制器
- *
- *
- * @author xiaokun
- * @since 2022-11-07
- */
- @RestController
- @RequestMapping("/user")
- public class UserController {
-
- @Autowired
- private IUserService userService;
-
- @RequestMapping("/toLogin")
- public JsonResponseBody toLogin(@Valid UserDto userDto, HttpServletRequest req, HttpServletResponse resp){
-
- return userService.toLogin(userDto,req,resp);
- }
-
-
- }
返回JsonResponseBody 
编写接口类 UserServiceImpl.java

- package com.xiaokun.spbootpro.service.impl;
-
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.xiaokun.spbootpro.exception.BusinessException;
- import com.xiaokun.spbootpro.model.User;
- import com.xiaokun.spbootpro.mapper.UserMapper;
- import com.xiaokun.spbootpro.model.dto.UserDto;
- import com.xiaokun.spbootpro.service.IUserService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.xiaokun.spbootpro.utils.JsonResponseBody;
- import com.xiaokun.spbootpro.utils.JsonResponseStatus;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- /**
- *
- * 用户信息表 服务实现类
- *
- *
- * @author xiaokun
- * @since 2022-11-07
- */
- @Service
- public class UserServiceImpl extends ServiceImpl
implements IUserService { - @Autowired
- private UserMapper userMapper;
-
- @Override
- public JsonResponseBody toLogin(UserDto userDto, HttpServletRequest req, HttpServletResponse resp) {
- // 1.5.1)判断mobile和password是否为空 @Valid注解帮我做完了
- // 1.5.2)判断mobile格式是否正确 @IsMobile注解帮我做完了
- // 1.5.3)根据用户手机号码查询用户是否存在
- User user = userMapper.selectOne(new QueryWrapper
() - .eq("id", userDto.getMobile()));
-
- // 1.5.4)校验账号
- if(user==null){
- throw new BusinessException(JsonResponseStatus.USER_USERNAME_ERROR);
- }
- // 1.5.5)校验密码
- if(userDto.getPassword().equals(user.getPassword())){
- throw new BusinessException(JsonResponseStatus.USER_PASSWORD_ERROR);
- }
- return new JsonResponseBody();
- }
- }
数据库明文密码内容

测试一波,登录的提示功能

前端加密:防止客户端浏览器F12导致密码泄露
后端加密:防止数据库数据泄露导致密码泄露

加载盐加密

login.js变更如下
- $(function () {
- alert(2);
-
- //登录向后台发送ajax请求
- $("#login").click(function () {
- let mobile = $("#mobile").val();
- let password = $("#password").val();
-
- //1.密码加密
- //1) 定义固定盐
- let salt='f1g2h3j4';
- //2) 固定盐混淆
- let temp=salt.charAt(1)+""+salt.charAt(5)+password+salt.charAt(0)+""+salt.charAt(3);
- //3) 使用MD5完成前端第一次加密
- let pwd=md5(temp);
-
- $.post("/user/toLogin", {
- mobile: mobile,
- password: pwd
- },function (res) {
- if (res.code!=200){
- alert(res.msg);
- }else
- // alert(res.msg)
- location.href='/';
- },"json");
- });
- });
UserServiceImpl.java变更如下
- package com.xiaokun.spbootpro.service.impl;
-
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.xiaokun.spbootpro.exception.BusinessException;
- import com.xiaokun.spbootpro.model.User;
- import com.xiaokun.spbootpro.mapper.UserMapper;
- import com.xiaokun.spbootpro.model.dto.UserDto;
- import com.xiaokun.spbootpro.service.IUserService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.xiaokun.spbootpro.utils.JsonResponseBody;
- import com.xiaokun.spbootpro.utils.JsonResponseStatus;
- import com.xiaokun.spbootpro.utils.MD5Utils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import sun.security.provider.MD5;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- /**
- *
- * 用户信息表 服务实现类
- *
- *
- * @author xiaokun
- * @since 2022-11-07
- */
- @Service
- public class UserServiceImpl extends ServiceImpl
implements IUserService { - @Autowired
- private UserMapper userMapper;
-
- @Override
- public JsonResponseBody toLogin(UserDto userDto, HttpServletRequest req, HttpServletResponse resp) {
- // 1.5.1)判断mobile和password是否为空 @Valid注解帮我做完了
- // 1.5.2)判断mobile格式是否正确 @IsMobile注解帮我做完了
- // 1.5.3)根据用户手机号码查询用户是否存在
- User user = userMapper.selectOne(new QueryWrapper
() - .eq("id", userDto.getMobile()));
-
- // 1.5.4)校验账号
- if(user==null){
- throw new BusinessException(JsonResponseStatus.USER_USERNAME_ERROR);
- }
-
- //前面传递到后台的密码,要经过工具类MD5加密一次,才有可能跟数据库密码匹配上
- String pwd = MD5Utils.formPassToDbPass(userDto.getPassword(), user.getSalt());
-
- // 1.5.5)校验密码
- if(!pwd.equals(user.getPassword())){
- throw new BusinessException(JsonResponseStatus.USER_PASSWORD_ERROR);
- }
- return new JsonResponseBody();
- }
- }
将登录的用户数据分别保留在客户端以及服务端
UserServiceImpl.java变更如下
- package com.xiaokun.spbootpro.service.impl;
-
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.xiaokun.spbootpro.exception.BusinessException;
- import com.xiaokun.spbootpro.mapper.UserMapper;
- import com.xiaokun.spbootpro.model.User;
- import com.xiaokun.spbootpro.model.dto.UserDto;
- import com.xiaokun.spbootpro.service.IRedisService;
- import com.xiaokun.spbootpro.service.IUserService;
- import com.xiaokun.spbootpro.utils.CookieUtils;
- import com.xiaokun.spbootpro.utils.JsonResponseBody;
- import com.xiaokun.spbootpro.utils.JsonResponseStatus;
- import com.xiaokun.spbootpro.utils.MD5Utils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.util.UUID;
-
- /**
- * @author 小坤
- * @create 2022-11-09-9:18
- */
- @Service
- public class UserServiceImpl extends ServiceImpl
implements IUserService { -
- @Autowired
- private UserMapper userMapper;
-
- @Autowired
- private IRedisService redisService;
-
- @Override
- public JsonResponseBody toLogin(UserDto userDto, HttpServletRequest req, HttpServletResponse resp) {
- //1.判断账号和密码是否为空(已由JSP303完成)
- //2.判断手机号码格式是否正确(自定义验证注解)
- //3.根据手机号码查询用户对象信息
- User user = userMapper.selectOne(new QueryWrapper
() - .eq("id", userDto.getMobile()));
- //4.判断用户对象是否存在
- if(null==user)
- throw new BusinessException(JsonResponseStatus.USER_USERNAME_ERROR);
-
- //5.判断用户对象密码与输入密码是否一致
- //将前端传入的第一道加密密码结合从数据库查询出来的随机盐进行加密(二道加密)
- String pwd = MD5Utils.formPassToDbPass(userDto.getPassword(), user.getSalt());
- if(!user.getPassword().equals(pwd))
- throw new BusinessException(JsonResponseStatus.USER_PASSWORD_ERROR);
-
- //6.将登陆用户对象与token令牌进行绑定保存到cookie和redis
- //创建登陆令牌token
- String token= UUID.randomUUID().toString().replace("-","");
- //将token令牌保存到cookie中
- CookieUtils.setCookie(req,resp,"token",token,7200);
- //将登陆token令牌与用户对象user绑定到redis中
- redisService.setUserToRedis(token,user);
- //将用户登陆的昵称设置到cookie中
- CookieUtils.setCookie(req,resp,"nickname",user.getNickname());
- return new JsonResponseBody<>();
- }
- }
IRedisService
- package com.xiaokun.spbootpro.service;
-
- import com.xiaokun.spbootpro.model.User;
-
- /**
- * @author 小坤
- * @create 2022-11-09-8:36
- */
- public interface IRedisService {
-
- /**
- * 将用户对象user与登陆token令牌保存到redis
- * @param token 登陆令牌token
- * @param user 登陆用户对象user
- */
- void setUserToRedis(String token, User user);
-
- /**
- * 根据登陆token令牌到redis中获取对应的用户登陆对象信息
- * @param token 登陆令牌token
- * @return 登陆用户对象
- */
- User getUserToRedis(String token);
-
- }
IRedisServiceImpl
- package com.xiaokun.spbootpro.service.impl;
-
- import com.xiaokun.spbootpro.model.User;
- import com.xiaokun.spbootpro.service.IRedisService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Service;
-
- import java.util.concurrent.TimeUnit;
-
- /**
- * @author 小坤
- * @create 2022-11-09-8:39
- */
- @Service
- public class IRedisServiceImpl implements IRedisService {
- @Autowired
- private RedisTemplate
redisTemplate; -
- @Override
- public void setUserToRedis(String token, User user) {
- redisTemplate.opsForValue().set("user:"+token,user,7200L, TimeUnit.SECONDS);
- }
-
- @Override
- public User getUserToRedis(String token) {
- return (User)redisTemplate.opsForValue().get("user:"+token);
- }
- }
开启你在application.yml中使用的虚拟机或云服务器
并使用虚拟机连接器连接成功后启动redis

运行测试,前端是否拿到token
