06/07 15:43
This commit is contained in:
parent
6c16abed71
commit
1fae68d6b6
4
pom.xml
4
pom.xml
@ -17,6 +17,10 @@
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sdk</groupId>
|
||||
<artifactId>alipay-sdk-java</artifactId>
|
||||
|
37
src/main/java/cn/czyx007/mt/config/RedisConfig.java
Normal file
37
src/main/java/cn/czyx007/mt/config/RedisConfig.java
Normal file
@ -0,0 +1,37 @@
|
||||
package cn.czyx007.mt.config;
|
||||
|
||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* @author : 张宇轩
|
||||
* @createTime : 2023/6/7 - 10:08
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class RedisConfig extends CachingConfigurerSupport {
|
||||
@Bean
|
||||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
|
||||
redisTemplate.setConnectionFactory(connectionFactory);
|
||||
// 使用 GenericFastJsonRedisSerializer 替换默认序列化
|
||||
GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
|
||||
// 设置key和value的序列化规则
|
||||
redisTemplate.setKeySerializer(new GenericToStringSerializer<>(Object.class));
|
||||
redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
|
||||
// 设置hashKey和hashValue的序列化规则
|
||||
redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
|
||||
redisTemplate.setHashValueSerializer(genericFastJsonRedisSerializer);
|
||||
// 设置支持事物
|
||||
redisTemplate.setEnableTransactionSupport(true);
|
||||
redisTemplate.afterPropertiesSet();
|
||||
return redisTemplate;
|
||||
}
|
||||
}
|
@ -11,12 +11,15 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -41,10 +44,15 @@ public class DishController {
|
||||
@Autowired
|
||||
private DishFlavorService dishFlavorService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
//保存菜品和口味信息
|
||||
@PostMapping
|
||||
public R<String> saveDishFlavor(@RequestBody DishDTO dishDTO){
|
||||
dishService.saveDishFlavor(dishDTO);
|
||||
//清除当前分类下的菜品信息
|
||||
redisTemplate.delete("dish_" + dishDTO.getCategoryId() + "_1");
|
||||
return R.success("菜品保存成功!");
|
||||
}
|
||||
|
||||
@ -110,6 +118,8 @@ public class DishController {
|
||||
dishFlavorService.remove(luw);
|
||||
dishService.removeById(id);
|
||||
}
|
||||
//清除所有的菜品缓存数据
|
||||
redisTemplate.delete("dish_*");
|
||||
return R.success("删除成功");
|
||||
}
|
||||
|
||||
@ -131,6 +141,13 @@ public class DishController {
|
||||
|
||||
@PutMapping
|
||||
public R<String> updateDishFlavor(@RequestBody DishDTO dishDTO){
|
||||
//先查询当前菜品的分类id,清除当前分类的redis数据
|
||||
Dish dish = dishService.getById(dishDTO.getId());
|
||||
redisTemplate.delete("dish_" + dish.getCategoryId() + "_1");
|
||||
//查询更新的菜品的分类id,清除更新的分类的redis数据
|
||||
if(!dish.getCategoryId().equals(dishDTO.getCategoryId())){
|
||||
redisTemplate.delete("dish_" + dishDTO.getCategoryId() + "_1");
|
||||
}
|
||||
dishService.updateDishFlavor(dishDTO);
|
||||
return R.success("修改菜品信息成功");
|
||||
}
|
||||
@ -147,13 +164,22 @@ public class DishController {
|
||||
// }
|
||||
@GetMapping("/list")
|
||||
private R<List<DishDTO>> list(Dish dish){
|
||||
//先从redis查询当前是否有分类下的菜品数据,如果有直接返回
|
||||
//string结构
|
||||
String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus();
|
||||
List<DishDTO> dishDTOList = (List<DishDTO>) redisTemplate.opsForValue().get(key);
|
||||
//如果有,直接返回数据
|
||||
if(!CollectionUtils.isEmpty(dishDTOList)){
|
||||
return R.success(dishDTOList);
|
||||
}
|
||||
//如果没有,去mysql数据库查询
|
||||
LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
|
||||
lqw.eq(Dish::getCategoryId, dish.getCategoryId())
|
||||
//status为空时对应后台页面,非空则对应客户端页面
|
||||
.eq(dish.getStatus()!=null, Dish::getStatus, dish.getStatus())
|
||||
.orderByDesc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
|
||||
List<Dish> dishList = dishService.list(lqw);
|
||||
List<DishDTO> dishDTOList = dishList.stream().map(item -> {
|
||||
dishDTOList = dishList.stream().map(item -> {
|
||||
DishDTO dishDTO = new DishDTO();
|
||||
BeanUtils.copyProperties(item, dishDTO);
|
||||
//封装口味数据
|
||||
@ -163,6 +189,8 @@ public class DishController {
|
||||
dishDTO.setFlavors(dishFlavorList);
|
||||
return dishDTO;
|
||||
}).collect(Collectors.toList());
|
||||
//把在msql查询的数据放入redis
|
||||
redisTemplate.opsForValue().set(key, dishDTOList,1, TimeUnit.HOURS);
|
||||
return R.success(dishDTOList);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import cn.czyx007.mt.utils.ValidateCodeUtils;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.mail.EmailException;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -15,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 用户信息(User)表控制层
|
||||
@ -32,6 +34,9 @@ public class UserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
//发送验证码
|
||||
@PostMapping("/sendMsg")
|
||||
public R<String> sendMsg(@RequestBody User user, HttpSession session){
|
||||
@ -41,8 +46,8 @@ public class UserController {
|
||||
//发送短信,让用户接受验证码
|
||||
try {
|
||||
SendEmailUtils.sendAuthCodeEmail(user.getPhone(), code);
|
||||
//把验证码保存到session
|
||||
session.setAttribute("code", code);
|
||||
//把验证码保存到redis,1分钟有效
|
||||
redisTemplate.opsForValue().set(user.getPhone() + ":code", code, 1, TimeUnit.MINUTES);
|
||||
return R.success("验证码发送成功");
|
||||
} catch (EmailException e) {
|
||||
e.printStackTrace();
|
||||
@ -56,9 +61,12 @@ public class UserController {
|
||||
//获取前端传递的数据
|
||||
String phone = (String) map.get("phone");
|
||||
String code = (String) map.get("code");
|
||||
//将前端传来的code与session中的code比较
|
||||
Object sessionCode = session.getAttribute("code");
|
||||
if(code!=null && code.equals(sessionCode)){
|
||||
//将前端传来的code与redis中的code比较
|
||||
Object redisCode = redisTemplate.opsForValue().get(phone + ":code");
|
||||
if(redisCode == null){
|
||||
return R.error("验证码过期,请重新发送验证码");
|
||||
}
|
||||
if(code!=null && code.equals(redisCode)){
|
||||
//根据手机号查询用户信息
|
||||
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
|
||||
lqw.eq(User::getPhone, phone);
|
||||
@ -72,6 +80,8 @@ public class UserController {
|
||||
}
|
||||
//将用户id存到session中
|
||||
session.setAttribute("user", user.getId());
|
||||
//验证码使用之后,从redis中删除
|
||||
redisTemplate.delete(phone + ":code");
|
||||
return R.success("用户登录成功");
|
||||
}
|
||||
return R.error("验证码错误");
|
||||
|
@ -21,6 +21,11 @@ spring:
|
||||
wall:
|
||||
config:
|
||||
start-transaction-allow: true
|
||||
#springboot整合redis
|
||||
redis:
|
||||
host: localhost
|
||||
port: 6379
|
||||
database: 0
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射 address_book ---> AddressBook
|
||||
@ -33,9 +38,9 @@ mybatis-plus:
|
||||
|
||||
#用于发送邮箱验证码的账户和密码
|
||||
email:
|
||||
#gmail
|
||||
# userName:
|
||||
# password:
|
||||
#qq
|
||||
#gmail
|
||||
# userName:
|
||||
# password:
|
||||
#qq
|
||||
userName:
|
||||
password:
|
||||
password:
|
||||
|
@ -3,13 +3,40 @@ package cn.czyx007.mt;
|
||||
import cn.czyx007.mt.utils.SendEmailUtils;
|
||||
import org.apache.commons.mail.EmailException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@SpringBootTest
|
||||
class ApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
@Test
|
||||
void contextLoads() throws EmailException {
|
||||
SendEmailUtils.sendAuthCodeEmail("xxx@qq.com", "不要再卷啦");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test1(){
|
||||
//在0号数据库中设置k1-v1,过期时间1分钟
|
||||
redisTemplate.opsForValue().set("k1", "v1", 1, TimeUnit.MINUTES);
|
||||
String value = (String) redisTemplate.opsForValue().get("k1");
|
||||
System.out.println("value = " + value);
|
||||
}
|
||||
|
||||
@Test
|
||||
void test2(){
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", 1001);
|
||||
map.put("name", "郭德纲");
|
||||
map.put("address", "上海");
|
||||
redisTemplate.opsForHash().putAll("user:1001", map);
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user