目录
4.2.1 第一步改造DishController 中list方法
4.2.2 第三步改造DishController中的save和update方法
6.2.2 application.yml配置缓存数据过期时间
6.2.3 开启缓存注解功能 ReggieApplication
6.2.5 在SetmealController的list方法加入@Cacheable注解
6.2.6 在SetmealController的delete方法加入@CacheEvict注解
6.2.7 在SetmealController的save方法加入@CacheEvict注解
前言:使用缓存进行项目优化



略。。后续都省略
略

-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
- server:
- port: 8080
- spring:
- application:
- # 应用名称 可选
- name: reggie_take_out_spuer
- datasource:
- druid:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
- username: rot
- password: ru
- redis:
- host: 127.0.0.1
- port: 6379
- password: 123
- database: 0
- mybatis-plus:
- configuration:
- #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
- map-underscore-to-camel-case: true
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- global-config:
- db-config:
- id-type: ASSIGN_ID
- # 文件上传目录
- reggie:
- path: D:\images\

- 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 {
- @Bean
- public RedisTemplate
- RedisTemplate
- //默认的Key序列化器为:JdkSerializationRedisSerializer
- redisTemplate.setKeySerializer(new StringRedisSerializer());
- redisTemplate.setConnectionFactory(connectionFactory);
- return redisTemplate;
- }
- }

- 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 {
-
- @Autowired
- private UserService userService;
-
- @Autowired
- private RedisTemplate redisTemplate;
-
- /**
- * 发送手机短信验证码
- * @param user
- * @return
- */
- @PostMapping("/sendMsg")
- public R
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 R
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)){
- //如果能够比对成功,说明登录成功
-
- LambdaQueryWrapper
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("登录失败");
- }
-
- }
启动后端服务、启动redis
http://localhost:8080/front/page/login.html



