This commit is contained in:
yuanteng0011 2025-03-10 09:57:50 +08:00
parent d54d93a78c
commit 23f638582e
31 changed files with 4 additions and 1512 deletions

View File

@ -62,6 +62,10 @@ springdoc:
server:
port: 9092
# ssl:
# key-store: classpath:carboner.cn.jks
# key-store-password: 6gsn1hke4m4f7
# key-store-type: JKS
servlet:
context-path: /api
# cookie 30 天过期

View File

@ -1,13 +0,0 @@
package com.cultural.heritage;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class HeritageApplicationTests {
@Test
void contextLoads() {
}
}

View File

@ -1,60 +0,0 @@
package com.cultural.heritage.aopTest;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.lang.reflect.Method;
import java.util.Set;
@Component
@Aspect
@Slf4j
public class AopConfig {
@Pointcut("@annotation(com.cultural.heritage.aopTest.HasRole)")
public void pointcut(){}
@Before("pointcut()")
public void before(JoinPoint joinPoint) {
System.out.println("before-----------------");
//获取到HttpServletRequest
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = servletRequestAttributes.getRequest();
String username = request.getParameter("username");
//获取当前用户角色的集合
Set<String> userRoles = CacheManager.USER_ROLE_MAP.get(username);
//获取当前请求的方法的签名
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//反射获取当前被调用的方法
Method method = signature.getMethod();
//判断当前方法是否有hasRole注解
//如果有判断用户是否具有注解属性中要求的角色
//如果没有hasRole注解那么说明方法不需要判断用户的角色可以匿名访问
HasRole hasRole = method.getDeclaredAnnotation(HasRole.class);
System.out.println("hasRole:" + hasRole.value());
System.out.println("userRoles:" + userRoles);
log.info("----------------");
if(!userRoles.contains(hasRole.value())) {
throw new RuntimeException("用户没有访问的权限");
}
}
}

View File

@ -1,31 +0,0 @@
package com.cultural.heritage.aopTest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/aop")
public class AopController {
//调用这个方法需要用户具有admin的角色
@HasRole("admin")
@GetMapping("query1")
public String query1() {
return "query1 需要 admin 角色";
}
//调用这个方法需要用户具有user的角色
@HasRole("user")
@GetMapping("query2")
public String query2() {
return "query2 需要 user 角色";
}
// 任何用户都可以访问
@GetMapping("query3")
public String query3() {
return "query3 可以匿名访问";
}
}

View File

@ -1,22 +0,0 @@
package com.cultural.heritage.aopTest;
import com.google.common.collect.Sets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CacheManager {
public static final Map<String, Set<String>> USER_ROLE_MAP = new HashMap<>();
static {
//用户zhangsan具有user和admin两个角色
Set<String> roleSet1 = Sets.newHashSet("admin", "user");
USER_ROLE_MAP.put("zhangsan", roleSet1);
Set<String> roleSet2 = Sets.newHashSet("user");
USER_ROLE_MAP.put("lisi", roleSet2);
}
}

View File

@ -1,12 +0,0 @@
package com.cultural.heritage.aopTest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface HasRole {
String value();
}

View File

