欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

大项目之网上书城(九)——订单Demo

程序员文章站 2022-04-29 20:39:07
大项目之网上书城(九)——订单Demo [TOC] 主要改动 加了创建订单,为订单付款,删除订单。 1.OrderServiceImpl 代码 有添加、删除、付款、根据是否付款查找。还加了添加收货信息登记。 2.OrderDaoImpl 代码 目前有添加、删除、根据order_id查找和根据user ......

大项目之网上书城(九)——订单demo

主要改动

加了创建订单,为订单付款,删除订单。

1.orderserviceimpl

代码

有添加、删除、付款、根据是否付款查找。还加了添加收货信息登记。

package cn.edu.bdu.mc.services.impls;

import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;
import java.util.uuid;

import cn.edu.bdu.mc.beans.book;
import cn.edu.bdu.mc.beans.order;
import cn.edu.bdu.mc.beans.orderitem;
import cn.edu.bdu.mc.daos.bookdao;
import cn.edu.bdu.mc.daos.orderdao;
import cn.edu.bdu.mc.daos.orderitemdao;
import cn.edu.bdu.mc.daos.impls.bookdaoimpl;
import cn.edu.bdu.mc.daos.impls.orderdaoimpl;
import cn.edu.bdu.mc.daos.impls.orderitemdaoimpl;
import cn.edu.bdu.mc.services.orderservice;

public class orderserviceimpl implements orderservice {
    
    private orderdao orderdao = new orderdaoimpl();
    private orderitemdao orderitemdao  = new orderitemdaoimpl();
    private bookdao bookdao = new bookdaoimpl();
    
    @override
    public boolean addorder(string order_id, int user_id, string name, string address, string phone, string[] book_ids) throws sqlexception {
        // todo auto-generated method stub
        list<book>books = new arraylist<book>();
        for (string book_id : book_ids) {   
            book book = bookdao.findbookbyid(integer.parseint(book_id));
            books.add(book);
            //如果数量不够了就本次购买失败
            if(book.getcount()==0) {
                return false;
            }
        }
        order order = new order();
        order.setuser_id(user_id);
        order.setreciver_name(name);
        order.setreciver_phone(phone);
        order.setreciver_address(address);
        order.setorder_id(order_id);
        orderdao.addorder(order);
        for (book book : books) {           
            orderitem orderitem = new orderitem();
            orderitem.setbook_id(book.getbook_id());
            orderitem.setorder_id(order_id);
            orderitem.setcount(1);
            orderitem.setprice(book.getprice());
            book.setcount(book.getcount()-1);
            bookdao.update(book);
            orderitemdao.addorderitem(orderitem);
        }
        return true;
    }
    
    @override
    public void afterpay(string order_id) throws sqlexception {
        order order = orderdao.findorderbyid(order_id);
        order.setpaystate(1);
        orderdao.update(order);
    }

    @override
    public order delbyid(string order_id) throws sqlexception {
        // todo auto-generated method stub
        order order = orderdao.delbyid(order_id);
        orderitemdao.delbyid(order_id);
        return order;
    }

    @override
    public list<order> findnotpaybyuser_id(int user_id) throws sqlexception {
        // todo auto-generated method stub
        return orderdao.findorderbyuser_id(user_id, 0);
    }

    @override
    public list<order> findareadypaybyuser_id(int user_id) throws sqlexception {
        // todo auto-generated method stub
        return orderdao.findorderbyuser_id(user_id, 1);
    }

    
}

2.orderdaoimpl

代码

目前有添加、删除、根据order_id查找和根据user_id查找

package cn.edu.bdu.mc.daos.impls;

import java.sql.sqlexception;
import java.text.simpledateformat;
import java.util.date;
import java.util.list;

import javax.sql.datasource;

import org.apache.commons.dbutils.queryrunner;
import org.apache.commons.dbutils.handlers.beanhandler;
import org.apache.commons.dbutils.handlers.beanlisthandler;

