package com.alatus.mall.cart.web; import com.alatus.mall.cart.service.CartService; import com.alatus.mall.cart.vo.Cart; import com.alatus.mall.cart.vo.CartItem; import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import java.util.concurrent.ExecutionException; @Controller public class CartController { @Autowired private CartService cartService; @Autowired private AmqpAdmin amqpAdmin; @GetMapping("/cart.html") public String cartListPage(Model model) throws ExecutionException, InterruptedException { Cart cart = cartService.getCart(); model.addAttribute("cart",cart); return "cartList"; } @GetMapping("/deleteItem") public String deleteItem(@RequestParam("skuId")Long skuId){ cartService.deleteItem(skuId); return "redirect:http://cart.alatusmall.com/cart.html"; } @GetMapping("/createExchange") public void createExchange(){ // 声明交换机 DirectExchange directExchange = new DirectExchange("helloWorld",true,false); amqpAdmin.declareExchange(directExchange); // 队列不能有排他性,要都能连接到这个队列才是正确的 Queue helloJava = new Queue("helloJava", true, true,false); amqpAdmin.declareQueue(helloJava); Binding binding = new Binding("helloJava", Binding.DestinationType.QUEUE,"helloWorld","hello",null); amqpAdmin.declareBinding(binding); } @GetMapping("/countItem") public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num){ cartService.changeItemCount(skuId,num); return "redirect:http://cart.alatusmall.com/cart.html"; } @GetMapping("/addToCart") public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes model) throws ExecutionException, InterruptedException { if(num > 0){ cartService.addToCart(skuId,num); // RedirectAttributes的addAttribute会自动拼串,自动以参数的形式携带数据 // RedirectAttributes的addFlashAttribute()会保存在session里面,但是仅可以取一次值 model.addAttribute("skuId",skuId); return "redirect:http://cart.alatusmall.com/addToCartSuccess.html"; } return "redirect:http://cart.alatusmall.com/addToCartSuccess.html"; } @GetMapping("/checkItem") public String checkItem(@RequestParam("skuId")Long skuId,@RequestParam("check")Integer check){ cartService.checkItem(skuId,check); return "redirect:http://cart.alatusmall.com/cart.html"; } @GetMapping("/addToCartSuccess.html") public String addToCartSuccessPage(@RequestParam("skuId")Long skuId,Model model){ CartItem cartItem = cartService.getCartItem(skuId); model.addAttribute("item",cartItem); return "success"; } }
package com.alatus.mall.cart.web; import com.alatus.mall.cart.service.CartService; import com.alatus.mall.cart.vo.Cart; import com.alatus.mall.cart.vo.CartItem; import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.DirectExchange; import org.springframework.amqp.core.Queue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import java.util.concurrent.ExecutionException; @Controller public class CartController { @Autowired private CartService cartService; @Autowired private AmqpAdmin amqpAdmin; @GetMapping("/cart.html") public String cartListPage(Model model) throws ExecutionException, InterruptedException { Cart cart = cartService.getCart(); model.addAttribute("cart",cart); return "cartList"; } @GetMapping("/deleteItem") public String deleteItem(@RequestParam("skuId")Long skuId){ cartService.deleteItem(skuId); return "redirect:http://cart.alatusmall.com/cart.html"; } @GetMapping("/createExchange") public void createExchange(){ // 声明交换机 DirectExchange directExchange = new DirectExchange("helloWorld",true,false); amqpAdmin.declareExchange(directExchange); // 队列不能有排他性,要都能连接到这个队列才是正确的 Queue helloJava = new Queue("helloJava", true, true,false); amqpAdmin.declareQueue(helloJava); Binding binding = new Binding("helloJava", Binding.DestinationType.QUEUE,"helloWorld","hello",null); amqpAdmin.declareBinding(binding); } @GetMapping("/countItem") public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num){ cartService.changeItemCount(skuId,num); return "redirect:http://cart.alatusmall.com/cart.html"; } @GetMapping("/addToCart") public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes model) throws ExecutionException, InterruptedException { if(num > 0){ cartService.addToCart(skuId,num); // RedirectAttributes的addAttribute会自动拼串,自动以参数的形式携带数据 // RedirectAttributes的addFlashAttribute()会保存在session里面,但是仅可以取一次值 model.addAttribute("skuId",skuId); return "redirect:http://cart.alatusmall.com/addToCartSuccess.html"; } return "redirect:http://cart.alatusmall.com/addToCartSuccess.html"; } @GetMapping("/checkItem") public String checkItem(@RequestParam("skuId")Long skuId,@RequestParam("check")Integer check){ cartService.checkItem(skuId,check); return "redirect:http://cart.alatusmall.com/cart.html"; } @GetMapping("/addToCartSuccess.html") public String addToCartSuccessPage(@RequestParam("skuId")Long skuId,Model model){ CartItem cartItem = cartService.getCartItem(skuId); model.addAttribute("item",cartItem); return "success"; } }
spring.application.name=Mall-cart spring.rabbitmq.host=192.168.56.10 spring.rabbitmq.port=5672 spring.rabbitmq.virtual-host=/ server.port=30000 spring.redis.host=192.168.56.10 spring.redis.port=6379 alatusmall.thread.keep-alive-time=10 alatusmall.thread.blocking-deque=100000 alatusmall.thread.core-size=20 alatusmall.thread.max-size=200 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=Mall-cart spring.rabbitmq.host=192.168.56.10 spring.rabbitmq.port=5672 spring.rabbitmq.virtual-host=/ server.port=30000 spring.redis.host=192.168.56.10 spring.redis.port=6379 alatusmall.thread.keep-alive-time=10 alatusmall.thread.blocking-deque=100000 alatusmall.thread.core-size=20 alatusmall.thread.max-size=200 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
package com.alatus.mall.cart; import org.springframework.amqp.rabbit.annotation.EnableRabbit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @EnableDiscoveryClient @EnableFeignClients @EnableRabbit public class MallCartApplication { public static void main(String[] args) { SpringApplication.run(MallCartApplication.class, args); } }
package com.alatus.mall.cart; import org.springframework.amqp.rabbit.annotation.EnableRabbit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @EnableDiscoveryClient @EnableFeignClients @EnableRabbit public class MallCartApplication { public static void main(String[] args) { SpringApplication.run(MallCartApplication.class, args); } }
本文转载自: https://blog.csdn.net/2201_75960169/article/details/142624463
版权归原作者 旧约Alatus 所有, 如有侵权,请联系我们删除。
版权归原作者 旧约Alatus 所有, 如有侵权,请联系我们删除。