@ -1,135 +0,0 @@
package com.cultural.heritage.order;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.exception.BusinessException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class OrderIdGenerator {
// 定义日期格式化器精确到秒
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
// 定义序列号最大值
private static final int MAX_SEQUENCE = 999999;
// 当前日期初始化为当天
private static String currentDate = getCurrentDate();
// 自增序列号线程安全
private static final AtomicInteger sequence = new AtomicInteger(0);
// 锁对象用于保护自增序列号的生成
private static final Lock lock = new ReentrantLock();
public static String generateOrderId() {
// 获取当前日期
String today = getCurrentDate();
// 如果日期发生变化重置序列号
if (!today.equals(currentDate)) {
currentDate = today;
sequence.set(0); // 重置序列号
}
// 获取时间戳精确到秒
String timestamp = DATE_FORMAT.format(new Date());
// 获取4位随机数
String randomNumber = generateRandomNumber();
// 获取6位自增序列号并确保不超过最大值
int seq = getNextSequence();
// 格式化序列号为6位
String formattedSequence = String.format("%06d", seq);
// 拼接生成订单号
return timestamp + randomNumber + formattedSequence;
}
// 获取当前日期格式yyyyMMdd
private static String getCurrentDate() {
return new SimpleDateFormat("yyyyMMdd").format(new Date());
}
// 生成4位随机数范围0000到9999
private static String generateRandomNumber() {
int random = (int) (Math.random() * 10000); // 生成0到9999之间的随机数
return String.format("%04d", random); // 格式化为4位
}
// 获取下一个自增序列号使用ReentrantLock来确保线程安全
private static int getNextSequence() {
lock.lock(); // 获取锁
try {
int seq = sequence.incrementAndGet();
if (seq > MAX_SEQUENCE) {
sequence.set(0); // 达到最大值后重置
seq = sequence.incrementAndGet();
}
return seq;
} finally {
lock.unlock(); // 确保在最终释放锁
}
}
// 模拟高并发生成订单号
public static void main(String[] args) throws InterruptedException {
// 创建一个线程池
ExecutorService executorService = Executors.newFixedThreadPool(100);
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
// 记录生成的订单号数量
final int totalOrders = 999999;
final CountDownLatch latch = new CountDownLatch(totalOrders);
// 使用线程池并发生成订单号
for (int i = 0; i < totalOrders; i++) {
executorService.submit(() -> {
// 每个线程生成一个订单号
String orderId = generateOrderId();
map.compute(orderId.substring(orderId.length() - 6), (key, value) -> {
if (value == null) {
return 1;
} else {
return value + 1; // 如果有重复则计数
}
});
System.out.println(orderId.substring(orderId.length() - 6));
latch.countDown(); // 完成一个订单号生成
});
}
// 等待所有订单号生成完成
latch.await();
// 关闭线程池
executorService.shutdown();
System.out.println("所有订单号生成完毕!");
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() > 1) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "订单号重复, 订单号为:" + entry.getKey());
}
}
System.out.println("订单号唯一性验证通过!");
}
}

View File

@ -1,44 +0,0 @@
//package com.cultural.heritage.rabbit;
//
//
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.stereotype.Component;
//
//import java.util.Date;
//
//@Component
//public class Consumer {
//
// @RabbitListener(queues = "myQueue")
// public void receive(String message) {
// System.out.println("接收时间:" + new Date());
// System.out.println("消费者收到消息:" + message);
// }
//
//// @RabbitListener(queues = "myQueue1")
//// public void receive2(String message) {
//// System.out.println("消费者接收消息:" + message);
//// }
//
//// @RabbitListener(queues = "myQueue1")
//// public void receive1(String productId, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long msgId) throws IOException {
//// System.out.println(productId + "开始减少库存");
//// if (productId.equals("产品编号3")) {
//// int num = 10 / 0;
//// }
//// // 手动应答
//// long deliveryTag = msgId;
//// channel.basicAck(deliveryTag, false);
//// System.out.println(productId + "减少库存成功!");
//// }
//
//// @RabbitListener(queues = "myQueue2")
//// public void receive2(String message) {
//// System.out.println("消费者2接收消息" + message);
//// }
////
//// @RabbitListener(queues = "myQueue3")
//// public void receive3(String message) {
//// System.out.println("消费者3接收消息" + message);
//// }
//}

View File

@ -1,47 +0,0 @@
//package com.cultural.heritage.rabbit;
//
//import jakarta.annotation.Resource;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.core.MessageBuilder;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.stereotype.Component;
//
//import java.util.Date;
//
//@Component
//public class Producer {
//
// @Resource
// private RabbitTemplate rabbitTemplate;
//
// private String exchange = "myExchange";
//
//
// public void send(String routingKey, String message) {
// Message msg = MessageBuilder
// .withBody(message.getBytes())
// .setHeader("x-delay", 5000)
// .build();
// System.out.println("发送时间:" + new Date());
// rabbitTemplate.convertAndSend(exchange, routingKey, msg);
// }
//
//
//// public void send(String routingKey, String message, MessageDeliveryMode mode) {
//// Message msg = MessageBuilder
//// .withBody(message.getBytes())
//// .setDeliveryMode(mode) // 指定消息是否持久化
//// .build();
//// rabbitTemplate.convertAndSend(exchange, routingKey, msg);
//// }
//
//// public void send(String message, String routingKey) {
////
//// CorrelationData correlationData = new CorrelationData();
//// correlationData.setId("101"); //UUID.randomUUID().toString()
//// rabbitTemplate.convertAndSend(exchangeName, routingKey, message, correlationData);
////
//// //rabbitTemplate.convertAndSend(exchangeName, routingKey, message);
//// }
//
//}

View File