import cn.edu.bdu.mc.beans.order;
import cn.edu.bdu.mc.daos.orderdao;
import cn.edu.bdu.mc.utils.jdbcutil;

public class orderdaoimpl implements orderdao {

    private datasource datasource = jdbcutil.getdatasource();
    private queryrunner queryrunner = new queryrunner(datasource);      
    
    @override
    public void addorder(order order) throws sqlexception {
        // todo auto-generated method stub
        string sql = "insert into `order`(user_id,order_id,reciver_name,reciver_phone,reciver_address,paystate,money,order_time) values(?,?,?,?,?,0,0,?)";
        date date = new date();
        simpledateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss");
        string date1 = dateformat.format(date);
        queryrunner.update(sql,order.getuser_id(),order.getorder_id(),order.getreciver_name(),order.getreciver_phone(),order.getreciver_address(),date1);
    }
    
    @override
    public void update(order order) throws sqlexception {
        // todo auto-generated method stub
        string sql = "update `order` set user_id = ?,reciver_name = ?,reciver_phone = ?,reciver_address = ?,paystate = ?,money = ?,order_time = ?";
        queryrunner.update(sql,order.getuser_id(),order.getreciver_name(),order.getreciver_phone(),order.getreciver_address(),order.getpaystate(),order.getmoney(),order.getorder_time());
    }

    @override
    public order findorderbyid(string order_id) throws sqlexception {
        // todo auto-generated method stub
        string sql = "select * from `order` where order_id = ?";
        return queryrunner.query(sql, new beanhandler<order>(order.class), order_id);
    }

    @override
    public order delbyid(string order_id) throws sqlexception {
        order order = findorderbyid(order_id);
        string sql = "delete from `order` where order_id = ?";
        queryrunner.update(sql, order_id);
        return order;
    }

    @override
    public list<order> findorderbyuser_id(int user_id, int paystate) throws sqlexception {
        // todo auto-generated method stub
        string sql = "select * from `order` where user_id = ? and paystate = ?";
        return queryrunner.query(sql, new beanlisthandler<order>(order.class), user_id, paystate);
    }
    
    

}

3.orderitemdaoimpl

代码

目前有添加和删除

package cn.edu.bdu.mc.daos.impls;

import java.sql.sqlexception;

import javax.sql.datasource;

import org.apache.commons.dbutils.queryrunner;

import cn.edu.bdu.mc.beans.orderitem;
import cn.edu.bdu.mc.daos.orderitemdao;
import cn.edu.bdu.mc.utils.jdbcutil;

public class orderitemdaoimpl implements orderitemdao {

    private datasource datasource = jdbcutil.getdatasource();
    private queryrunner queryrunner = new queryrunner(datasource);      

    @override
    public void addorderitem(orderitem orderitem) throws sqlexception {
        // todo auto-generated method stub
        string sql = "insert into orderitem values (?,?,?,?)";
        queryrunner.update(sql,orderitem.getorder_id(),orderitem.getbook_id(),orderitem.getcount(),orderitem.getprice());
    }

    @override
    public void delbyid(string order_id) throws sqlexception {
        // todo auto-generated method stub
        string sql = "delete from `order` where order_id = ?";
        queryrunner.update(sql, order_id);
    }

}

4.orderitem_insert触发器

代码

orderitem中每增加一项,将他对应的order_id的订单的钱增加上他的书的价格乘以数量。

drop trigger if exists `orderitem_insert`;
delimiter ;;
create trigger `orderitem_insert` after insert on `orderitem` for each row begin
    update `order` set money = money + new.price * new.count where order_id = new.order_id;
end
;;
delimiter ;

5.delorderservlet

代码

package cn.edu.bdu.mc.servlets;

import java.io.ioexception;
import java.sql.sqlexception;
import java.util.list;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import cn.edu.bdu.mc.beans.order;
import cn.edu.bdu.mc.services.orderservice;
import cn.edu.bdu.mc.services.impls.orderserviceimpl;

/**
 * servlet implementation class delorderservlet
 */
