• 购物车系列教材 (五)- 订单模块


    在购物车页面,增加一个创建订单的超链 

    通过创建订单,在Order表里新增一条数据,同时把session中的订单条目都保存到数据库中。

    步骤1:SQL
    步骤2:Order
    步骤3:OrderItem
    步骤4:OrderDAO
    步骤5:OrderItemDAO
    步骤6:listOrderItem.jsp
    步骤7:OrderCreateServlet
    步骤8:配置web.xml
    步骤9:测试

    步骤 1 : SQL

    创建order表,里面有一个uid字段用于表明该订单属于哪个用户
    注: order是关键字,不能直接用作表名,通常的做法是加一个下划线order_

    创建orderitem,表里有id,pid,num,oid等字段。 分别表示主键,商品对应的id,购买数量以及订单id。

    create table order_(

      id int AUTO_INCREMENT,

      uid int,

      primary key(id)

    );

    create table orderitem(

      id int AUTO_INCREMENT,

      pid int,

      num int,

      oid int,

      primary key(id)

    );

    步骤 2 : Order

    OrderItem类似的,会有一个User属性,而不是使用int类型的uid。

    package bean;

    public class Order {

        int id;

        User user;

        public int getId() {

            return id;

        }

        public void setId(int id) {

            this.id = id;

        }

        public User getUser() {

            return user;

        }

        public void setUser(User user) {

            this.user = user;

        }

    }

    步骤 3 : OrderItem

    OrderItem在原来的基础上,增加一个Order属性

    package bean;

    public class OrderItem {

        private int id;

        private Product product;

        private int num;

        private Order order;

        public Order getOrder() {

            return order;

        }

        public void setOrder(Order order) {

            this.order = order;

        }

        public int getId() {

            return id;

        }

        public void setId(int id) {

            this.id = id;

        }

        public Product getProduct() {

            return product;

        }

        public void setProduct(Product product) {

            this.product = product;

        }

        public int getNum() {

            return num;

        }

        public void setNum(int num) {

            this.num = num;

        }

    }

    步骤 4 : OrderDAO

    OrderDAO把订单对象保存到数据库中。

    这里需要注意的是,Order对象保存到数据库中后,该对象就会有对应的id,这个id,在后续保存OrderItem的时候,是作为order id存在的。

    所以在保存的数据库的时候,要获取自增长id
     

    ResultSet rs = ps.getGeneratedKeys();

       if (rs.next()) {

       int id = rs.getInt(1);

       o.setId(id);

    }

    package dao;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import bean.Order;

    public class OrderDAO {

        public void insert(Order o) {

            try {

                Class.forName("com.mysql.jdbc.Driver");

                Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cart?characterEncoding=UTF-8",

                        "root""admin");

                String sql = "insert into order_ values(null,?)";

                PreparedStatement ps = c.prepareStatement(sql);

                ps.setInt(1, o.getUser().getId());

                ps.execute();

                ResultSet rs = ps.getGeneratedKeys();

                if (rs.next()) {

                    int id = rs.getInt(1);

                    o.setId(id);

                }

                ps.close();

                c.close();

            catch (ClassNotFoundException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            catch (SQLException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

    步骤 5 : OrderItemDAO

    将OrderItem保存到数据库中

    package dao;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.util.ArrayList;

    import java.util.List;

    import bean.OrderItem;

    import bean.Product;

    public class OrderItemDAO {

        public static void main(String[] args) {

        }

         

        public void insert(OrderItem oi) {

            try {

                Class.forName("com.mysql.jdbc.Driver");

                Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cart?characterEncoding=UTF-8",

                        "root""admin");

                String sql = "insert into orderitem values(null,?,?,?)";

                PreparedStatement ps = c.prepareStatement(sql);

                ps.setInt(1,oi.getProduct().getId());

                ps.setInt(2,oi.getNum());

                ps.setInt(3,oi.getOrder().getId());

                 

                ps.execute();

                ps.close();

                c.close();

            catch (ClassNotFoundException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            catch (SQLException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

             

        }

    }

    步骤 6 : listOrderItem.jsp

    在listOrderItem.jsp页面新增加一个"生成订单"的链接
     

    <c:if test="${!empty ois}">

            <tr>

                <td colspan="4" align="right">

                    <a href="/createOrder">生成订单</a>

                </td>

            </tr>

    </c:if>

    <%@ page language="java" contentType="text/html; charset=UTF-8"

        pageEncoding="UTF-8" import="java.util.*" isELIgnored="false"%>

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

    <h1 align="center" >购物车</h1>

    <table align='center' border='1' cellspacing='0'>

        <tr>

            <td>商品名称</td>

            <td>单价</td>

            <td>数量</td>

            <td>小计</td>

        </tr>

        <c:forEach items="${ois}" var="oi" varStatus="st">

            <tr>

                <td>${oi.product.name}</td>

                <td>${oi.product.price}</td>

                <td>${oi.num}</td>

                <td>${oi.product.price*oi.num}</td>

            </tr>

        </c:forEach>

         

        <c:if test="${!empty ois}">

            <tr>

                <td colspan="4" align="right">

                    <a href="createOrder">生成订单</a>

                </td>

            </tr>

        </c:if>

         

    </table>

    步骤 7 : OrderCreateServlet

    OrderCreateServlet创建订单的Servlet
    1. 首选判断用户是否登陆,如果没有登陆跳转到登陆页面
    2. 创建一个订单对象,并设置其所属用户
    3. 把该订单对象保存到数据库中
    4. 遍历session中所有的订单项,设置他们的Order。 然后保存到数据库中
    5. 清空session中的订单项
    6. 最后打印订单创建成功

    package servlet;

    import java.io.IOException;

    import java.util.List;

    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import bean.Order;

    import bean.OrderItem;

    import bean.User;

    import dao.OrderDAO;

    import dao.OrderItemDAO;

    public class OrderCreateServlet extends HttpServlet {

        protected void service(HttpServletRequest request, HttpServletResponse response)

                throws ServletException, IOException {

            User u = (User) request.getSession().getAttribute("user");

            if(null==u){

                response.sendRedirect("/login.jsp");

                return;

            }

             

            Order o = new Order();

            o.setUser(u);

            new OrderDAO().insert(o);

            List<OrderItem> ois = (List<OrderItem>) request.getSession().getAttribute("ois");

            for (OrderItem oi : ois) {

                oi.setOrder(o);

                new OrderItemDAO().insert(oi);

            }

             

            ois.clear();

             

            response.setContentType("text/html; charset=UTF-8");

            response.getWriter().println("订单创建成功");

        }

    }

    步骤 8 : 配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <web-app>

        <servlet>

            <servlet-name>ProductListServlet</servlet-name>

            <servlet-class>servlet.ProductListServlet</servlet-class>

        </servlet>

        <servlet-mapping>

            <servlet-name>ProductListServlet</servlet-name>

            <url-pattern>/listProduct</url-pattern>

        </servlet-mapping>

        <servlet>

            <servlet-name>UserLoginServlet</servlet-name>

            <servlet-class>servlet.UserLoginServlet</servlet-class>

        </servlet>

        <servlet-mapping>

            <servlet-name>UserLoginServlet</servlet-name>

            <url-pattern>/login</url-pattern>

        </servlet-mapping>

        <servlet>

            <servlet-name>OrderItemAddServlet</servlet-name>

            <servlet-class>servlet.OrderItemAddServlet</servlet-class>

        </servlet>

        <servlet-mapping>

            <servlet-name>OrderItemAddServlet</servlet-name>

            <url-pattern>/addOrderItem</url-pattern>

        </servlet-mapping>

         

        <servlet>

            <servlet-name>OrderItemListServlet</servlet-name>

            <servlet-class>servlet.OrderItemListServlet</servlet-class>

        </servlet>

        <servlet-mapping>

            <servlet-name>OrderItemListServlet</servlet-name>

            <url-pattern>/listOrderItem</url-pattern>

        </servlet-mapping>

        <servlet>

            <servlet-name>OrderCreateServlet</servlet-name>

            <servlet-class>servlet.OrderCreateServlet</servlet-class>

        </servlet>

        <servlet-mapping>

            <servlet-name>OrderCreateServlet</servlet-name>

            <url-pattern>/createOrder</url-pattern>

        </servlet-mapping>

    </web-app>

    步骤 9 : 测试

    点击创建订单后,在数据库中的orderitem表观察到插入的数据

    测试


    更多内容,点击了解: https://how2j.cn/k/cart/cart-order/599.html

  • 相关阅读:
    装饰器
    提供离线chrome谷歌浏览器插件crx的网站有
    关于使用AWS上的RHEL8.x/Redhat系统使用自己单独购买的Redhat官网license导致的yum命令报错处理
    关于aws账单数据中几个重要的与费用相关的字段的意义分析
    在vCenter或者ESXi中通过ova/ovf进行还原部署虚拟机的过程记录
    关于python爬虫request.get()方法的常用参数
    关于aws cli命令的exit/return code分析
    关于pycharm代码运行后控制台的输出不完整被截断的处理
    关于变量的值中包含另一个变量引用的处理间接变量引用
    关于在python中使用pandas模块将列表list/元组tuple写入excel中
  • 原文地址:https://www.cnblogs.com/Lanht/p/12789429.html
Copyright © 2020-2023  润新知