@ -1,27 +0,0 @@
//package com.cultural.heritage.rabbit;
//
//import jakarta.annotation.Resource;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit4.SpringRunner;
//
//@SpringBootTest
//@RunWith(SpringRunner.class)
//public class TestRabbit {
//
// @Resource
// private Producer producer;
//
// @Test
// public void test() {
// String routingKey = "myKey";
// String message = "Hello, RabbitMQ";
// producer.send(routingKey, message);
// try {
// Thread.sleep(30 * 1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
//}

View File

@ -1,94 +0,0 @@
//package com.cultural.heritage.rabbitmq;
//
//import com.rabbitmq.client.*;
//import org.junit.Test;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.io.IOException;
//import java.util.concurrent.TimeoutException;
//
//@RestController
//public class AppTest {
//
// // 服务器IP
// private String host = "123.249.108.160";
//
// // RabbitMQ端口
// private int port = 5672;
//
// // 用户名
// private String username = "chenxinzhi";
//
// // 密码
// private String password = "yuanteng";
//
// // 虚拟机名字
// private String vhost = "vhost1";
//
// // 交换机名字
// private String exchangeName = "myExchange1";
//
// // 队列名字
// private String queueName = "myQueue1";
//
// // 路由Key
// private String routingKey = "myRouting1";
//
//
// @Test
// public void testProducer() throws IOException, TimeoutException {
//
// // 创建连接工厂连接信道
// ConnectionFactory factory = new ConnectionFactory();
// factory.setHost(host);
// factory.setPort(port);
// factory.setUsername(username);
// factory.setPassword(password);
// factory.setVirtualHost(vhost);
// // 连接
// Connection connection = factory.newConnection();
// // 信道
// Channel channel = connection.createChannel();
// // 创建交换机:直连交换机
// channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT,
// true, false, null);
// // 创建队列
// channel.queueDeclare(queueName, true, false, false, null);
// // 交换机绑定
// channel.queueBind(queueName, exchangeName, routingKey);
// // 发送消息
// String msg = "Hello, RabbitMQ";
// channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());
// // 关闭
// channel.close();
// connection.close();
//
// }
//
//
// @Test
// public void testConsumer() throws IOException, TimeoutException {
// // 创建连接工厂连接信道
// ConnectionFactory factory = new ConnectionFactory();
// factory.setHost(host);
// factory.setPort(port);
// factory.setUsername(username);
// factory.setPassword(password);
// factory.setVirtualHost(vhost);
// // 连接
// Connection connection = factory.newConnection();
// // 信道
// Channel channel = connection.createChannel();
// // 获取消息
// DeliverCallback deliverCallback = (consumerTag, message) -> {
// String msg = new String(message.getBody());
// System.out.println(msg);
// };
// CancelCallback cancelCallback = consumerTag -> {
//
// };
// channel.basicConsume(queueName, true, deliverCallback, cancelCallback);
// }
//
//
//}

View File

@ -1,119 +0,0 @@
//package com.cultural.heritage.rabbitmq;
//
//import com.rabbitmq.client.*;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit4.SpringRunner;
//
//import java.io.IOException;
//import java.util.concurrent.TimeoutException;
//
//@SpringBootTest
//@RunWith(SpringRunner.class)
//public class TestDirect {
//
// // 服务器IP
// private String host = "123.249.108.160";
//
// // RabbitMQ端口
// private int port = 5672;
//
// // 用户名
// private String username = "chenxinzhi";
//
// // 密码
// private String password = "yuanteng";
//
// // 虚拟机名字
// private String vhost = "vhost1";
//
// // 交换机名字
// private String exchangeName = "myExchange1";
//
// // 队列名字
// private String queueName1 = "myQueue1";
//
// private String queueName2 = "myQueue2";
//
// private String queueName3 = "myQueue3";
//
// // 路由Key
// private String routingKey1 = "myRouting1";
//
// private String routingKey2 = "myRouting2";
//
// // 连接对象
// private Connection connection;
//
// // 信道对象
// private Channel channel;
//
//
// public void setUp() throws IOException, TimeoutException {
//
// // 创建连接工厂连接信道
// ConnectionFactory factory = new ConnectionFactory();
// factory.setHost(host);
// factory.setPort(port);
// factory.setUsername(username);
// factory.setPassword(password);
// factory.setVirtualHost(vhost);
// // 连接
// connection = factory.newConnection();
// // 信道
// channel = connection.createChannel();
// }
//
//
// @Test
// public void testProducer() throws IOException, TimeoutException {
// // 创建交换机:直连交换机
// setUp();
// channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT,
// true, false, null);
// // 创建队列
// channel.queueDeclare(queueName1, true, false, false, null);
// channel.queueDeclare(queueName2, true, false, false, null);
// channel.queueDeclare(queueName3, true, false, false, null);
// // 交换机绑定
// channel.queueBind(queueName1, exchangeName, routingKey1);
// channel.queueBind(queueName2, exchangeName, routingKey2);
// channel.queueBind(queueName3, exchangeName, routingKey1);
// // 发送消息
// String msg = "Hello, RabbitMQ";
// channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());
// String msg2 = "RabbitMQ, Hello";
// channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());
// // 关闭
// channel.close();
// connection.close();
//
// }
//
//
// @Test
// public void testConsumer() throws IOException, TimeoutException {
// // 获取消息
// setUp();
// DeliverCallback deliverCallback = (consumerTag, message) -> {
// String msg = new String(message.getBody());
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// };
// CancelCallback cancelCallback = consumerTag -> {
//
// };
// channel.basicConsume(queueName3, true, deliverCallback, cancelCallback);
// }
//
//
//}

