12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package com.free.controller;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.free.config.CustomizationException;
- import com.free.config.ExceptionEnum;
- import com.free.config.ResponseFormat;
- import com.free.entity.Test;
- import com.free.mq.MqListeners;
- import com.free.service.TestService;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
- @RestController
- @RequestMapping("/test")
- public class TestController {
- @Autowired
- private TestService testService;
- @Autowired
- RabbitTemplate rabbitTemplate; // 使用RabbitTemplate,这提供了接收/发送等等方法
- @GetMapping("/mq/send")
- public Object mqSendTest() {
- String messageId = String.valueOf(UUID.randomUUID());
- String messageData = "message: M A N ";
- String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
- Map<String, Object> manMap = new HashMap<>();
- manMap.put("messageId", messageId);
- manMap.put("messageData", messageData);
- manMap.put("createTime", createTime);
- rabbitTemplate.convertAndSend(MqListeners.exName, MqListeners.queueName + "1", manMap);
- return ResponseFormat.success();
- }
- /**
- * 列表查询
- *
- * @return
- */
- @GetMapping()
- public Object list() {
- List list = testService.list();
- return ResponseFormat.success(list);
- }
- /**
- * 创建数据
- *
- * @param test
- * @return
- */
- @PostMapping()
- public Object save(@RequestBody Test test) {
- boolean result = testService.save(test);
- return ResponseFormat.success((result));
- }
- /**
- * 修改数据
- *
- * @param id
- * @param test
- * @return
- */
- @PostMapping("/{id}")
- public Object update(@PathVariable long id, @RequestBody Test test) {
- QueryWrapper<Test> qw = new QueryWrapper<Test>();
- qw.eq("id", id);
- Long num = testService.count(qw);
- if (num <= 0)
- throw new CustomizationException(ExceptionEnum.NOT_FOUND);
- testService.updateById(test);
- Test newData = testService.getById(id);
- return ResponseFormat.success(newData);
- }
- @DeleteMapping("/{id}")
- public Object delete(@PathVariable long id) {
- QueryWrapper<Test> qw = new QueryWrapper<Test>();
- qw.eq("id", id);
- Long num = testService.count(qw);
- if (num <= 0)
- throw new CustomizationException(ExceptionEnum.NOT_FOUND);
- testService.removeById(id);
- return ResponseFormat.success();
- }
- }
|