From d93bb58d60057baaca79fe0f98ec8c3dfe4f0c49 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Tue, 3 Dec 2024 20:06:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E5=95=86=E5=93=81?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cultural/heritage/config/RedisConfig.java | 26 ++++++ .../controller/good/GoodController.java | 1 + .../cultural/heritage/model/entity/Good.java | 3 +- .../service/good/impl/GoodServiceImpl.java | 8 +- .../cultural/heritage/test/GoodHandler.java | 89 +++++++++++++++++++ .../com/cultural/heritage/test/TestRedis.java | 25 +++--- src/main/resources/application.yml | 3 +- .../com/cultural/heritage/thread/Counter.java | 30 +++++++ .../com/cultural/heritage/thread/Test.java | 30 +++++++ 9 files changed, 199 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/cultural/heritage/config/RedisConfig.java create mode 100644 src/main/java/com/cultural/heritage/test/GoodHandler.java create mode 100644 src/test/java/com/cultural/heritage/thread/Counter.java create mode 100644 src/test/java/com/cultural/heritage/thread/Test.java diff --git a/src/main/java/com/cultural/heritage/config/RedisConfig.java b/src/main/java/com/cultural/heritage/config/RedisConfig.java new file mode 100644 index 0000000..40e151e --- /dev/null +++ b/src/main/java/com/cultural/heritage/config/RedisConfig.java @@ -0,0 +1,26 @@ +package com.cultural.heritage.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.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@Configuration +public class RedisConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(factory); + + // 指定kv的序列化方式 + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); + redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + + return redisTemplate; + } +} diff --git a/src/main/java/com/cultural/heritage/controller/good/GoodController.java b/src/main/java/com/cultural/heritage/controller/good/GoodController.java index e9a3917..c7ae49d 100644 --- a/src/main/java/com/cultural/heritage/controller/good/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/good/GoodController.java @@ -108,6 +108,7 @@ public class GoodController { } // 向商品表插入服务类商品的基本信息 Good good = new Good(); + good.setIsGoodType(0); BeanUtils.copyProperties(serviceGoodAddRequest, good); // 校验 goodService.validGood(good, false); diff --git a/src/main/java/com/cultural/heritage/model/entity/Good.java b/src/main/java/com/cultural/heritage/model/entity/Good.java index 3c29abb..5e370b5 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Good.java +++ b/src/main/java/com/cultural/heritage/model/entity/Good.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.io.Serial; +import java.io.Serializable; import java.math.BigDecimal; import java.util.Date; @@ -18,7 +19,7 @@ import java.util.Date; */ @TableName(value = "good") @Data -public class Good { +public class Good implements Serializable { /** * 商品编号 */ diff --git a/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java b/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java index d6581ca..12b20a1 100644 --- a/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/good/impl/GoodServiceImpl.java @@ -78,7 +78,7 @@ public class GoodServiceImpl extends ServiceImpl implements Go String introDetail = good.getIntroDetail(); String detailImg = good.getDetailImg(); String label = good.getLabel(); - Integer inventory = good.getInventory(); +// Integer inventory = good.getInventory(); Integer festivalOrder = good.getFestivalOrder(); BigDecimal price = good.getPrice(); @@ -90,9 +90,9 @@ public class GoodServiceImpl extends ServiceImpl implements Go if (StringUtils.isAnyBlank(type, goodImg, intro, introDetail, detailImg, label, name)) { throw new BusinessException(ErrorCode.PARAMS_ERROR, "存在参数为空"); } - if (ObjectUtils.isEmpty(inventory) || inventory < 1) { - throw new BusinessException(ErrorCode.PARAMS_ERROR, "库存量不能小于1"); - } +// if (ObjectUtils.isEmpty(inventory) || inventory < 1) { +// throw new BusinessException(ErrorCode.PARAMS_ERROR, "库存量不能小于1"); +// } if (ObjectUtils.isEmpty(festivalOrder) || festivalOrder < 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR, "节日参数错误"); } diff --git a/src/main/java/com/cultural/heritage/test/GoodHandler.java b/src/main/java/com/cultural/heritage/test/GoodHandler.java new file mode 100644 index 0000000..a36ee5c --- /dev/null +++ b/src/main/java/com/cultural/heritage/test/GoodHandler.java @@ -0,0 +1,89 @@ +package com.cultural.heritage.test; + +import com.cultural.heritage.model.dto.CommonRequest; +import com.cultural.heritage.model.entity.Good; +import com.cultural.heritage.service.good.GoodService; +import jakarta.annotation.Resource; +import org.springframework.data.redis.core.*; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Set; + +@RestController +public class GoodHandler { + + @Resource + private RedisTemplate redisTemplate; + + + @Resource + private GoodService goodService; + + @PostMapping("/set") + public void set(@RequestBody CommonRequest commonRequest) { + Long id = commonRequest.getId(); + Good good = goodService.getById(id); + redisTemplate.opsForValue().set("good", good); + } + + @GetMapping("/get/{key}") + public Good get(@PathVariable("key") String key) { + return (Good) redisTemplate.opsForValue().get(key); + } + + @DeleteMapping("/delete/{key}") + public boolean delete(@PathVariable("key") String key) { + redisTemplate.delete(key); + return redisTemplate.hasKey(key); + } + + @GetMapping("/string") + public String stringTest() { + redisTemplate.opsForValue().set("str", "Hello World!"); + return (String) redisTemplate.opsForValue().get("str"); + } + + @GetMapping("/list") + public List listTest() { + ListOperations listOperations = redisTemplate.opsForList(); + listOperations.leftPush("list", "hello"); + listOperations.leftPush("list", "World"); + listOperations.leftPush("list", "Java"); + List list = listOperations.range("list", 0, 2); + return list; + } + + + @GetMapping("/set") + public Set setTest() { + SetOperations setOperations = redisTemplate.opsForSet(); + setOperations.add("set", "Hello"); + setOperations.add("set", "Hello"); + setOperations.add("set", "World"); + setOperations.add("set", "World"); + Set set = setOperations.members("set"); + return set; + } + + + @GetMapping("/zset") + public Set zSetTest() { + ZSetOperations zSetOperations = redisTemplate.opsForZSet(); + zSetOperations.add("zset", "Hello", 1); + zSetOperations.add("zset", "World", 3); + zSetOperations.add("zset", "Hi", 2); + Set zset = zSetOperations.range("zset", 0, 2); + return zset; + } + + @GetMapping("/hash") + public String hashTest() { + HashOperations hashOperations = redisTemplate.opsForHash(); + hashOperations.put("key", "hashKey", "hello"); + String value = hashOperations.get("key", "hashKey"); + return value; + } + + +} diff --git a/src/main/java/com/cultural/heritage/test/TestRedis.java b/src/main/java/com/cultural/heritage/test/TestRedis.java index f5af862..230298a 100644 --- a/src/main/java/com/cultural/heritage/test/TestRedis.java +++ b/src/main/java/com/cultural/heritage/test/TestRedis.java @@ -1,13 +1,18 @@ package com.cultural.heritage.test; -//@Component -//public class TestRedis { -// -// @Resource -// private StringRedisTemplate stringRedisTemplate; -// -// @Resource -// private GoodMapper goodMapper; +import com.cultural.heritage.mapper.GoodMapper; +import jakarta.annotation.Resource; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +@Component +public class TestRedis { + + @Resource + private StringRedisTemplate stringRedisTemplate; + + @Resource + private GoodMapper goodMapper; // // @PostConstruct // public void test() { @@ -18,5 +23,5 @@ package com.cultural.heritage.test; // stringRedisTemplate.opsForHash().putAll("test", map); // stringRedisTemplate.opsForHash().get("test", "238"); // } -// -//} + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 0bb2203..11d3faf 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -17,8 +17,9 @@ spring: data: redis: port: 6379 - host: localhost + host: 123.249.108.160 database: 0 + password: yuanteng servlet: multipart: max-file-size: 10MB diff --git a/src/test/java/com/cultural/heritage/thread/Counter.java b/src/test/java/com/cultural/heritage/thread/Counter.java new file mode 100644 index 0000000..31b916a --- /dev/null +++ b/src/test/java/com/cultural/heritage/thread/Counter.java @@ -0,0 +1,30 @@ +package com.cultural.heritage.thread; + +public class Counter { + + private int num = 10; + + public synchronized void print() throws InterruptedException { + for (int i = 0; i < 10; i++) { + String name = Thread.currentThread().getName(); + if (name.equals("e1")) { + System.out.println("------------------------------"); + } else { + System.out.println("#"); + } + Thread.sleep(100); + } + } +// public void deposit(int amount) { +// balance += amount; +// System.out.println(Thread.currentThread().getName() + ":" + balance); +// } +// +// public void withdraw(int amount) { +// balance -= amount; +// System.out.println(Thread.currentThread().getName() + ":" + balance); +// } + + + +} diff --git a/src/test/java/com/cultural/heritage/thread/Test.java b/src/test/java/com/cultural/heritage/thread/Test.java new file mode 100644 index 0000000..4e722f4 --- /dev/null +++ b/src/test/java/com/cultural/heritage/thread/Test.java @@ -0,0 +1,30 @@ +package com.cultural.heritage.thread; + +public class Test { + public static void main(String[] args) throws InterruptedException { + Counter counter1 = new Counter(); + Counter counter2 = new Counter(); + + Thread e1 = new Thread(() -> { + try { + counter1.print(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }, "e1"); + + Thread e2 = new Thread(() -> { + try { + counter1.print(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }, "e2"); + + e1.start(); + e2.start(); + + e1.join(); + e2.join(); + } +}