View File

@ -1,107 +0,0 @@
//package com.cultural.heritage.rabbitmq;
//
//import com.rabbitmq.client.*;
//import org.junit.Test;
//import org.springframework.boot.test.context.SpringBootTest;
//
//import java.io.IOException;
//import java.util.concurrent.TimeoutException;
//
//@SpringBootTest
//public class TestFanout {
//
// // 服务器IP
// private String host = "123.249.108.160";
//
// // RabbitMQ端口
// private int port = 5672;
//
// // 用户名
// private String username = "chenxinzhi";
//
// // 密码
// private String password = "yuanteng";
//
// // 虚拟机名字
// private String vhost = "vhost1";
//
// // 交换机名字
// private String exchangeName = "myExchange2";
//
// // 队列名字
// private String queueName1 = "myQueue1";
//
// private String queueName2 = "myQueue2";
//
// private String queueName3 = "myQueue3";
//
// // 路由Key
// private String routingKey1 = "myRouting1";
//
// private String routingKey2 = "myRouting2";
//
// // 连接对象
// private Connection connection;
//
// // 信道对象
// private Channel channel;
//
//
// public void setUp() throws IOException, TimeoutException {
//
// // 创建连接工厂连接信道
// ConnectionFactory factory = new ConnectionFactory();
// factory.setHost(host);
// factory.setPort(port);
// factory.setUsername(username);
// factory.setPassword(password);
// factory.setVirtualHost(vhost);
// // 连接
// connection = factory.newConnection();
// // 信道
// channel = connection.createChannel();
// }
//
//
// @Test
// public void testProducer() throws IOException, TimeoutException {
// setUp();
// // 创建交换机:直连交换机
// channel.exchangeDeclare(exchangeName, BuiltinExchangeType.FANOUT,
// true, false, null);
// // 创建队列
// channel.queueDeclare(queueName1, true, false, false, null);
// channel.queueDeclare(queueName2, true, false, false, null);
// channel.queueDeclare(queueName3, true, false, false, null);
// // 交换机绑定
// channel.queueBind(queueName1, exchangeName, "");
// channel.queueBind(queueName2, exchangeName, "");
// channel.queueBind(queueName3, exchangeName, "");
// // 发送消息
// String msg = "Hello, RabbitMQ";
// channel.basicPublish(exchangeName, "", null, msg.getBytes());
// // 关闭
// channel.close();
// connection.close();
//
// }
//
//
// @Test
// public void testConsumer() throws IOException, TimeoutException {
// setUp();
// // 获取消息
// DeliverCallback deliverCallback = (consumerTag, message) -> {
// String msg = new String(message.getBody());
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// };
// CancelCallback cancelCallback = consumerTag -> {
//
// };
// channel.basicConsume(queueName1, true, deliverCallback, cancelCallback);
// }
//
//
//}

View File

