长春微信网站建设,wordpress恢复旧版,工作计划及目标,php网站开发与设计目录
一、问题说明
二、环境搭建
2.1 Git管理代码
2.1.1 创建本地仓库
2.1.2 创建远程仓库
2.1.3 创建分支--》推送到远程仓库
2.2 maven坐标
2.3 配置文件application.yml
2.4 配置类RedisConfig
三、缓存短信验证码
3.1 实现思路
3.2 代码改造
3.2.1 UserContro…目录
一、问题说明
二、环境搭建
2.1 Git管理代码
2.1.1 创建本地仓库
2.1.2 创建远程仓库
2.1.3 创建分支--》推送到远程仓库
2.2 maven坐标
2.3 配置文件application.yml
2.4 配置类RedisConfig
三、缓存短信验证码
3.1 实现思路
3.2 代码改造
3.2.1 UserController
3.3 功能测试
四、缓存菜品数据
4.1 实现思路
4.2 代码改造
4.2.1 第一步改造DishController 中list方法
4.2.2 第三步改造DishController中的save和update方法
4.3 功能测试
五、Spring Cache
5.1 Spring Cache介绍
5.2 Spring Cache常用注解
5.3 Spring Cache使用方式 5.3.1 pom
六、缓存套餐数据
6.1 实现思路
6.2 代码改造 6.2.1 pom 坐标
6.2.2 application.yml配置缓存数据过期时间
6.2.3 开启缓存注解功能 ReggieApplication 6.2.4 R实现 Serializable 接口 6.2.5 在SetmealController的list方法加入Cacheable注解
6.2.6 在SetmealController的delete方法加入CacheEvict注解
6.2.7 在SetmealController的save方法加入CacheEvict注解
6.3 功能测试 前言使用缓存进行项目优化 一、问题说明 二、环境搭建
2.1 Git管理代码
2.1.1 创建本地仓库 2.1.2 创建远程仓库
略。。后续都省略
2.1.3 创建分支--》推送到远程仓库
略
2.2 maven坐标 !--redis--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency
2.3 配置文件application.yml
server:port: 8080
spring:application:# 应用名称 可选name: reggie_take_out_spuerdatasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/reggie?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8zeroDateTimeBehaviorconvertToNulluseSSLfalseallowPublicKeyRetrievaltrueusername: rotpassword: ruredis:host: 127.0.0.1port: 6379password: 123database: 0
mybatis-plus:configuration:#在映射实体或者属性时将数据库中表名和字段名中的下划线去掉按照驼峰命名法映射map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:id-type: ASSIGN_ID
# 文件上传目录
reggie:path: D:\images\2.4 配置类RedisConfig package com.runa.reggie.config;import org.springframework.cache.annotation.CachingConfigurerSupport;
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.StringRedisSerializer;Configuration
public class RedisConfig extends CachingConfigurerSupport {Beanpublic RedisTemplateObject, Object redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplateObject, Object redisTemplate new RedisTemplate();//默认的Key序列化器为JdkSerializationRedisSerializerredisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setConnectionFactory(connectionFactory);return redisTemplate;}
}三、缓存短信验证码
3.1 实现思路 3.2 代码改造
3.2.1 UserController
package com.runa.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.runa.reggie.common.R;
import com.runa.reggie.entity.User;
import com.runa.reggie.service.UserService;
import com.runa.reggie.utils.SMSUtils;
import com.runa.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;RestController
RequestMapping(/user)
Slf4j
public class UserController {Autowiredprivate UserService userService;Autowiredprivate RedisTemplate redisTemplate;/*** 发送手机短信验证码* param user* return*/PostMapping(/sendMsg)public RString sendMsg(RequestBody User user, HttpSession session){//获取手机号String phone user.getPhone();if(StringUtils.isNotEmpty(phone)){//生成随机的4位验证码String code ValidateCodeUtils.generateValidateCode(4).toString();log.info(code{},code);//调用阿里云提供的短信服务API完成发送短信//SMSUtils.sendMessage(瑞吉外卖,,phone,code);//需要将生成的验证码保存到Session
// session.setAttribute(phone,code);// 将生成的验证码缓存到Redis中,并且设置有效期为5分钟redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);return R.success(手机验证码短信发送成功);}return R.error(短信发送失败);}/*** 移动端用户登录* param map* param session* return*/PostMapping(/login)public RUser login(RequestBody Map map, HttpSession session){log.info(map.toString());//获取手机号String phone map.get(phone).toString();//获取验证码String code map.get(code).toString();//从Session中获取保存的验证码
// Object codeInSession session.getAttribute(phone);//从redis中获取缓存的验证码Object codeInSession redisTemplate.opsForValue().get(phone);//进行验证码的比对页面提交的验证码和Session中保存的验证码比对if(codeInSession ! null codeInSession.equals(code)){//如果能够比对成功说明登录成功LambdaQueryWrapperUser queryWrapper new LambdaQueryWrapper();queryWrapper.eq(User::getPhone,phone);User user userService.getOne(queryWrapper);if(user null){//判断当前手机号对应的用户是否为新用户如果是新用户就自动完成注册user new User();user.setPhone(phone);user.setStatus(1);userService.save(user);}session.setAttribute(user,user.getId());// 如果用户登录成功删除redis缓存的验证码redisTemplate.delete(phone);return R.success(user);}return R.error(登录失败);}}3.3 功能测试
启动后端服务、启动redis
http://localhost:8080/front/page/login.html 四、缓存菜品数据
4.1 实现思路 4.2 代码改造
4.2.1 第一步改造DishController 中list方法
package com.runa.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Dish;
import com.runa.reggie.entity.DishFlavor;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.DishFlavorService;
import com.runa.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;/*** 菜品管理*/
RestController
RequestMapping(/dish)
Slf4j
public class DishController {Autowiredprivate DishService dishService;Autowiredprivate DishFlavorService dishFlavorService;Autowiredprivate CategoryService categoryService;Autowiredprivate RedisTemplate redisTemplate;/*** 新增菜品* param dishDto* return*/PostMappingpublic RString save(RequestBody DishDto dishDto){log.info(dishDto.toString());dishService.saveWithFlavor(dishDto);return R.success(新增菜品成功);}GetMapping(/page)public RPage page(int page, int pageSize, String name){// 构造分页构造器对象PageDish pageInfo new Page(page, pageSize);PageDishDto dishDtoPage new Page();//条件构造器LambdaQueryWrapperDish queryWrapper new LambdaQueryWrapper();// 添加过滤条件queryWrapper.like(name ! null,Dish::getName,name);// 添加排序条件queryWrapper.orderByDesc(Dish::getUpdateTime);//执行分页查询dishService.page(pageInfo,queryWrapper);// 对象拷贝BeanUtils.copyProperties(pageInfo, dishDtoPage,records);ListDish records pageInfo.getRecords();ListDishDto list records.stream().map((item) - {DishDto dishDto new DishDto();BeanUtils.copyProperties(item,dishDto);Long categoryId item.getCategoryId();// 分类idCategory category categoryService.getById(categoryId);String categoryName category.getName();dishDto.setCategoryName(categoryName);return dishDto;}).collect(Collectors.toList());dishDtoPage.setRecords(list);return R.success(dishDtoPage);}/*** 根据ID 查询菜品和对应口味信息 回显* param id* return*/GetMapping(/{id})public RDishDto get(PathVariable Long id){log.info(要查询显示的菜品信息是{},id);DishDto dishDto dishService.getByIdWithFlavor(id);return R.success(dishDto);}/*** 修改菜品* param dishDto* return*/PutMappingpublic RString update(RequestBody DishDto dishDto){log.info(dishDto.toString());dishService.updateWithFlavor(dishDto);return R.success(修改菜品成功);}// /**
// * 根据条件查询对于菜品数据
// * param dish
// * return
// */
// GetMapping(/list)
// public RListDish list(Dish dish){
//
// // 构造查询条件
// LambdaQueryWrapperDish queryWrapper new LambdaQueryWrapper();
// queryWrapper.eq(dish.getCategoryId() ! null,Dish::getCategoryId,dish.getCategoryId());
// // 添加条件查询状态为1起售状态的菜品
// queryWrapper.eq(Dish::getStatus,1);
// // 天添加排序条件
// queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
// ListDish list dishService.list(queryWrapper);
// return R.success(list);
// }
//}/*** 根据条件查询对于菜品数据* param dish* return*/GetMapping(/list)public RListDishDto list(Dish dish){ListDishDto dishDtoList null;// 动态构造keyString key dish_ dish.getCategoryId() _ dish.getStatus();// 先从redis获取缓存数据dishDtoList (ListDishDto) redisTemplate.opsForValue().get(key);if(dishDtoList ! null){// 1 如果存在直接返回无需查询数据库return R.success(dishDtoList);}// 2 如果不存在需要查询数据库// 构造查询条件LambdaQueryWrapperDish queryWrapper new LambdaQueryWrapper();queryWrapper.eq(dish.getCategoryId() ! null,Dish::getCategoryId,dish.getCategoryId());// 添加条件查询状态为1起售状态的菜品queryWrapper.eq(Dish::getStatus,1);// 天添加排序条件queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);ListDish list dishService.list(queryWrapper);dishDtoList list.stream().map((item) - {DishDto dishDto new DishDto();BeanUtils.copyProperties(item,dishDto);Long categoryId item.getCategoryId();// 分类id//根据id查询分类对象Category category categoryService.getById(categoryId);if(category ! null){String categoryName category.getName();dishDto.setCategoryName(categoryName);}// 当前菜品的idLong dishId item.getId();LambdaQueryWrapperDishFlavor lambdaQueryWrapper new LambdaQueryWrapper();lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);ListDishFlavor dishFlavorList dishFlavorService.list(lambdaQueryWrapper);dishDto.setFlavors(dishFlavorList);return dishDto;}).collect(Collectors.toList());// 2-1 不存在将查询到的菜品数据缓存到redisredisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);return R.success(dishDtoList);}
}4.2.2 第三步改造DishController中的save和update方法
package com.runa.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.DishDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Dish;
import com.runa.reggie.entity.DishFlavor;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.DishFlavorService;
import com.runa.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;/*** 菜品管理*/
RestController
RequestMapping(/dish)
Slf4j
public class DishController {Autowiredprivate DishService dishService;Autowiredprivate DishFlavorService dishFlavorService;Autowiredprivate CategoryService categoryService;Autowiredprivate RedisTemplate redisTemplate;/*** 新增菜品* param dishDto* return*/PostMappingpublic RString save(RequestBody DishDto dishDto){log.info(dishDto.toString());dishService.saveWithFlavor(dishDto);// 清理Redis所有菜品的缓存数据
// Set keys redisTemplate.keys(dish_*);// 清理Redis某个分类下的菜品的缓存数据// 动态构造keyString key dish_ dishDto.getCategoryId() _1;redisTemplate.delete(key);return R.success(新增菜品成功);}GetMapping(/page)public RPage page(int page, int pageSize, String name){// 构造分页构造器对象PageDish pageInfo new Page(page, pageSize);PageDishDto dishDtoPage new Page();//条件构造器LambdaQueryWrapperDish queryWrapper new LambdaQueryWrapper();// 添加过滤条件queryWrapper.like(name ! null,Dish::getName,name);// 添加排序条件queryWrapper.orderByDesc(Dish::getUpdateTime);//执行分页查询dishService.page(pageInfo,queryWrapper);// 对象拷贝BeanUtils.copyProperties(pageInfo, dishDtoPage,records);ListDish records pageInfo.getRecords();ListDishDto list records.stream().map((item) - {DishDto dishDto new DishDto();BeanUtils.copyProperties(item,dishDto);Long categoryId item.getCategoryId();// 分类idCategory category categoryService.getById(categoryId);String categoryName category.getName();dishDto.setCategoryName(categoryName);return dishDto;}).collect(Collectors.toList());dishDtoPage.setRecords(list);return R.success(dishDtoPage);}/*** 根据ID 查询菜品和对应口味信息 回显* param id* return*/GetMapping(/{id})public RDishDto get(PathVariable Long id){log.info(要查询显示的菜品信息是{},id);DishDto dishDto dishService.getByIdWithFlavor(id);return R.success(dishDto);}/*** 修改菜品* param dishDto* return*/PutMappingpublic RString update(RequestBody DishDto dishDto){log.info(dishDto.toString());dishService.updateWithFlavor(dishDto);// 清理Redis所有菜品的缓存数据
// Set keys redisTemplate.keys(dish_*);// 清理Redis某个分类下的菜品的缓存数据// 动态构造keyString key dish_ dishDto.getCategoryId() _1;redisTemplate.delete(key);return R.success(修改菜品成功);}// /**
// * 根据条件查询对于菜品数据
// * param dish
// * return
// */
// GetMapping(/list)
// public RListDish list(Dish dish){
//
// // 构造查询条件
// LambdaQueryWrapperDish queryWrapper new LambdaQueryWrapper();
// queryWrapper.eq(dish.getCategoryId() ! null,Dish::getCategoryId,dish.getCategoryId());
// // 添加条件查询状态为1起售状态的菜品
// queryWrapper.eq(Dish::getStatus,1);
// // 天添加排序条件
// queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
// ListDish list dishService.list(queryWrapper);
// return R.success(list);
// }
//}/*** 根据条件查询对于菜品数据* param dish* return*/GetMapping(/list)public RListDishDto list(Dish dish){ListDishDto dishDtoList null;// 动态构造keyString key dish_ dish.getCategoryId() _ dish.getStatus();// 先从redis获取缓存数据dishDtoList (ListDishDto) redisTemplate.opsForValue().get(key);if(dishDtoList ! null){// 1 如果存在直接返回无需查询数据库return R.success(dishDtoList);}// 2 如果不存在需要查询数据库// 构造查询条件LambdaQueryWrapperDish queryWrapper new LambdaQueryWrapper();queryWrapper.eq(dish.getCategoryId() ! null,Dish::getCategoryId,dish.getCategoryId());// 添加条件查询状态为1起售状态的菜品queryWrapper.eq(Dish::getStatus,1);// 天添加排序条件queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);ListDish list dishService.list(queryWrapper);dishDtoList list.stream().map((item) - {DishDto dishDto new DishDto();BeanUtils.copyProperties(item,dishDto);Long categoryId item.getCategoryId();// 分类id//根据id查询分类对象Category category categoryService.getById(categoryId);if(category ! null){String categoryName category.getName();dishDto.setCategoryName(categoryName);}// 当前菜品的idLong dishId item.getId();LambdaQueryWrapperDishFlavor lambdaQueryWrapper new LambdaQueryWrapper();lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);ListDishFlavor dishFlavorList dishFlavorService.list(lambdaQueryWrapper);dishDto.setFlavors(dishFlavorList);return dishDto;}).collect(Collectors.toList());// 2-1 不存在将查询到的菜品数据缓存到redisredisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);return R.success(dishDtoList);}
}4.3 功能测试
http://localhost:8080/front/page/login.htmlhttp://localhost:8080/backend/index.html 五、Spring Cache
5.1 Spring Cache介绍 5.2 Spring Cache常用注解 5.3 Spring Cache使用方式 5.3.1 pom
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.runa/groupIdartifactIdreggie_take_out_spuer/artifactIdversion1.0-SNAPSHOT/versionpackagingjar/packagingparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.4.5/versionrelativePath/ !-- lookup parent from repository --/parentnamereggie_take_out_spuer/nameurlhttp://maven.apache.org/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingjava.version1.8/java.version/propertiesdependencies!--阿里云短信服务--dependencygroupIdcom.aliyun/groupIdartifactIdaliyun-java-sdk-core/artifactIdversion4.5.16/version/dependencydependencygroupIdcom.aliyun/groupIdartifactIdaliyun-java-sdk-dysmsapi/artifactIdversion2.1.0/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdscopecompile/scope/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.2/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.20/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.76/version/dependencydependencygroupIdcommons-lang/groupIdartifactIdcommons-lang/artifactIdversion2.6/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.1.23/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion3.8.1/versionscopetest/scope/dependency!--redis--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!-- spring Cache --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.4.5/version/plugin/plugins/build
/project六、缓存套餐数据
6.1 实现思路 6.2 代码改造 6.2.1 pom 坐标
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.runa/groupIdartifactIdreggie_take_out_spuer/artifactIdversion1.0-SNAPSHOT/versionpackagingjar/packagingparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.4.5/versionrelativePath/ !-- lookup parent from repository --/parentnamereggie_take_out_spuer/nameurlhttp://maven.apache.org/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingjava.version1.8/java.version/propertiesdependencies!--阿里云短信服务--dependencygroupIdcom.aliyun/groupIdartifactIdaliyun-java-sdk-core/artifactIdversion4.5.16/version/dependencydependencygroupIdcom.aliyun/groupIdartifactIdaliyun-java-sdk-dysmsapi/artifactIdversion2.1.0/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdscopecompile/scope/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.4.2/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.20/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.76/version/dependencydependencygroupIdcommons-lang/groupIdartifactIdcommons-lang/artifactIdversion2.6/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion1.1.23/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion3.8.1/versionscopetest/scope/dependency!--redis--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!-- spring Cache --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion2.4.5/version/plugin/plugins/build
/project6.2.2 application.yml配置缓存数据过期时间
server:port: 8080
spring:application:# 应用名称 可选name: reggie_take_out_spuerdatasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/reggie?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8zeroDateTimeBehaviorconvertToNulluseSSLfalseallowPublicKeyRetrievaltrueusername: rootpassword: runa#2050redis:host: 127.0.0.1port: 6379password: 123456database: 0cache:redis:time-to-live: 1800000 # 设置缓存数据的过期时间
mybatis-plus:configuration:#在映射实体或者属性时将数据库中表名和字段名中的下划线去掉按照驼峰命名法映射map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:id-type: ASSIGN_ID
# 文件上传目录
reggie:path: D:\images\6.2.3 开启缓存注解功能 ReggieApplication package com.runa.reggie;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;Slf4j
SpringBootApplication
ServletComponentScan
EnableTransactionManagement
EnableCaching // 开启spring Cache注解方式的缓存功能
public class ReggieApplication {public static void main(String[] args) {SpringApplication.run(ReggieApplication.class,args);log.info(项目启动成功~~~);}
}6.2.4 R实现 Serializable 接口
package com.runa.reggie.common;import lombok.Data;import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;/*** 通用返回结果服务端响应的数据最终都会封装成此对象* param T*/
Data
public class RT implements Serializable {private Integer code; //编码1成功0和其它数字为失败private String msg; //错误信息private T data; //数据private Map map new HashMap(); //动态数据public static T RT success(T object) {RT r new RT();r.data object;r.code 1;return r;}public static T RT error(String msg) {R r new R();r.msg msg;r.code 0;return r;}public RT add(String key, Object value) {this.map.put(key, value);return this;}}6.2.5 在SetmealController的list方法加入Cacheable注解
package com.runa.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Setmeal;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.SetmealDishService;
import com.runa.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.stream.Collectors;RestController
RequestMapping(/setmeal)
Slf4j
public class SetmealController {Autowiredprivate SetmealService setmealService;Autowiredprivate CategoryService categoryService;Autowiredprivate SetmealDishService setmealDishService;/*** 新增套餐* param setmealDto* return*/PostMappingpublic RString save(RequestBody SetmealDto setmealDto){log.info(套餐信息{},setmealDto);setmealService.saveWithDish(setmealDto);return R.success(新增套餐成功);}/*** 套餐分页查询* param page* param pageSize* param name* return*/GetMapping(/page)public RPage page(int page, int pageSize, String name){//分页构造器对象PageSetmeal pageInfo new Page(page,pageSize);PageSetmealDto dtoPage new Page();LambdaQueryWrapperSetmeal queryWrapper new LambdaQueryWrapper();//添加查询条件根据name进行like模糊查询queryWrapper.like(name ! null,Setmeal::getName,name);//添加排序条件根据更新时间降序排列queryWrapper.orderByDesc(Setmeal::getUpdateTime);setmealService.page(pageInfo,queryWrapper);//对象拷贝BeanUtils.copyProperties(pageInfo,dtoPage,records);ListSetmeal records pageInfo.getRecords();ListSetmealDto list records.stream().map((item) - {SetmealDto setmealDto new SetmealDto();//对象拷贝BeanUtils.copyProperties(item,setmealDto);//分类idLong categoryId item.getCategoryId();//根据分类id查询分类对象Category category categoryService.getById(categoryId);if(category ! null){//分类名称String categoryName category.getName();setmealDto.setCategoryName(categoryName);}return setmealDto;}).collect(Collectors.toList());dtoPage.setRecords(list);return R.success(dtoPage);}/*** 删除套餐* param ids* return*/DeleteMappingpublic RString delete(RequestParam ListLong ids){log.info(ids:{},ids);setmealService.removeWithDish(ids);return R.success(套餐数据删除成功);}/*** 根据条件查询套餐数据* param setmeal* return*/GetMapping(/list)Cacheable(value setmealCache, key #setmeal.categoryId _ #setmeal.status)public RListSetmeal list(Setmeal setmeal){LambdaQueryWrapperSetmeal queryWrapper new LambdaQueryWrapper();queryWrapper.eq(setmeal.getCategoryId() ! null,Setmeal::getCategoryId,setmeal.getCategoryId());queryWrapper.eq(setmeal.getStatus() ! null,Setmeal::getStatus,setmeal.getStatus());queryWrapper.orderByDesc(Setmeal::getUpdateTime);ListSetmeal list setmealService.list(queryWrapper);return R.success(list);}}6.2.6 在SetmealController的delete方法加入CacheEvict注解
package com.runa.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Setmeal;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.SetmealDishService;
import com.runa.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.stream.Collectors;RestController
RequestMapping(/setmeal)
Slf4j
public class SetmealController {Autowiredprivate SetmealService setmealService;Autowiredprivate CategoryService categoryService;Autowiredprivate SetmealDishService setmealDishService;/*** 新增套餐* param setmealDto* return*/PostMappingpublic RString save(RequestBody SetmealDto setmealDto){log.info(套餐信息{},setmealDto);setmealService.saveWithDish(setmealDto);return R.success(新增套餐成功);}/*** 套餐分页查询* param page* param pageSize* param name* return*/GetMapping(/page)public RPage page(int page, int pageSize, String name){//分页构造器对象PageSetmeal pageInfo new Page(page,pageSize);PageSetmealDto dtoPage new Page();LambdaQueryWrapperSetmeal queryWrapper new LambdaQueryWrapper();//添加查询条件根据name进行like模糊查询queryWrapper.like(name ! null,Setmeal::getName,name);//添加排序条件根据更新时间降序排列queryWrapper.orderByDesc(Setmeal::getUpdateTime);setmealService.page(pageInfo,queryWrapper);//对象拷贝BeanUtils.copyProperties(pageInfo,dtoPage,records);ListSetmeal records pageInfo.getRecords();ListSetmealDto list records.stream().map((item) - {SetmealDto setmealDto new SetmealDto();//对象拷贝BeanUtils.copyProperties(item,setmealDto);//分类idLong categoryId item.getCategoryId();//根据分类id查询分类对象Category category categoryService.getById(categoryId);if(category ! null){//分类名称String categoryName category.getName();setmealDto.setCategoryName(categoryName);}return setmealDto;}).collect(Collectors.toList());dtoPage.setRecords(list);return R.success(dtoPage);}/*** 删除套餐* param ids* return*/DeleteMappingCacheEvict(value setmealCache , allEntries true)public RString delete(RequestParam ListLong ids){log.info(ids:{},ids);setmealService.removeWithDish(ids);return R.success(套餐数据删除成功);}/*** 根据条件查询套餐数据* param setmeal* return*/GetMapping(/list)Cacheable(value setmealCache, key #setmeal.categoryId _ #setmeal.status)public RListSetmeal list(Setmeal setmeal){LambdaQueryWrapperSetmeal queryWrapper new LambdaQueryWrapper();queryWrapper.eq(setmeal.getCategoryId() ! null,Setmeal::getCategoryId,setmeal.getCategoryId());queryWrapper.eq(setmeal.getStatus() ! null,Setmeal::getStatus,setmeal.getStatus());queryWrapper.orderByDesc(Setmeal::getUpdateTime);ListSetmeal list setmealService.list(queryWrapper);return R.success(list);}}6.2.7 在SetmealController的save方法加入CacheEvict注解
package com.runa.reggie.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.runa.reggie.common.R;
import com.runa.reggie.dto.SetmealDto;
import com.runa.reggie.entity.Category;
import com.runa.reggie.entity.Setmeal;
import com.runa.reggie.service.CategoryService;
import com.runa.reggie.service.SetmealDishService;
import com.runa.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.stream.Collectors;RestController
RequestMapping(/setmeal)
Slf4j
public class SetmealController {Autowiredprivate SetmealService setmealService;Autowiredprivate CategoryService categoryService;Autowiredprivate SetmealDishService setmealDishService;/*** 新增套餐* param setmealDto* return*/PostMappingCacheEvict(value setmealCache , allEntries true)public RString save(RequestBody SetmealDto setmealDto){log.info(套餐信息{},setmealDto);setmealService.saveWithDish(setmealDto);return R.success(新增套餐成功);}/*** 套餐分页查询* param page* param pageSize* param name* return*/GetMapping(/page)public RPage page(int page, int pageSize, String name){//分页构造器对象PageSetmeal pageInfo new Page(page,pageSize);PageSetmealDto dtoPage new Page();LambdaQueryWrapperSetmeal queryWrapper new LambdaQueryWrapper();//添加查询条件根据name进行like模糊查询queryWrapper.like(name ! null,Setmeal::getName,name);//添加排序条件根据更新时间降序排列queryWrapper.orderByDesc(Setmeal::getUpdateTime);setmealService.page(pageInfo,queryWrapper);//对象拷贝BeanUtils.copyProperties(pageInfo,dtoPage,records);ListSetmeal records pageInfo.getRecords();ListSetmealDto list records.stream().map((item) - {SetmealDto setmealDto new SetmealDto();//对象拷贝BeanUtils.copyProperties(item,setmealDto);//分类idLong categoryId item.getCategoryId();//根据分类id查询分类对象Category category categoryService.getById(categoryId);if(category ! null){//分类名称String categoryName category.getName();setmealDto.setCategoryName(categoryName);}return setmealDto;}).collect(Collectors.toList());dtoPage.setRecords(list);return R.success(dtoPage);}/*** 删除套餐* param ids* return*/DeleteMappingCacheEvict(value setmealCache , allEntries true)public RString delete(RequestParam ListLong ids){log.info(ids:{},ids);setmealService.removeWithDish(ids);return R.success(套餐数据删除成功);}/*** 根据条件查询套餐数据* param setmeal* return*/GetMapping(/list)Cacheable(value setmealCache, key #setmeal.categoryId _ #setmeal.status)public RListSetmeal list(Setmeal setmeal){LambdaQueryWrapperSetmeal queryWrapper new LambdaQueryWrapper();queryWrapper.eq(setmeal.getCategoryId() ! null,Setmeal::getCategoryId,setmeal.getCategoryId());queryWrapper.eq(setmeal.getStatus() ! null,Setmeal::getStatus,setmeal.getStatus());queryWrapper.orderByDesc(Setmeal::getUpdateTime);ListSetmeal list setmealService.list(queryWrapper);return R.success(list);}}6.3 功能测试
http://localhost:8080/front/page/login.htmlhttp://localhost:8080/backend/index.html