@webservlet("/delorder")
public class delorderservlet extends httpservlet {
    private static final long serialversionuid = 1l;
       
    /**
     * @see httpservlet#httpservlet()
     */
    public delorderservlet() {
        super();
        // todo auto-generated constructor stub
    }

    /**
     * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
     */
    protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        try {
            string order_id = request.getparameter("order_id");
            orderservice orderservice = new orderserviceimpl();
            order order = orderservice.delbyid(order_id);
            response.sendredirect(request.getcontextpath()+"/client/order.jsp");
        } catch (sqlexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }
    }

    /**
     * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
     */
    protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        // todo auto-generated method stub
        doget(request, response);
    }

}

6.neworderservlet

代码

package cn.edu.bdu.mc.servlets;

import java.io.ioexception;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;
import java.util.map;
import java.util.uuid;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;

import cn.edu.bdu.mc.beans.order;
import cn.edu.bdu.mc.beans.user;
import cn.edu.bdu.mc.services.orderservice;
import cn.edu.bdu.mc.services.impls.orderserviceimpl;

/**
 * servlet implementation class neworderservlet
 */
@webservlet("/neworder")
public class neworderservlet extends httpservlet {
    private static final long serialversionuid = 1l;
       
    /**
     * @see httpservlet#httpservlet()
     */
    public neworderservlet() {
        super();
        // todo auto-generated constructor stub
    }

    /**
     * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
     */
    protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        if(request.getsession().getattribute("shouhuo")==null) {
            response.sendredirect(request.getcontextpath()+"/client/shouhuo.jsp");
        }else {
            try {
                string[] book_ids = request.getparameter("book_id_list").split("#");
                user user = (user) request.getsession().getattribute("user");
                int user_id = user.getuser_id();
                map<string, string>shouhuo = (map<string, string>) request.getsession().getattribute("shouhuo");
                string name = shouhuo.get("name");
                string phone = shouhuo.get("phone");
                string address = shouhuo.get("address");
                orderservice orderservice = new orderserviceimpl();
                string order_id = uuid.randomuuid().tostring();
                boolean create_order = orderservice.addorder(order_id, user_id, name, address, phone, book_ids);
                if(create_order) {
                    string htmlcode="<!doctype html>\n" + 
                            "<html>"
                            + "<head>"
                            + "<link rel=\"stylesheet\" href=\""+request.getcontextpath()+"/bootstrap-3.3.7-dist/css/bootstrap.min.css\">"
                            + "</head>"
                            + "<body>"
                            + "<div style=\"position:absolute;left:44%;top:42%;height:140px;width:240px;background-color:rgba(145, 162, 196, 0.9);border:1px;text-align:center;\"id=\"quit1\">\r\n" + 
                            "   <h3>订单创建成功!请尽快付款!</h3><a class=\"btn btn-info\" href=\""+request.getcontextpath()+"/client/order.jsp\">查看订单</a>&nbsp;&nbsp;&nbsp;&nbsp;"
                            +"<a class=\"btn btn-info\" href=\""+request.getcontextpath()+"/payfor?order_id=\""+order_id+">立即付款</a>\n" + 
                            "</div>"
                            + "</body>"
                            + "</html>";
                    response.getwriter().write(htmlcode);
                }else {
                    string htmlcode="<!doctype html>\n" + 
                            "<html>"
                            + "<head>"
                            + "<link rel=\"stylesheet\" href=\""+request.getcontextpath()+"/bootstrap-3.3.7-dist/css/bootstrap.min.css\">"
                            + "</head>"
                            + "<body>"
                            + "<div style=\"position:absolute;left:44%;top:46%;height:100px;width:240px;background-color:rgba(145, 162, 196, 0.9);border:1px;text-align:center;\"id=\"quit1\">\r\n" + 
                            "   <h3>订单创建失败!请检查订单!</h3><a class=\"btn btn-info\" href=\""+request.getcontextpath()+"/client/order.jsp\">查看订单</a>&nbsp;&nbsp;&nbsp;&nbsp;"
                            +"<a class=\"btn btn-info\" href=\""+request.getcontextpath()+"/client/index.jsp\">再逛逛</a>\n" + 
                            "</div>"
                            + "</body>"
                            + "</html>";
                    response.getwriter().write(htmlcode);
                }
            } catch (sqlexception e) {
                // todo auto-generated catch block
                e.printstacktrace();
            }
        }
    }

    /**
     * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
     */
    protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        // todo auto-generated method stub
        doget(request, response);
    }

}