@ -1,118 +0,0 @@
//package com.cultural.heritage.rabbitmq;
//
//import com.rabbitmq.client.*;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit4.SpringRunner;
//
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.concurrent.TimeoutException;
//
//@SpringBootTest
//@RunWith(SpringRunner.class)
//public class TestHeader {
//
// // 服务器IP
// private String host = "123.249.108.160";
//
// // RabbitMQ端口
// private int port = 5672;
//
// // 用户名
// private String username = "chenxinzhi";
//
// // 密码
// private String password = "yuanteng";
//
// // 虚拟机名字
// private String vhost = "vhost1";
//
// // 交换机名字
// private String exchangeName = "myExchange4";
//
// // 队列名字
// private String queueName1 = "myQueue1";
//
//
// // 连接对象
// private Connection connection;
//
// // 信道对象
// private Channel channel;
//
//
// public void setUp() throws IOException, TimeoutException {
//
// // 创建连接工厂连接信道
// ConnectionFactory factory = new ConnectionFactory();
// factory.setHost(host);
// factory.setPort(port);
// factory.setUsername(username);
// factory.setPassword(password);
// factory.setVirtualHost(vhost);
// // 连接
// connection = factory.newConnection();
// // 信道
// channel = connection.createChannel();
// }
//
//
// @Test
// public void testProducer() throws IOException, TimeoutException {
// setUp();
// // 创建交换机:直连交换机
// channel.exchangeDeclare(exchangeName, BuiltinExchangeType.HEADERS,
// true, false, null);
// // 创建队列
// channel.queueDeclare(queueName1, true, false, false, null);
//
// // 发送消息
// String msg = "Hello, RabbitMQ";
// Map map = new HashMap();
// map.put("one", "one2");
// map.put("two", "two");
// AMQP.BasicProperties props = new AMQP.BasicProperties().builder().headers(map).build();
// channel.basicPublish(exchangeName, "", props, msg.getBytes());
// // 关闭
// channel.close();
// connection.close();
//
// }
//
//
// @Test
// public void testConsumer() throws IOException, TimeoutException {
// setUp();
// // 获取消息
// DeliverCallback deliverCallback = (consumerTag, message) -> {
// String msg = new String(message.getBody());
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// };
// CancelCallback cancelCallback = consumerTag -> {
//
// };
// // 绑定队列和交换机
// Map map = new HashMap();
// map.put("x-match", "any");
// map.put("one", "one");
// map.put("two", "two");
// channel.queueBind(queueName1, exchangeName, "", map);
// // 接收消息
// channel.basicConsume(queueName1, true, deliverCallback, cancelCallback);
// }
//
//
// @Test
// public void test() throws IOException, TimeoutException, InterruptedException {
// testProducer();
// Thread.sleep(5000);
// testConsumer();
// }
//
//
//}

View File

@ -1,117 +0,0 @@
//package com.cultural.heritage.rabbitmq;
//
//import com.rabbitmq.client.*;
//import org.junit.Test;
//import org.springframework.boot.test.context.SpringBootTest;
//
//import java.io.IOException;
//import java.util.concurrent.TimeoutException;
//
//@SpringBootTest
//public class TestTopic {
//
// // 服务器IP
// private String host = "123.249.108.160";
//
// // RabbitMQ端口
// private int port = 5672;
//
// // 用户名
// private String username = "chenxinzhi";
//
// // 密码
// private String password = "yuanteng";
//
// // 虚拟机名字
// private String vhost = "vhost1";
//
// // 交换机名字
// private String exchangeName = "myExchange3";
//
// // 队列名字
// private String queueName1 = "myQueue1";
//
// private String queueName2 = "myQueue2";
//
// private String queueName3 = "myQueue3";
//
// // 路由Key
// private String routingKey1 = "myRouting.*";
//
// private String routingKey2 = "myRouting.b";
//
// private String routingKey3 = "myRouting.#";
//
// // 连接对象
// private Connection connection;
//
// // 信道对象
// private Channel channel;
//
//
// public void setUp() throws IOException, TimeoutException {
//
// // 创建连接工厂连接信道
// ConnectionFactory factory = new ConnectionFactory();
// factory.setHost(host);
// factory.setPort(port);
// factory.setUsername(username);
// factory.setPassword(password);
// factory.setVirtualHost(vhost);
// // 连接
// connection = factory.newConnection();
// // 信道
// channel = connection.createChannel();
// }
//
//
// @Test
// public void testProducer() throws IOException, TimeoutException {
// setUp();
// // 创建交换机:直连交换机
// channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC,
// true, false, null);
// // 创建队列
// channel.queueDeclare(queueName1, true, false, false, null);
// channel.queueDeclare(queueName2, true, false, false, null);
// channel.queueDeclare(queueName3, true, false, false, null);
// // 交换机绑定
// channel.queueBind(queueName1, exchangeName, routingKey1);
// channel.queueBind(queueName2, exchangeName, routingKey2);
// channel.queueBind(queueName3, exchangeName, routingKey3);
// // 发送消息
// String msg = "Hello, RabbitMQ";
// channel.basicPublish(exchangeName, "myRouting.a.b", null, msg.getBytes());
// // 关闭
// channel.close();
// connection.close();
//
// }
//
//
// @Test
// public void testConsumer() throws IOException, TimeoutException {
// setUp();
// // 获取消息
// DeliverCallback deliverCallback = (consumerTag, message) -> {
// String msg = new String(message.getBody());
// System.out.println(msg);
// System.out.println(msg);
// System.out.println(msg);
// };
// CancelCallback cancelCallback = consumerTag -> {
//
// };
// channel.basicConsume(queueName3, true, deliverCallback, cancelCallback);
// }
//
//
// @Test
// public void test() throws IOException, TimeoutException, InterruptedException {
// testProducer();
// Thread.sleep(5000);
// testConsumer();
// }
//
//
//}

