SpringBoot构建电商基础秒杀项目总结-交易模块开发

Posted hequnwang10

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot构建电商基础秒杀项目总结-交易模块开发相关的知识,希望对你有一定的参考价值。

交易模块开发

1、创建数据库

CREATE TABLE `order_info`  (
  `id` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `user_id` int(11) NOT NULL DEFAULT 0,
  `item_id` int(11) NOT NULL DEFAULT 0,
  `item_price` decimal(10, 2) NOT NULL DEFAULT 0.00,
  `amount` int(11) NOT NULL DEFAULT 0,
  `order_price` decimal(40, 2) NOT NULL DEFAULT 0.00,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;

生成交易流水号

CREATE TABLE `sequence_info`  (
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `current_value` int(11) NOT NULL DEFAULT 0,
  `step` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`name`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Compact;

2、重写mybatis-generator.xml

        <table tableName="order_info" domainObjectName="OrderDO"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
	     <table tableName="sequence_info" domainObjectName="SequenceDO"
	            enableCountByExample="false"
	            enableUpdateByExample="false"
	            enableDeleteByExample="false"
	            enableSelectByExample="false"
	            selectByExampleQueryId="false" ></table>

重新运行一下mybatis-generator

3、设计用户下单的交易模型

OrderModel.java

//用户下单的交易模型
public class OrderModel {
    //交易单号,例如2019052100001212,使用string类型
    private String id;

    //购买的用户id
    private Integer userId;

    //购买的商品id
    private Integer itemId;

    //购买时商品的单价
    private BigDecimal itemPrice;

    //购买数量
    private Integer amount;

    //购买金额
    private BigDecimal orderPrice;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getItemId() {
        return itemId;
    }

    public void setItemId(Integer itemId) {
        this.itemId = itemId;
    }

    public BigDecimal getItemPrice() {
        return itemPrice;
    }

    public void setItemPrice(BigDecimal itemPrice) {
        this.itemPrice = itemPrice;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public BigDecimal getOrderPrice() {
        return orderPrice;
    }

    public void setOrderPrice(BigDecimal orderPrice) {
        this.orderPrice = orderPrice;
    }
}

还有前端的几个界面的代码,这里也重新完全给出。自行对应。秒杀模块的前端也在这里

交易模型管理——前端界面

1、login.html

<html>
<head>
    <meta charset="UTF-8">
    <link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <link href="static/assets/global/plugins/css/component.css" rel="stylesheet" type="text/css"/>
    <link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>
    <script src="static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
    <title>Title</title>
</head>
<body class="login">
    <div class="content">
        <h3 class="form-title">用户登录</h3>
        <div class="form-group">
            <label class="control-label">手机号</label>
            <div>
                <input class="form-control" type="text" placeholder="手机号" name="telphone" id="telphone"/>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">密码</label>
            <div>
                <input class="form-control" type="password" placeholder="密码" name="password" id="password"/>
            </div>
        </div>
        <div class="form-actions">
            <button class="btn blue" id="login" type="submit">
                登录
            </button>
            <button class="btn green" id="register" type="submit">
                注册
            </button>
        </div>
    </div>
</body>
<script>
    jQuery(document).ready(function () {
        //绑定注册按钮的click事件用于跳转到注册页面
        $("#register").on("click",function () {
            window.location.href = "getotp.html";
        });
        //绑定登录按钮的click事件用于登录
        $("#login").on("click",function () {

            var telphone=$("#telphone").val();
            var password=$("#password").val();
            if (telphone==null || telphone=="") {
                alert("手机号不能为空");
                return false;
            }
            if (password==null || password=="") {
                alert("密码不能为空");
                return false;
            }
            //映射到后端@RequestMapping(value = "/login", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
            $.ajax({
                type:"POST",
                contentType:"application/x-www-form-urlencoded",
                url:"http://localhost:8090/user/login",
                data:{
                    "telphone":telphone,
                    "password":password
                },
                //允许跨域请求
                xhrFields:{withCredentials:true},
                success:function (data) {
                    if (data.status=="success") {
                        alert("登录成功");
                        window.location.href="listitem.html"
                    }else {
                        alert("登录失败,原因为" + data.data.errMsg);
                    }
                },
                error:function (data) {
                    alert("登录失败,原因为"+data.responseText);
                }
            });
            return false;
        });
    });
</script>
</html>

2、register.html

<html>
<head>
  <meta charset="UTF-8">
  <link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
  <link href="static/assets/global/plugins/css/component.css" rel="stylesheet" type="text/css"/>
  <link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>
  <script src="static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
  <title>Title</title>
</head>
<body class="login">
<div class="content">
  <h3 class="form-title">用户注册</h3>
  <div class="form-group">
    <label class="control-label">手机号</label>
    <div>
      <input class="form-control" type="text" placeholder="手机号" name="telphone" id="telphone"/>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label">验证码</label>
    <div>
      <input class="form-control" type="text" placeholder="验证码" name="otpCode" id="otpCode"/>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label">用户昵称</label>
    <div>
      <input class="form-control" type="text" placeholder="用户昵称" name="name" id="name"/>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label">性别</label>
    <div>
      <input class="form-control" type="text" placeholder="性别" name="gender" id="gender"/>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label">年龄</label>
    <div>
      <input class="form-control" type="text" placeholder="年龄" name="age" id="age"/>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label">密码</label>
    <div>
      <input class="form-control" type="password" placeholder="密码" nameSpringBoot构建电商基础秒杀项目总结

SpringBoot构建电商基础秒杀项目总结

SpringBoot构建电商基础秒杀项目总结

SpringBoot构建电商基础秒杀项目总结

SpringBoot构建电商基础秒杀项目总结-商品模块开发

SpringBoot构建电商基础秒杀项目总结-商品列表