7.payforservlet

代码

package cn.edu.bdu.mc.servlets;

import java.io.ioexception;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;

import cn.edu.bdu.mc.beans.order;
import cn.edu.bdu.mc.beans.user;
import cn.edu.bdu.mc.daos.orderdao;
import cn.edu.bdu.mc.daos.impls.orderdaoimpl;
import cn.edu.bdu.mc.services.orderservice;
import cn.edu.bdu.mc.services.impls.orderserviceimpl;

/**
 * servlet implementation class payforservlet
 */
@webservlet("/payfor")
public class payforservlet extends httpservlet {
    private static final long serialversionuid = 1l;
       
    /**
     * @see httpservlet#httpservlet()
     */
    public payforservlet() {
        super();
        // todo auto-generated constructor stub
    }

    /**
     * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)
     */
    protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        
        try {
            string order_id = request.getparameter("order_id");
            orderservice orderservice = new orderserviceimpl();
            orderservice.afterpay(order_id);
            string htmlcode="<!doctype html>\n" + 
                    "<html>"
                    + "<head>"
                    + "<link rel=\"stylesheet\" href=\""+request.getcontextpath()+"/bootstrap-3.3.7-dist/css/bootstrap.min.css\">"
                    + "</head>"
                    + "<body>"
                    + "<div style=\"position:absolute;left:44%;top:46%;height:100px;width:240px;background-color:rgba(145, 162, 196, 0.9);border:1px;text-align:center;\"id=\"quit1\">\r\n" + 
                    "   <h3>购买成功!</h3><a class=\"btn btn-info\" href=\""+request.getcontextpath()+"/client/index.jsp\">继续购买</a>&nbsp;&nbsp;&nbsp;&nbsp;"
                            +"<a class=\"btn btn-info\" href=\""+request.getcontextpath()+"/client/order.jsp\">查看订单</a>\n" + 
                    "</div>"
                    + "</body>"
                    + "</html>";
            response.getwriter().write(htmlcode);
        } catch (sqlexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }
        
    }

    /**
     * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)
     */
    protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        // todo auto-generated method stub
        doget(request, response);
    }

}

8.shouhuo.jsp

代码

<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
<!doctype html>
<html>
<head>
<title>收货地址</title>
</head>
<body style="background-color:#bbb;width:1400px;margin:0 auto">
<!-- 调用头部页面 -->
<div style="width:100%;height:100px;float:left">
<jsp:include page="/client/head.jsp"></jsp:include>
</div>
<!-- 通用内容体大小 -->
<div style="width:70%;height:720px;float:left;margin-left:15%;">
    <!-- 好看的图 -->
    <div style="width:55%;height:100%;float:left;margin-top:10%;">
        <img alt="快递员" src="${pagecontext.request.contextpath }/client/img/kuaidi.jpg" style="width:90%;">
    </div>
    <!-- 登录界面 -->
    <div style="width:45%;height:80%;float:left;margin-top:7%">
        <h1 style="color:#8b6914;text-align:center">收货信息</h1>
        <hr style="height:2px;border:none;border-top:5px ridge green;" />
        <form action="${pagecontext.request.contextpath }/shouhuo" method="post" class="form-horizontal" role="form">
            <div class="form-group">
                <label for="lastname" class="col-sm-3 control-label input-lg">姓名</label>
                <div class="col-sm-9">
                    <input type="text" name="name" class="form-control input-lg"
                           placeholder="收货人姓名" style="float:left"/>
                </div>
            </div>
            <div class="form-group">
                <label for="firstname" class="col-sm-3 control-label input-lg">手机号</label>
                <div class="col-sm-9">
                    <input type="text" name="phone" 
                    class="form-control input-lg" placeholder="收货人手机号" style="float:left"/>
                </div>
            </div>
            
            <div class="form-group">
                <label for="firstname" class="col-sm-3 control-label input-lg">地址</label>
                <div class="col-sm-9">
                    <input type="text" name="address"
                    class="form-control input-lg" placeholder="收货人地址" style="float:left"/>
                </div>
            </div>
            <div class="form-group">
                <label for="firstname" class="col-sm-1 control-label input-lg"></label>
                <div class="col-sm-5">
                    <input type="submit" name="submit" value="提交"
                    class="form-control input-lg btn btn-primary"style="width:100%;float:left"/>
                </div>
                <div class="col-sm-5">
                    <input type="reset" name="reset" value="重置" id="re"
                    class="form-control input-lg btn btn-warning"style="width:100%;float:left"/>
                </div>
            </div>
        </form>
    </div>