View File

@ -1,21 +0,0 @@
//package com.cultural.heritage.rabbitmqtest;
//
//import org.springframework.amqp.rabbit.annotation.Exchange;
//import org.springframework.amqp.rabbit.annotation.Queue;
//import org.springframework.amqp.rabbit.annotation.QueueBinding;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.stereotype.Component;
//
//@Component
//public class ConsumerTest {
//
// @RabbitListener(bindings = @QueueBinding(
// value = @Queue("myQueue"),
// exchange = @Exchange(name = "myExchange", delayed = "true"),
// key = "myKey"
// ))
// public void listenDelayMessage(String msg) {
// System.out.println("接收延时消息:" + msg);
// }
//
//}

View File

@ -1,44 +0,0 @@
//package com.cultural.heritage.rabbitmqtest;
//
//
//import jakarta.annotation.Resource;
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.amqp.AmqpException;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.core.MessagePostProcessor;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit4.SpringRunner;
//
//@SpringBootTest
//@RunWith(SpringRunner.class)
//public class RabbitMQTest {
//
// @Resource
// private RabbitTemplate rabbitTemplate;
//
//
// @Test
// public void testDelayMessage() {
// String exchange = "myExchange";
// String message = "Hello, delay message!";
//
// rabbitTemplate.convertAndSend(exchange, "myKey", message, new MessagePostProcessor() {
// @Override
// public Message postProcessMessage(Message message) throws AmqpException {
// // 添加延迟消息属性
// message.getMessageProperties().setDelay(5000);
// return message;
// }
// });
// try {
// Thread.sleep(30 * 1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
//
//
//
//}

View File

@ -1,62 +0,0 @@
package com.cultural.heritage.redis;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JedisTest {
private Jedis jedis;
/**
* 初始化Jedis实例
*/
@Before
public void init() {
//指定Redis服务器的IP地址和端口号
jedis = new Jedis("127.0.0.1", 6379);
System.out.println("连接成功");
}
@Test
public void redisJedisTest() {
//操作字符串
//方法名与操作原生redis的命令一致
jedis.set("dec", "hello");
String dec = jedis.get("dec");
System.out.println(dec);
//操作list
jedis.lpush("data", "v1", "v2", "v3");
jedis.rpush("data", "v4", "v5", "v6");
List<String> data = jedis.lrange("data", 0, -1);
for (String s : data) {
System.out.print(s + " ");
}
System.out.println();
//操作hash
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("id", "1");
hashMap.put("name", "huhai");
hashMap.put("age", "24");
jedis.hset("object", hashMap);
Map<String, String> object = jedis.hgetAll("object");
object.forEach((key, value) -> System.out.print(key + "=" + value + " "));
}
/**
* 关闭Jedis连接
*/
@After
public void close() {
jedis.close();
System.out.println("连接已关闭");
}
}

View File

@ -1,32 +0,0 @@
package com.cultural.heritage.redis;
import com.cultural.heritage.mapper.GoodMapper;
import com.cultural.heritage.model.entity.Good;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class TestRedis {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private GoodMapper goodMapper;
@Test
public void test() {
List<Good> goodList = goodMapper.selectList(null);
Map<String, String> map = goodList.stream().collect(Collectors.toMap
(good -> good.getId().toString(), good -> good.getName()));
stringRedisTemplate.opsForHash().putAll("test", map);
stringRedisTemplate.opsForHash().get("test", "238");
}
}

View File

