zoukankan      html  css  js  c++  java
  • 高并发秒杀系统方案(项目框架搭建)

    项目框架搭建:

     DemoController:

    package com.imooc.miaosha.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.imooc.miaosha.result.CodeMsg;
    import com.imooc.miaosha.result.Result;
    
    @Controller
    @RequestMapping("/demo")
    public class DemoController {
        
             @RequestMapping("/")
            @ResponseBody
            String home() {
                return "Hello World!";
            }
             //1.rest api json输出 2.页面
             @RequestMapping("/hello")
            @ResponseBody
            public Result<String> hello() {
                 return Result.success("hello,imooc");
               // return new Result(0, "success", "hello,imooc");
            }
             
             @RequestMapping("/helloError")
            @ResponseBody
            public Result<String> helloError() {
                 return Result.error(CodeMsg.SERVER_ERROR);
                 //return new Result(500102, "XXX");
            }
             
             @RequestMapping("/thymeleaf")
            public String  thymeleaf(Model model) {
                 model.addAttribute("name", "Joshua");
                 return "hello";
            }
             
    }

    CodeMsg:

    package com.imooc.miaosha.result;
    
    public class CodeMsg {
        private int code;
        private String msg;
        
        //通用异常
        public static CodeMsg SUCCESS = new CodeMsg(0, "success");
        public static CodeMsg SERVER_ERROR = new CodeMsg(500100, "服务端异常");
        //登录模块 5002XX
        
        //商品模块 5003XX
        
        //订单模块 5004XX
        
        //秒杀模块 5005XX
        
        
        private CodeMsg(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }
        
        public int getCode() {
            return code;
        }
        public String getMsg() {
            return msg;
        }
    }

    Result:

    package com.imooc.miaosha.result;
    
    public class Result<T> {
        private int code;
        private String msg;
        private T data;
    
        /**
         * 成功时候的调用
         * */
        public static <T> Result<T> success(T data){
            return new  Result<T>(data);
        }
        
        /**
         * 失败时候的调用
         * */
        public static <T> Result<T> error(CodeMsg cm){
            return new  Result<T>(cm);
        }
        
        private Result(T data) {
            this.code = 0;
            this.msg = "success";
            this.data = data;
        }
        
        private Result(CodeMsg cm) {
            if(cm == null) {
                return;
            }
            this.code = cm.getCode();
            this.msg = cm.getMsg();
        }
    
        public int getCode() {
            return code;
        }
        public String getMsg() {
            return msg;
        }
        public T getData() {
            return data;
        }
    }

    MainApplication:

    package com.imooc.miaosha;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MainApplication {
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(MainApplication.class, args);
        }
    }

    hello.html:

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>hello</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <p th:text="'hello:'+${name}" ></p>
    </body>
    </html>

    application.properties:

    spring.thymeleaf.cache=false
    spring.thymeleaf.content-type=text/html
    spring.thymeleaf.enabled=true
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.mode=HTML5
    spring.thymeleaf.prefix=classpath:/templates/
    spring.thymeleaf.suffix=.html
  • 相关阅读:
    Windows Server 2008 R2域控组策略设置禁用USB
    Windows Server 2008 R2组策略设置计算机配置和用户配置
    Windows Server 2008 R2父域管理员与子域管理员相互登录访问
    转载:如何处理浏览器的断网情况?
    转载:浏览器缓存库设计总结(localStorage/indexedDB)
    手写启动一个本地服务器的命令行工具
    Node.js-核心模块-zlib
    使用console.log打印公司招聘信息和字符画
    转载:准备刷 leetcode 了,才发现自己连时间复杂度都不懂
    转载:前端通信那些事儿
  • 原文地址:https://www.cnblogs.com/XJJD/p/8548533.html
Copyright © 2011-2022 走看看