</div>
<!-- 调用底部页面 -->
<div style="width:100%;height:60px;float:left">
<jsp:include page="/client/foot.jsp"></jsp:include>
</div>
</body>
</html>

效果图

大项目之网上书城(九)——订单Demo

9.order.jsp

代码

<%@page import="cn.edu.bdu.mc.beans.user"%>
<%@page import="cn.edu.bdu.mc.services.impls.orderserviceimpl"%>
<%@page import="cn.edu.bdu.mc.services.orderservice"%>
<%@page import="cn.edu.bdu.mc.beans.order"%>
<%@page import="java.util.list"%>
<%@ page language="java" contenttype="text/html; charset=utf-8"
    pageencoding="utf-8"%>
<!doctype html>
<html>
<head>
<title>订单</title>
</head>
<%
    user user = (user)request.getsession().getattribute("user");
    orderservice orderservice = new orderserviceimpl();
    list<order>notpay = orderservice.findnotpaybyuser_id(user.getuser_id());
    list<order>areadypay = orderservice.findareadypaybyuser_id(user.getuser_id());
    
%>
<body style="background-color:#bbb;width:1400px;margin:0 auto">
<!-- 调用头部页面 -->
<div style="width:100%;height:100px;float:left">
<jsp:include page="/client/head.jsp"></jsp:include>
</div>
<!-- 通用内容体大小 -->
<div style="width:70%;height:720px;float:left;margin-left:15%;">
    未付款:<br>
    <% if(notpay==null){ %>
        无<br>
    <% }else{ %>
        <% for(order order : notpay){%>
            id=<font><%=order.getorder_id() %></font>&nbsp;&nbsp;money=<font><%=order.getmoney() %></font>&nbsp;&nbsp;
            <a href="${pagecontext.request.contextpath }/payfor?order_id=<%=order.getorder_id() %>">付款</a>&nbsp;&nbsp;
            <a href="${pagecontext.request.contextpath }/delorder?order_id=<%=order.getorder_id() %>">删除</a>&nbsp;&nbsp;
            <br>
        <% } %>
    <% } %>
    已付款:<br>
    <% if(areadypay==null){ %>
        无<br>
    <% }else{ %>
        <% for(order order : areadypay){%>
            id=<font><%=order.getorder_id() %></font>&nbsp;&nbsp;money=<font><%=order.getmoney() %></font>&nbsp;&nbsp;
            <br>
        <% } %>
    <% } %>
</div>
<!-- 调用底部页面 -->
<div style="width:100%;height:60px;float:left">
<jsp:include page="/client/foot.jsp"></jsp:include>
</div>
</body>
</html>

效果图

大项目之网上书城(九)——订单Demo

点击付款后,还删除了一个,忘截图了
大项目之网上书城(九)——订单Demo

大项目之网上书城(九)——订单Demo

总结

今天弄得不是很多,又是各种报错。再加上开始的晚,于是拖到了现在才更。