@ -1,52 +0,0 @@
//package com.cultural.heritage.config;
//
//import jakarta.annotation.PostConstruct;
//import jakarta.annotation.Resource;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.core.ReturnedMessage;
//import org.springframework.amqp.rabbit.connection.CorrelationData;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.stereotype.Component;
//
//@Component
//public class MessageConfirmConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {
//
// @Resource
// private RabbitTemplate rabbitTemplate;
//
// @PostConstruct
// public void init() {
// // 设置发布确认交当前对象处理-this
// rabbitTemplate.setConfirmCallback(this);
//
// // 设置消息退回交当前对象处理-this
// rabbitTemplate.setReturnsCallback(this);
//
// // 设置是否保留退回的消息默认保留
//// rabbitTemplate.setMandatory(true);
// }
//
// @Override
// public void confirm(CorrelationData correlationData, boolean ack, String cause) {
// if (ack) {
// System.out.println("mq收到消息: = " + correlationData.getId());
// } else {
// System.out.println("mq未收到消息" + cause);
// }
// }
//
// @Override
// public void returnedMessage(ReturnedMessage returned) {
// Message message = returned.getMessage();
// String replyText = returned.getReplyText();
// int replyCode = returned.getReplyCode();
// String exchange = returned.getExchange();
// String routingKey = returned.getRoutingKey();
// System.out.println("消息被退回:");
// System.out.println("消息:" + new String(message.getBody()));
// System.out.println("原因:" + replyText);
// System.out.println("应答码:" + replyCode);
// System.out.println("交换机:" + exchange);
// System.out.println("路由:" + routingKey);
// }
//}

View File

@ -1,77 +0,0 @@
//package com.cultural.heritage.config;
//
//import org.springframework.amqp.core.*;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//@Configuration
//public class RabbitConfig {
//
// private String exchange = "myExchange";
//
// private String queue = "myQueue";
////
//// private String queueName2 = "myQueue2";
////
//// private String queueName3 = "myQueue3";
////
// private String routingKey = "myKey";
////
//// private String routingKey2 = "routingKey.b";
////
//// private String routingKey3 = "routingKey.#";
//
// @Bean("myExchange")
// public org.springframework.amqp.core.Exchange exchange() {
// return ExchangeBuilder
// .directExchange(exchange)
// .delayed()
// .build();
// }
//
// @Bean("myQueue")
// public Queue queue() {
// return QueueBuilder.durable(queue).build();
//// return QueueBuilder.durable(queueName1).lazy().build();
// }
//
//// @Bean("myQueue2")
//// public Queue queue2() {
//// return QueueBuilder.durable(queueName2).build();
//// }
////
//// @Bean("myQueue3")
//// public Queue queue3() {
//// return QueueBuilder.durable(queueName3).build();
//// }
//
// @Bean
// public Binding binding(@Qualifier("myExchange") Exchange exchange, @Qualifier("myQueue") Queue queue) {
// return BindingBuilder.bind(queue).to(exchange).with(routingKey).noargs();
// }
//
//
//// @Bean
//// public Binding binding1(@Qualifier("myExchange1") Exchange exchange, @Qualifier("myQueue1") Queue queue) {
//// Map map = new HashMap();
//// map.put("x-match", "any");
//// map.put("one", "one");
//// map.put("two", "two");
//// return BindingBuilder.bind(queue).to(exchange).with("").and(map);
//// }
////
//// @Bean
//// public Binding binding2(@Qualifier("myExchange1") Exchange exchange, @Qualifier("myQueue2") Queue queue) {
//// return BindingBuilder.bind(queue).to(exchange).with(routingKey2).noargs();
//// }
////
//// @Bean
//// public Binding binding3(@Qualifier("myExchange1") Exchange exchange, @Qualifier("myQueue3") Queue queue) {
//// return BindingBuilder.bind(queue).to(exchange).with(routingKey3).noargs();
//// }
//
//
//
//
//}

View File

@ -1,34 +0,0 @@
package com.cultural.heritage.controller.user;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cultural.heritage.utils.UrlUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/wx")
@Slf4j
@Tag(name = "微信登录接口")
public class WeChatController {
@GetMapping("/login")
public JSONObject list(String code) {
String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
Map<String,String> requestUrlParam = new HashMap<>();
requestUrlParam.put("appid","wx3f968a09e31d6bed"); //开发者设置中的appId
requestUrlParam.put("secret", "847bdda7c2b01e88d59948b9ba50ef8d"); //开发者设置中的appSecret
requestUrlParam.put("js_code", code); //小程序调用wx.login返回的code
requestUrlParam.put("grant_type", "authorization_code");//默认参数
JSONObject jsonObject = JSON.parseObject(UrlUtil.sendPost(requestUrl, requestUrlParam));
return jsonObject;
}
}

