Java秒杀电商实战

Java秒杀电商实战

实践过程

  1. Spring Boot的环境搭建
    通过IDEA创建springboot项目,FIle->New->Project->Spring initializr(选择mybatis、redis、Mysql、Thymaleaf的依赖)->Finish

  2. 集成Thymeleaf,Result结果封装
    在配置文件中加入

    spring.thymeleaf.prefix=classpath:/templates/
    spring.thymeleaf.suffix=.html
    spring.thymeleaf.cache=false
    spring.thymeleaf.servlet.content-type=text/html
    spring.thymeleaf.enabled=true
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.mode= HTML5
    

    前两行加上后,controller层中返回的字符串会到resources/temples下映射。后面的几行是配置网页的信息格式。
    controller层的映射

    @Controller
    @RequestMapping("/demo")
    public class SimpleController {
    
        @RequestMapping("/thymeleaf")
        public String thymeleaf(Model model){
    
            model.addAttribute("name","joshua");#为属性name添加joshua值
    
            return  "hello";
        }
    

    接下来需要加入Result结果封装(抽离出的小框架),有两个类一个是CodeMsg另一个是Result。CodeMsg中对状态码和状态信息做处理,如

    public class CodeMsg {
        
        private int code;
        private String msg;
    
        public static CodeMsg SUCCESS = new CodeMsg(0, "success");
        public static CodeMsg  ERROR = new CodeMsg(500, "服务端异常");
    
        private CodeMsg(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }
        
        public int getCode() {
            return code;
        }
        public String getMsg() {
            return msg;
        }
    }
    

    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;
        }
    
        /**
         * 将返回消息CodeMsg封装到本类中
         * */
        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;
        }
        
    }
    
  3. 集成Mybatis+Druid
    添加完依赖后,须注意去redis的配置文件(redis.windows.conf或redis.windows-service.conf)中修改密码,否则会提示未设置密码

    # use a very strong password otherwise it will be very easy to break.
    #
    requirepass 123456
    
    # Command renaming.
    #
    # It is possible to change the name of dangerous commands in a shared
    

    注意改完后,需要将requirepass前面的#和“ ”(空格)删掉,否则启动redis时cmd会提示Invalid argument during startup: unknown conf file parameter : requirepass。
    application.properties配置:

    #mybatis
    mybatis.type-aliases-package=com.example.demo.domain
    mybatis.configuration.map-underscore-to-camel-case=true
    mybatis.configuration.default-fetch-size=100
    mybatis.configuration.default-statement-timeout=3000
    mybatis.mapper-locations=classpath:com/example/demo/dao/*.xml
    
    #druid
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/miaosha?useSSL=false&serverTimezone=UTC&autoReconnect=true
    spring.datasource.username=root
    spring.datasource.password=1234567890
    spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
    spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.filters= stat
    spring.datasource.maxActive=2
    spring.datasource.initialSize= 1
    spring.datasource.maxWait= 60000
    spring.datasource.minldle= 1
    spring.datasource.timeBetweenEvictionRunsMillis= 60000
    spring.datasource.minEvictableldleTimeMillis= 300000
    spring.datasource.validationQuery=select 'x'
    spring.datasource.testWhileldle= true
    spring.datasource.testOnBorrow= false
    spring.datasource.testOnReturn= false
    spring.datasource.poolPreparedStatements =true
    spring.datasource.maxOpenPreparedStatements=20
    
    #redis
    redis.host=127.0.0.1
    redis.port=6379
    redis.timeout=3
    redis.password=123456
    redis.poolMaxTotal=10
    redis.poolMaxldle=10
    redis.poolMaxWait=3
    
  4. 集成Jedis+Redis的安装+通用缓存Key封装
    Redis的安装(此处去github下载,redis官方没有windows版本):https://github.com/microsoftarchive/redis/releases
    下载完解压后进入在cmd中进入redis的安装目录,并输入redis-server.exe redis.windows.conf创建临时redis服务器。
    集成和缓存:
    在RedisConfig里建立成员变量接受配置的对应数据信息,在RedisService中实现Redis服务,通过JedisPool获取Redis对象,用@Bean将JedisPool注入Spring容器,通过KeyPrefix 区分模块,对key进行封装,避免key被覆盖,采用方式是:接口->抽象类->实现类。

项目源码见https://github.com/Micheal12575/miaosha2

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值