- 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 {
-
- @Autowired
- private DishService dishService;
-
- @Autowired
- private DishFlavorService dishFlavorService;
-
- @Autowired
- private CategoryService categoryService;
-
- @Autowired
- private RedisTemplate redisTemplate;
-
- /**
- * 新增菜品
- * @param dishDto
- * @return
- */
- @PostMapping
- public R
save(@RequestBody DishDto dishDto){ - log.info(dishDto.toString());
- dishService.saveWithFlavor(dishDto);
- return R.success("新增菜品成功");
- }
-
- @GetMapping("/page")
- public R
page(int page, int pageSize, String name){ - // 构造分页构造器对象
- Page
pageInfo = new Page<>(page, pageSize); - Page
dishDtoPage = new Page<>(); -
- //条件构造器
- LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); - // 添加过滤条件
- queryWrapper.like(name != null,Dish::getName,name);
- // 添加排序条件
- queryWrapper.orderByDesc(Dish::getUpdateTime);
-
- //执行分页查询
- dishService.page(pageInfo,queryWrapper);
-
- // 对象拷贝
- BeanUtils.copyProperties(pageInfo, dishDtoPage,"records");
- List
records = pageInfo.getRecords(); - List
list = records.stream().map((item) -> { - DishDto dishDto = new DishDto();
-
- BeanUtils.copyProperties(item,dishDto);
- Long categoryId = item.getCategoryId();// 分类id
- Category 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 R
get(@PathVariable Long id){ - log.info("要查询显示的菜品信息是:{}",id);
- DishDto dishDto = dishService.getByIdWithFlavor(id);
- return R.success(dishDto);
- }
-
- /**
- * 修改菜品
- * @param dishDto
- * @return
- */
- @PutMapping
- public R
update(@RequestBody DishDto dishDto){ - log.info(dishDto.toString());
- dishService.updateWithFlavor(dishDto);
- return R.success("修改菜品成功");
- }
-
-
-
-
- // /**
- // * 根据条件查询对于菜品数据
- // * @param dish
- // * @return
- // */
- // @GetMapping("/list")
- // public R
> list(Dish dish){
- //
- // // 构造查询条件
- // LambdaQueryWrapper
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);
- // List
list = dishService.list(queryWrapper); - // return R.success(list);
- // }
- //}
-
- /**
- * 根据条件查询对于菜品数据
- * @param dish
- * @return
- */
- @GetMapping("/list")
- public R
> list(Dish dish){
- List
dishDtoList = null; - // 动态构造key
- String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus();
-
- // 先从redis获取缓存数据
-
- dishDtoList = (List
) redisTemplate.opsForValue().get(key); -
- if(dishDtoList != null){
- // 1 如果存在,直接返回,无需查询数据库
- return R.success(dishDtoList);
-
- }
-
- // 2 如果不存在,需要查询数据库,
-
- // 构造查询条件
- LambdaQueryWrapper
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);
- List
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);
- }
- // 当前菜品的id
- Long dishId = item.getId();
-
- LambdaQueryWrapper
lambdaQueryWrapper = new LambdaQueryWrapper<>(); - lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
- List
dishFlavorList = dishFlavorService.list(lambdaQueryWrapper); - dishDto.setFlavors(dishFlavorList);
-
- return dishDto;
- }).collect(Collectors.toList());
-
- // 2-1 不存在,将查询到的菜品数据缓存到redis
- redisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);
-
- return R.success(dishDtoList);
- }
- }
- 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 {
-
- @Autowired
- private DishService dishService;
-
- @Autowired
- private DishFlavorService dishFlavorService;
-
- @Autowired
- private CategoryService categoryService;
-
- @Autowired
- private RedisTemplate redisTemplate;
-
- /**
- * 新增菜品
- * @param dishDto
- * @return
- */
- @PostMapping
- public R
save(@RequestBody DishDto dishDto){ - log.info(dishDto.toString());
- dishService.saveWithFlavor(dishDto);
-
- // 清理Redis所有菜品的缓存数据
- // Set keys = redisTemplate.keys("dish_*");
- // 清理Redis某个分类下的菜品的缓存数据
- // 动态构造key
- String key = "dish_" + dishDto.getCategoryId() + "_1";
- redisTemplate.delete(key);
-
- return R.success("新增菜品成功");
- }
-
- @GetMapping("/page")
- public R
page(int page, int pageSize, String name){ - // 构造分页构造器对象
- Page
pageInfo = new Page<>(page, pageSize); - Page
dishDtoPage = new Page<>(); -
- //条件构造器
- LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); - // 添加过滤条件
- queryWrapper.like(name != null,Dish::getName,name);
- // 添加排序条件
- queryWrapper.orderByDesc(Dish::getUpdateTime);
-
- //执行分页查询
- dishService.page(pageInfo,queryWrapper);
-
- // 对象拷贝
- BeanUtils.copyProperties(pageInfo, dishDtoPage,"records");
- List
records = pageInfo.getRecords(); - List
list = records.stream().map((item) -> { - DishDto dishDto = new DishDto();
-
- BeanUtils.copyProperties(item,dishDto);
- Long categoryId = item.getCategoryId();// 分类id
- Category 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 R
get(@PathVariable Long id){ - log.info("要查询显示的菜品信息是:{}",id);
- DishDto dishDto = dishService.getByIdWithFlavor(id);
- return R.success(dishDto);
- }
-
- /**
- * 修改菜品
- * @param dishDto
- * @return
- */
- @PutMapping
- public R
update(@RequestBody DishDto dishDto){ - log.info(dishDto.toString());
- dishService.updateWithFlavor(dishDto);
-
- // 清理Redis所有菜品的缓存数据
- // Set keys = redisTemplate.keys("dish_*");
- // 清理Redis某个分类下的菜品的缓存数据
- // 动态构造key
- String key = "dish_" + dishDto.getCategoryId() + "_1";
- redisTemplate.delete(key);
-
- return R.success("修改菜品成功");
- }
-
-
-
-
- // /**
- // * 根据条件查询对于菜品数据
- // * @param dish
- // * @return
- // */
- // @GetMapping("/list")
- // public R
> list(Dish dish){
- //
- // // 构造查询条件
- // LambdaQueryWrapper
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);
- // List
list = dishService.list(queryWrapper); - // return R.success(list);
- // }
- //}
-
- /**
- * 根据条件查询对于菜品数据
- * @param dish
- * @return
- */
- @GetMapping("/list")
- public R
> list(Dish dish){
- List
dishDtoList = null; - // 动态构造key
- String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus();
-
- // 先从redis获取缓存数据
-
- dishDtoList = (List
) redisTemplate.opsForValue().get(key); -
- if(dishDtoList != null){
- // 1 如果存在,直接返回,无需查询数据库
- return R.success(dishDtoList);
-
- }
-
- // 2 如果不存在,需要查询数据库,
-
- // 构造查询条件
- LambdaQueryWrapper
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);
- List
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);
- }
- // 当前菜品的id
- Long dishId = item.getId();
-
- LambdaQueryWrapper
lambdaQueryWrapper = new LambdaQueryWrapper<>(); - lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
- List
dishFlavorList = dishFlavorService.list(lambdaQueryWrapper); - dishDto.setFlavors(dishFlavorList);
-
- return dishDto;
- }).collect(Collectors.toList());
-
- // 2-1 不存在,将查询到的菜品数据缓存到redis
- redisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);
-
- return R.success(dishDtoList);
- }
- }
- http://localhost:8080/front/page/login.html
-
- http://localhost:8080/backend/index.html
-