View File

@ -1,12 +0,0 @@
package com.cultural.heritage.test;
import java.util.ArrayList;
import java.util.List;
public class A {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
System.out.println(list.isEmpty());
}
}

View File

@ -1,39 +0,0 @@
package com.cultural.heritage.test;
class Animal {
String name;
int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Bird extends Animal {
public Bird(String name, int age) {
super(name, age);
}
public void fly() {
System.out.println(name + " 能飞行");
}
public void showBirdInfo() {
System.out.println("鸟的名称: " + name);
}
public static void main(String[] args) {
Bird bird = new Bird("麻雀", 2);
bird.showBirdInfo();
bird.fly();
}
}

View File

@ -1,11 +0,0 @@
package com.cultural.heritage.test;
public class B {
static {
System.out.println("静态块已执行");
}
public static void main(String[] args) {
new B(); // 创建对象观察静态块是否执行
}
}

View File

@ -1,25 +0,0 @@
package com.cultural.heritage.test;
import java.math.BigDecimal;
public class Hello {
public static void main(String[] args) {
// Jedis jedis = new Jedis("localhost", 6379);
// System.out.println("连接成功");
// System.out.println("服务正在运行:" + jedis.ping());
//
// System.out.println(jedis.get("name"));
// jedis.lpush("site-list", "Runoob");
// jedis.lpush("site-list", "Google");
// jedis.lpush("site-list", "Taobao");
// List<String> list = jedis.lrange("site-list", 0 ,2);
// for(int i=0; i<list.size(); i++) {
// System.out.println("列表项为: "+list.get(i));
// }
BigDecimal minPrice = new BigDecimal(10);
BigDecimal maxPrice = new BigDecimal(20);
System.out.println(minPrice.compareTo(maxPrice));
}
}

View File

@ -1,18 +0,0 @@
package com.cultural.heritage.test;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date1 = new Date();
Date date2 = new Date(date1.getTime() + 1000); // date2 date1 1
if (date1.before(date2)) {
System.out.println("date1 早于 date2");
}
if (date1.after(date2)) {
System.out.println("date1 晚于 date2");
}
}
}

View File

@ -1,54 +0,0 @@
package com.cultural.heritage.test;
import cn.hutool.core.date.DateUtil;
public class Test {
public static void main(String[] args) {
// Date date = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// String format = sdf.format(date);
// System.out.println(format);
//
// List<Order> list = new ArrayList<>();
// System.out.println(ObjectUtils.isEmpty(list));
// Date date = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// String format = sdf.format(date);
// System.out.println(format);
// System.out.println(StringUtils.isBlank(null));
//
// double num = 3.1415926;
// System.out.printf("%.2f", num); // 输出3.14
// System.out.println(StringUtils.isAnyBlank("333", "333", null, "dfs"));
// System.out.println(StringUtils.isAnyBlank(null));
// System.out.println(ObjectUtils.anyNull(null));
// Integer quantity = 10;
// BigDecimal bigDecimal = BigDecimal.valueOf(quantity);
// System.out.println(bigDecimal);
// DateTime date = DateUtil.date();
// System.out.println(date);
// String formatDate = DateUtil.formatDate(date);
// System.out.println(formatDate);
// String str = "2024-11-21 22:43:33";
// DateTime parse = DateUtil.parse(str);
// System.out.println(parse);
// Map<Long, Good> map = new HashMap<>();
// System.out.println(map.get(5));
// List<Long> ids = new ArrayList<>();
// Long number = 1L;
// ids.add(1L);
// System.out.println(ids.get(0) == number);
// System.out.println(Objects.equals(new A().num, new B().num));
System.out.println(DateUtil.date());
}
}

View File

@ -1,25 +0,0 @@
package com.cultural.heritage.test;
public class TestHashCode {
private int num;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestHashCode that = (TestHashCode) o;
return num == that.num;
}
// @Override
// public int hashCode() {
// return Objects.hash(num);
// }
public static void main(String[] args) {
TestHashCode testHashCode1 = new TestHashCode();
TestHashCode testHashCode2 = new TestHashCode();
System.out.println(testHashCode1.equals(testHashCode2));
testHashCode1.hashCode();
}
}

View File

@ -1,30 +0,0 @@
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

@ -1,30 +0,0 @@
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();
}
}