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

Java 实战项目锤炼之网上商城系统的实现流程

程序员文章站 2022-03-19 09:06:51
一、项目简述(+需求文档+ppt)功能: 主页显示热销商品;所有商品展示,可进行商品搜索;点 击商品进入商品详情页,显示库存,具有立即购买和加入 购物车功能,可增减购买商品数量亦可手动输入(同时验证...

一、项目简述(+需求文档+ppt)

功能: 主页显示热销商品;所有商品展示,可进行商品搜索;点 击商品进入商品详情页,显示库存,具有立即购买和加入 购物车功能,可增减购买商品数量亦可手动输入(同时验证 库存),热销商品展示。立即购买进入确认订单页面,可选 择已经添加的地址,亦可新增地址。(同时验证库存),可 选择购买哪些商品,可删除不需要的商品。点击结算进入 确认订单页面,确认后提交订单,订单重复提交给予响 应,库存不足或商品下架给予响应。后台管理:(修改密码 等),商品管理(商品批量添加、上下架、库存维护等), 订单管理。

二、项目运行

环境配置: jdk1.8 + tomcat8.5 + mysql + eclispe ( intellij idea,eclispe,myeclispe,sts 都支持)

项目技术: jsp + c3p0+ servlert + html+ css + javascript + jquery + ajax + fileupload等等。

Java 实战项目锤炼之网上商城系统的实现流程

Java 实战项目锤炼之网上商城系统的实现流程

Java 实战项目锤炼之网上商城系统的实现流程

Java 实战项目锤炼之网上商城系统的实现流程

后台管理-主页操作代码:

/**
 * 后台管理-主页
 */
@controller
public class adminhomecontroller extends basecontroller {
    @resource(name = "adminservice")
    private adminservice adminservice;
    @resource(name = "productorderservice")
    private productorderservice productorderservice;
    @resource(name = "productservice")
    private productservice productservice;
    @resource(name = "userservice")
    private userservice userservice;
 
    /**
     * 转到后台管理-主页
     * @param session session对象
     * @param map 前台传入的map
     * @return 响应数据
     * @throws parseexception 转换异常
     */
    @requestmapping(value = "admin", method = requestmethod.get)
    public string gotopage(httpsession session, map<string, object> map) throws parseexception {
        logger.info("获取管理员信息");
        object adminid = checkadmin(session);
        if (adminid == null) {
            return "redirect:/admin/login";
        }
        admin admin = adminservice.get(null, integer.parseint(adminid.tostring()));
        map.put("admin", admin);
        logger.info("获取统计信息");
        //产品总数
        integer producttotal = productservice.gettotal(null, new byte[]{0, 2});
        //用户总数
        integer usertotal = userservice.gettotal(null);
        //订单总数
        integer ordertotal = productorderservice.gettotal(null, new byte[]{3});
        logger.info("获取图表信息");
        map.put("jsonobject", getchartdata(null,null,7));
        map.put("producttotal", producttotal);
        map.put("usertotal", usertotal);
        map.put("ordertotal", ordertotal);
 
        logger.info("转到后台管理-主页");
        return "admin/homepage";
    }
 
    /**
     * 转到后台管理-主页(ajax方式)
     * @param session session对象
     * @param map 前台传入的map
     * @return 响应数据
     * @throws parseexception 转换异常
     */
    @requestmapping(value = "admin/home", method = requestmethod.get)
    public string gotopagebyajax(httpsession session, map<string, object> map) throws parseexception {
        logger.info("获取管理员信息");
        object adminid = checkadmin(session);
        if (adminid == null) {
            return "admin/include/loginmessage";
        }
        admin admin = adminservice.get(null, integer.parseint(adminid.tostring()));
        map.put("admin", admin);
        logger.info("获取统计信息");
        integer producttotal = productservice.gettotal(null, new byte[]{0, 2});
        integer usertotal = userservice.gettotal(null);
        integer ordertotal = productorderservice.gettotal(null, new byte[]{3});
        logger.info("获取图表信息");
        map.put("jsonobject", getchartdata(null, null,7));
        logger.info("获取图表信息");
        map.put("jsonobject", getchartdata(null,null,7));
        map.put("producttotal", producttotal);
        map.put("usertotal", usertotal);
        map.put("ordertotal", ordertotal);
        logger.info("转到后台管理-主页-ajax方式");
        return "admin/homemanagepage";
    }
 
    /**
     * 按日期查询图表数据(ajax方式)
     * @param begindate 开始日期
     * @param enddate 结束日期
     * @return 响应数据
     * @throws parseexception 转换异常
     */
    @responsebody
    @requestmapping(value = "admin/home/charts", method = requestmethod.get, produces = "application/json;charset=utf-8")
    public string getchartdatabydate(@requestparam(required = false) string begindate, @requestparam(required = false) string enddate) throws parseexception {
        if (begindate != null && enddate != null) {
            //转换日期格式
            simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd");
            return getchartdata(simpledateformat.parse(begindate), simpledateformat.parse(enddate),7).tojsonstring();
        } else {
            return getchartdata(null, null,7).tojsonstring();
        }
    }
 
    /**
     * 按日期获取图表数据
     * @param begindate 开始日期
     * @param enddate 结束日期
     * @param days 天数
     * @return 图表数据的json对象
     * @throws parseexception 转换异常
     */
    private jsonobject getchartdata(date begindate,date enddate,int days) throws parseexception {
        jsonobject jsonobject = new jsonobject();
        simpledateformat time = new simpledateformat("yyyy-mm-dd", locale.uk);
        simpledateformat time2 = new simpledateformat("mm/dd", locale.uk);
        simpledateformat timespecial = new simpledateformat("yyyy-mm-dd hh:mm:ss", locale.uk);
        //如果没有指定开始和结束日期
        if (begindate == null || enddate == null) {
            //指定一周前的日期为开始日期
            calendar cal = calendar.getinstance();
            cal.add(calendar.date, 1-days);
            begindate = time.parse(time.format(cal.gettime()));
            //指定当前日期为结束日期
            cal = calendar.getinstance();
            enddate = cal.gettime();
        } else {
            begindate = time.parse(time.format(begindate));
            enddate = timespecial.parse(time.format(enddate) + " 23:59:59");
        }
        logger.info("根据订单状态分类");
        //未付款订单数统计数组
        int[] orderunpaidarray = new int[7];
        //未发货订单数统计叔祖
        int[] ordernotshippedarray = new int[7];
        //未确认订单数统计数组
        int[] orderunconfirmedarray = new int[7];
        //交易成功订单数统计数组
        int[] ordersuccessarray = new int[7];
        //总交易订单数统计数组
        int[] ordertotalarray = new int[7];
        logger.info("从数据库中获取统计的订单集合数据");
        list<ordergroup> ordergrouplist = productorderservice.gettotalbydate(begindate, enddate);
        //初始化日期数组
        jsonarray datestr = new jsonarray(days);
        //按指定的天数进行循环
        for (int i = 0; i < days; i++) {
            //格式化日期串(mm/dd)并放入日期数组中
            calendar cal = calendar.getinstance();
            cal.settime(begindate);
            cal.add(calendar.date, i);
            string formatdate = time2.format(cal.gettime());
            datestr.add(formatdate);
            //该天的订单总数
            int ordercount = 0;
            //循环订单集合数据的结果集
            for(int j = 0; j < ordergrouplist.size(); j++){
                ordergroup ordergroup = ordergrouplist.get(j);
                //如果该订单日期与当前日期一致
                if(ordergroup.getproductorder_pay_date().equals(formatdate)){
                    //从结果集中移除数据
                    ordergrouplist.remove(j);
                    //根据订单状态将统计结果存入对应的订单状态数组中
                    switch (ordergroup.getproductorder_status()) {
                        case 0:
                            //未付款订单
                            orderunpaidarray[i] = ordergroup.getproductorder_count();
                            break;
                        case 1:
                            //未发货订单
                            ordernotshippedarray[i] = ordergroup.getproductorder_count();
                            break;
                        case 2:
                            //未确认订单
                            orderunconfirmedarray[i] = ordergroup.getproductorder_count();
                            break;
                        case 3:
                            //交易成功订单
                            ordersuccessarray[i] = ordergroup.getproductorder_count();
                            break;
                    }
                    //累加当前日期的订单总数
                    ordercount += ordergroup.getproductorder_count();
                }
            }
            //将统计的订单总数存入总交易订单数统计数组
            ordertotalarray[i] = ordercount;
        }
        logger.info("返回结果集map");
        jsonobject.put("ordertotalarray", ordertotalarray);
        jsonobject.put("orderunpaidarray", orderunpaidarray);
        jsonobject.put("ordernotshippedarray", ordernotshippedarray);
        jsonobject.put("orderunconfirmedarray", orderunconfirmedarray);
        jsonobject.put("ordersuccessarray", ordersuccessarray);
        jsonobject.put("datestr",datestr);
        return jsonobject;
    }
}

前台主页代码:

/**
 * 主页
 */
@controller
public class forehomecontroller extends basecontroller {
    @resource(name = "userservice")
    private userservice userservice;
    @resource(name="categoryservice")
    private categoryservice categoryservice;
    @resource(name="productservice")
    private productservice productservice;
    @resource(name="productimageservice")
    private productimageservice productimageservice;
 
    //转到前台天猫-主页
    @requestmapping(value = "/", method = requestmethod.get)
    public string gotopage(httpsession session, map<string, object> map) {
        logger.info("检查用户是否登录");
        object userid = checkuser(session);
        if (userid != null) {
            logger.info("获取用户信息");
            user user = userservice.get(integer.parseint(userid.tostring()));
            map.put("user", user);
        }
        logger.info("获取产品分类列表");
        list<category> categorylist = categoryservice.getlist(null,null);
        logger.info("获取每个分类下的产品列表");
       for(category category : categorylist){
           list<product> productlist = productservice.getlist(
                   new product().setproduct_category(category),
                   new byte[]{0, 2},
                   new orderutil("product_sale_count", true), new pageutil(0, 8)
           );
           if (productlist != null) {
               for (product product : productlist) {
                   integer product_id = product.getproduct_id();
                   product.setsingleproductimagelist(
                           productimageservice.getlist(
                                   product_id, (byte) 0, new pageutil(0, 1)
                           )
                   );
               }
           }
           category.setproductlist(productlist);
       }
        map.put("categorylist",categorylist);
        logger.info("获取促销产品列表");
        list<product> specialproductlist = productservice.getlist(
                null, new byte[]{2}, null, new pageutil(0, 6)
        );
        map.put("specialproductlist", specialproductlist);
 
        logger.info("转到前台主页");
        return "fore/homepage";
    }
 
    //转到前台天猫-错误页
    @requestmapping(value = "error", method = requestmethod.get)
    public string gotoerrorpage() {
        return "fore/errorpage";
    }
 
    //获取主页分类下产品信息-ajax
    @responsebody
    @requestmapping(value = "product/nav/{category_id}", method = requestmethod.get, produces = "application/json;charset=utf-8")
    public string getproductbynav(@pathvariable("category_id") integer category_id) {
        jsonobject object = new jsonobject();
        if (category_id == null) {
            object.put("success", false);
            return object.tojsonstring();
        }
        logger.info("获取分类id为{}的产品标题数据", category_id);
        list<product> productlist = productservice.gettitle(
                new product().setproduct_category(new category().setcategory_id(category_id)),
                new pageutil(0, 40)
        );
        list<list<product>> complexproductlist = new arraylist<>(8);
        list<product> products = new arraylist<>(5);
        for (int i = 0; i < productlist.size(); i++) {
            //如果临时集合中产品数达到5个,加入到产品二维集合中,并重新实例化临时集合
            if (i % 5 == 0) {
                complexproductlist.add(products);
                products = new arraylist<>(5);
            }
            products.add(productlist.get(i));
        }
        complexproductlist.add(products);
        category category = new category().setcategory_id(category_id).setcomplexproductlist(complexproductlist);
        object.put("success", true);
        object.put("category", category);
        return object.tojsonstring();
    }
}

以上就是java 实战项目锤炼之网上商城系统的实现流程的详细内容,更多关于java 网上商城系统的资料请关注其它相关文章!