更新了商品管理模块

This commit is contained in:
chen-xin-zhi 2024-12-03 20:06:00 +08:00
parent 0dd9f106b0
commit d93bb58d60
9 changed files with 199 additions and 16 deletions

View File

@ -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<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
// 指定kv的序列化方式
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}

View File

@ -108,6 +108,7 @@ public class GoodController {
}
// 向商品表插入服务类商品的基本信息
Good good = new Good();
good.setIsGoodType(0);
BeanUtils.copyProperties(serviceGoodAddRequest, good);
// 校验
goodService.validGood(good, false);

View File

@ -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 {
/**
* 商品编号
*/

View File

@ -78,7 +78,7 @@ public class GoodServiceImpl extends ServiceImpl<GoodMapper, Good> 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<GoodMapper, Good> 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, "节日参数错误");
}

View File

@ -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<String> listTest() {
ListOperations<String, String> listOperations = redisTemplate.opsForList();
listOperations.leftPush("list", "hello");
listOperations.leftPush("list", "World");
listOperations.leftPush("list", "Java");
List<String> list = listOperations.range("list", 0, 2);
return list;
}
@GetMapping("/set")
public Set<String> setTest() {
SetOperations<String, String> setOperations = redisTemplate.opsForSet();
setOperations.add("set", "Hello");
setOperations.add("set", "Hello");
setOperations.add("set", "World");
setOperations.add("set", "World");
Set<String> set = setOperations.members("set");
return set;
}
@GetMapping("/zset")
public Set<String> zSetTest() {
ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("zset", "Hello", 1);
zSetOperations.add("zset", "World", 3);
zSetOperations.add("zset", "Hi", 2);
Set<String> zset = zSetOperations.range("zset", 0, 2);
return zset;
}
@GetMapping("/hash")
public String hashTest() {
HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
hashOperations.put("key", "hashKey", "hello");
String value = hashOperations.get("key", "hashKey");
return value;
}
}

View File

@ -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");
// }
//
//}
}

View File

@ -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

View File

@ -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);
// }
}

View File

@ -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();
}
}