From d9e8587dfa51b53bea8042cba4f29507e3a8f710 Mon Sep 17 00:00:00 2001
From: zyx <1029606625@qq.com>
Date: Thu, 8 Jun 2023 17:21:24 +0800
Subject: [PATCH] 06/08 17:21
---
pom.xml | 2 +
src/main/java/cn/czyx007/mt/Application.java | 10 +++-
.../cn/czyx007/mt/config/AlipayConfig.java | 2 +-
.../cn/czyx007/mt/config/RedisConfig.java | 52 ++++++++++++++++++-
.../mt/controller/SetmealController.java | 4 ++
5 files changed, 66 insertions(+), 4 deletions(-)
diff --git a/pom.xml b/pom.xml
index fca7d29..7e485c8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -91,6 +91,8 @@
org.springframework.boot
spring-boot-maven-plugin
+
+ cn.czyx007.mt.Application
org.projectlombok
diff --git a/src/main/java/cn/czyx007/mt/Application.java b/src/main/java/cn/czyx007/mt/Application.java
index 27fe782..3cc0fd5 100644
--- a/src/main/java/cn/czyx007/mt/Application.java
+++ b/src/main/java/cn/czyx007/mt/Application.java
@@ -2,14 +2,22 @@ package cn.czyx007.mt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
+import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass = true)
-public class Application {
+@EnableCaching
+public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
+ @Override
+ protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
+ return builder.sources(this.getClass());
+ }
}
diff --git a/src/main/java/cn/czyx007/mt/config/AlipayConfig.java b/src/main/java/cn/czyx007/mt/config/AlipayConfig.java
index 6a44532..dde7eca 100644
--- a/src/main/java/cn/czyx007/mt/config/AlipayConfig.java
+++ b/src/main/java/cn/czyx007/mt/config/AlipayConfig.java
@@ -12,7 +12,7 @@ public class AlipayConfig {
// 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://商户网关地址/alipay.trade.wap.pay-JAVA-UTF-8/notify_url.jsp";
// 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址
- public static String return_url = "http://localhost:8080/order/afterPayOrder";
+ public static String return_url = "http://101.43.84.101:8080/order/afterPayOrder";
// 请求网关地址
public static String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
// 编码
diff --git a/src/main/java/cn/czyx007/mt/config/RedisConfig.java b/src/main/java/cn/czyx007/mt/config/RedisConfig.java
index 4e5734c..98976ae 100644
--- a/src/main/java/cn/czyx007/mt/config/RedisConfig.java
+++ b/src/main/java/cn/czyx007/mt/config/RedisConfig.java
@@ -1,14 +1,30 @@
package cn.czyx007.mt.config;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
+import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
+import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
+import org.springframework.cache.CacheManager;
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.cache.RedisCacheConfiguration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.cache.RedisCacheWriter;
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;
+import org.springframework.data.redis.serializer.*;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
/**
* @author : 张宇轩
@@ -34,4 +50,36 @@ public class RedisConfig extends CachingConfigurerSupport {
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
+
+ @Bean
+ public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
+ RedisSerializer redisSerializer = new StringRedisSerializer();
+
+ ObjectMapper om = new ObjectMapper();
+ // 解决jackson2无法反序列化LocalDateTime的问题
+ //LocalDatetime序列化
+ JavaTimeModule timeModule = new JavaTimeModule();
+ timeModule.addDeserializer(LocalDate.class,
+ new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+ timeModule.addDeserializer(LocalDateTime.class,
+ new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
+ timeModule.addSerializer(LocalDate.class,
+ new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+ timeModule.addSerializer(LocalDateTime.class,
+ new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
+ om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+ om.registerModule(timeModule);
+
+ //设置GenericJackson2JsonRedisSerializer可以将R类的对象类型也能进行JSON保存
+ om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
+ GenericJackson2JsonRedisSerializer gen = new GenericJackson2JsonRedisSerializer(om);
+
+ //配置序列化
+ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
+ RedisCacheConfiguration redisCacheConfiguration = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
+ .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(gen));
+ return RedisCacheManager
+ .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
+ .cacheDefaults(redisCacheConfiguration).build();
+ }
}
\ No newline at end of file
diff --git a/src/main/java/cn/czyx007/mt/controller/SetmealController.java b/src/main/java/cn/czyx007/mt/controller/SetmealController.java
index 81b769a..ee913bd 100644
--- a/src/main/java/cn/czyx007/mt/controller/SetmealController.java
+++ b/src/main/java/cn/czyx007/mt/controller/SetmealController.java
@@ -15,6 +15,8 @@ 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.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
@@ -47,6 +49,7 @@ public class SetmealController {
private DishService dishService;
//添加套餐信息
+ @CacheEvict(value = "setmealCache", allEntries = true)
@PostMapping
public R saveSetmealDish(@RequestBody SetmealDTO setmealDTO){
setmealService.saveSetmealDish(setmealDTO);
@@ -127,6 +130,7 @@ public class SetmealController {
// List list = setmealService.list(lqw);
// return R.success(list);
// }
+ @Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
public R> list(Setmeal setmeal){
LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
lqw.eq(Setmeal::getCategoryId, setmeal.getCategoryId())