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
  • 相关阅读:
    array_map()与array_shift()搭配使用 PK array_column()函数
    Educational Codeforces Round 8 D. Magic Numbers
    hdu 1171 Big Event in HDU
    hdu 2844 poj 1742 Coins
    hdu 3591 The trouble of Xiaoqian
    hdu 2079 选课时间
    hdu 2191 珍惜现在,感恩生活 多重背包入门题
    hdu 5429 Geometric Progression 高精度浮点数(java版本)
    【BZOJ】1002: [FJOI2007]轮状病毒 递推+高精度
    hdu::1002 A + B Problem II
  • 原文地址:https://www.cnblogs.com/XJJD/p/8548533.html
Copyright © 2011-2022 走看看