- <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.runagroupId>
- <artifactId>reggie_take_out_spuerartifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>jarpackaging>
-
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.4.5version>
- <relativePath/>
- parent>
-
- <name>reggie_take_out_spuername>
- <url>http://maven.apache.orgurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <java.version>1.8java.version>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>com.aliyungroupId>
- <artifactId>aliyun-java-sdk-coreartifactId>
- <version>4.5.16version>
- dependency>
- <dependency>
- <groupId>com.aliyungroupId>
- <artifactId>aliyun-java-sdk-dysmsapiartifactId>
- <version>2.1.0version>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- <scope>compilescope>
- dependency>
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.4.2version>
- dependency>
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.20version>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>fastjsonartifactId>
- <version>1.2.76version>
- dependency>
-
- <dependency>
- <groupId>commons-langgroupId>
- <artifactId>commons-langartifactId>
- <version>2.6version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <scope>runtimescope>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druid-spring-boot-starterartifactId>
- <version>1.1.23version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>3.8.1version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-cacheartifactId>
- dependency>
-
-
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <version>2.4.5version>
- plugin>
- plugins>
- build>
- project>

- <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.runagroupId>
- <artifactId>reggie_take_out_spuerartifactId>
- <version>1.0-SNAPSHOTversion>
- <packaging>jarpackaging>
-
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.4.5version>
- <relativePath/>
- parent>
-
- <name>reggie_take_out_spuername>
- <url>http://maven.apache.orgurl>
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <java.version>1.8java.version>
- properties>
-
- <dependencies>
-
- <dependency>
- <groupId>com.aliyungroupId>
- <artifactId>aliyun-java-sdk-coreartifactId>
- <version>4.5.16version>
- dependency>
- <dependency>
- <groupId>com.aliyungroupId>
- <artifactId>aliyun-java-sdk-dysmsapiartifactId>
- <version>2.1.0version>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- <scope>compilescope>
- dependency>
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.4.2version>
- dependency>
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.20version>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>fastjsonartifactId>
- <version>1.2.76version>
- dependency>
-
- <dependency>
- <groupId>commons-langgroupId>
- <artifactId>commons-langartifactId>
- <version>2.6version>
- dependency>
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <scope>runtimescope>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druid-spring-boot-starterartifactId>
- <version>1.1.23version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>3.8.1version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-cacheartifactId>
- dependency>
-
-
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <version>2.4.5version>
- plugin>
- plugins>
- build>
- project>
- server:
- port: 8080
- spring:
- application:
- # 应用名称 可选
- name: reggie_take_out_spuer
- datasource:
- druid:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
- username: root
- password: runa#2050
- redis:
- host: 127.0.0.1
- port: 6379
- password: 123456
- database: 0
- cache:
- redis:
- time-to-live: 1800000 # 设置缓存数据的过期时间
- mybatis-plus:
- configuration:
- #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
- map-underscore-to-camel-case: true
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- global-config:
- db-config:
- id-type: ASSIGN_ID
- # 文件上传目录
- reggie:
- path: D:\images\
- 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("项目启动成功~~~");
- }
- }
- package com.runa.reggie.common;
-
- import lombok.Data;
-
- import java.io.Serializable;
- import java.util.HashMap;
- import java.util.Map;
-
-
- /**
- * 通用返回结果,服务端响应的数据最终都会封装成此对象
- * @param
- */
- @Data
- public class R
implements Serializable { -
- private Integer code; //编码:1成功,0和其它数字为失败
-
- private String msg; //错误信息
-
- private T data; //数据
-
- private Map map = new HashMap(); //动态数据
-
- public static
R success(T object) { - R
r = new R(); - r.data = object;
- r.code = 1;
- return r;
- }
-
- public static
R error(String msg) { - R r = new R();
- r.msg = msg;
- r.code = 0;
- return r;
- }
-
- public R
add(String key, Object value) { - this.map.put(key, value);
- return this;
- }
-
- }
- 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 {
-
- @Autowired
- private SetmealService setmealService;
-
- @Autowired
- private CategoryService categoryService;
-
- @Autowired
- private SetmealDishService setmealDishService;
-
- /**
- * 新增套餐
- * @param setmealDto
- * @return
- */
- @PostMapping
- public R
save(@RequestBody SetmealDto setmealDto){ - log.info("套餐信息:{}",setmealDto);
-
- setmealService.saveWithDish(setmealDto);
-
- return R.success("新增套餐成功");
- }
-
- /**
- * 套餐分页查询
- * @param page
- * @param pageSize
- * @param name
- * @return
- */
- @GetMapping("/page")
- public R
page(int page, int pageSize, String name){ - //分页构造器对象
- Page
pageInfo = new Page<>(page,pageSize); - Page
dtoPage = new Page<>(); -
- LambdaQueryWrapper
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");
- List
records = pageInfo.getRecords(); -
- List
list = records.stream().map((item) -> { - SetmealDto setmealDto = new SetmealDto();
- //对象拷贝
- BeanUtils.copyProperties(item,setmealDto);
- //分类id
- Long 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
- */
- @DeleteMapping
- public R
delete(@RequestParam List 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 R
> list(Setmeal setmeal){
- LambdaQueryWrapper
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);
- List
list = setmealService.list(queryWrapper); - return R.success(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.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 {
-
- @Autowired
- private SetmealService setmealService;
-
- @Autowired
- private CategoryService categoryService;
-
- @Autowired
- private SetmealDishService setmealDishService;
-
- /**
- * 新增套餐
- * @param setmealDto
- * @return
- */
- @PostMapping
- public R
save(@RequestBody SetmealDto setmealDto){ - log.info("套餐信息:{}",setmealDto);
-
- setmealService.saveWithDish(setmealDto);
-
- return R.success("新增套餐成功");
- }
-
- /**
- * 套餐分页查询
- * @param page
- * @param pageSize
- * @param name
- * @return
- */
- @GetMapping("/page")
- public R
page(int page, int pageSize, String name){ - //分页构造器对象
- Page
pageInfo = new Page<>(page,pageSize); - Page
dtoPage = new Page<>(); -
- LambdaQueryWrapper
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");
- List
records = pageInfo.getRecords(); -
- List
list = records.stream().map((item) -> { - SetmealDto setmealDto = new SetmealDto();
- //对象拷贝
- BeanUtils.copyProperties(item,setmealDto);
- //分类id
- Long 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
- */
- @DeleteMapping
- @CacheEvict(value = "setmealCache" , allEntries = true)
- public R
delete(@RequestParam List 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 R
> list(Setmeal setmeal){
- LambdaQueryWrapper
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);
- List
list = setmealService.list(queryWrapper); - return R.success(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.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 {
-
- @Autowired
- private SetmealService setmealService;
-
- @Autowired
- private CategoryService categoryService;
-
- @Autowired
- private SetmealDishService setmealDishService;
-
- /**
- * 新增套餐
- * @param setmealDto
- * @return
- */
- @PostMapping
- @CacheEvict(value = "setmealCache" , allEntries = true)
- public R
save(@RequestBody SetmealDto setmealDto){ - log.info("套餐信息:{}",setmealDto);
-
- setmealService.saveWithDish(setmealDto);
-
- return R.success("新增套餐成功");
- }
-
- /**
- * 套餐分页查询
- * @param page
- * @param pageSize
- * @param name
- * @return
- */
- @GetMapping("/page")
- public R
page(int page, int pageSize, String name){ - //分页构造器对象
- Page
pageInfo = new Page<>(page,pageSize); - Page
dtoPage = new Page<>(); -
- LambdaQueryWrapper
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");
- List
records = pageInfo.getRecords(); -
- List
list = records.stream().map((item) -> { - SetmealDto setmealDto = new SetmealDto();
- //对象拷贝
- BeanUtils.copyProperties(item,setmealDto);
- //分类id
- Long 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
- */
- @DeleteMapping
- @CacheEvict(value = "setmealCache" , allEntries = true)
- public R
delete(@RequestParam List 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 R
> list(Setmeal setmeal){
- LambdaQueryWrapper
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);
- List
list = setmealService.list(queryWrapper); - return R.success(list);
-
- }
-
- }
- http://localhost:8080/front/page/login.html
-
- http://localhost:8080/backend/index.html

