【JavaWeb _13 session购物车部分功能讲解

本文主要介绍了在JavaWeb中实现session购物车的两种方式:数据库存储和session存储。分析了各自的优缺点,并详细讲解了使用session存储购物车的实现步骤,包括实体类、商品查询和页面展示等。同时,给出了加入购物车事件的处理逻辑,以及如何将商品信息封装到Cart实体类中并保存到session。
摘要由CSDN通过智能技术生成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、pandas是什么?

一、项目分析

1、将购物车保存到数据库(永久的)
--优点:只要你登录了,并且选购了,不管你换不换设备,数据仍然在,方便了用户。
--缺点:太占数据库的内存,性能差,效率低


2、将购物车保存到session中(临时的)
--优点:性能佳 效率高
--缺点:会话具有时效性,超时数据会消失

----提示语法:
存到session: session.setAttribute(键String,值Object)
从session取值:session.getAttribute(键) 返回Object

----我们是将订单项的集合List<OrderItem>保存到session中 

实体类
User
Goods(商品类):gid gname gprice ginfo gpath(图片路径)
OrderItem(订单项类):goods oinum oiprice=数量*单价(goods.getGprice())

doshop.jsp:接收gid 根据gid拿到商品对象 形成订单项 加到集合中保存到session 
————————————————
版权声明:本文为CSDN博主「名字太难~」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_67888923/article/details/124111466

1.引入库

代码如下(示例):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context

二、编写代码以及步骤
1、商城首页的实现(分页)
  (1)编写实体类 

package com.zking.cart.entity;
 
public class Goods {
    private int gid;
    private String gname;
    private float gprice;
    private String ginfo;
    private String gpath;
 
    public Goods() {
        // TODO Auto-generated constructor stub
    }
 
    public Goods(String gname, float gprice, String ginfo, String gpath) {
        super();
        this.gname = gname;
        this.gprice = gprice;
        this.ginfo = ginfo;
        this.gpath = gpath;
    }
 
    public Goods(int gid, String gname, float gprice, String ginfo, String gpath) {
        super();
        this.gid = gid;
        this.gname = gname;
        this.gprice = gprice;
        this.ginfo = ginfo;
        this.gpath = gpath;
    }
 
    public int getGid() {
        return gid;
    }
 
    public void setGid(int gid) {
        this.gid = gid;
    }
 
    public String getGname() {
        return gname;
    }
 
    public void setGname(String gname) {
        this.gname = gname;
    }
 
    public float getGprice() {
        return gprice;
    }
 
    public void setGprice(float gprice) {
        this.gprice = gprice;
    }
 
    public String getGinfo() {
        return ginfo;
    }
 
    public void setGinfo(String ginfo) {
        this.ginfo = ginfo;
    }
 
    public String getGpath() {
        return gpath;
    }
 
    public void setGpath(String gpath) {
        this.gpath = gpath;
    }
 
    @Override
    public String toString() {
        return "Goods [gid=" + gid + ", gname=" + gname + ", gprice=" + gprice + ", ginfo=" + ginfo + ", gpath=" + gpath
                + "]";
    }
    
}

(2)编写查询所有和显示条数以及单个查询的功能

@Override
    public int getGoodsCount() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;// 保存表的总记录数
        try {
            conn = DBHelper.getConn();
            String sql = "select count(*) from goods";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBHelper.myClose(conn, ps, rs);
        }
        return count;
    }
 
    
    @Override
    public List<Goods> queryGoodsAll(int pageIndex, int pageSize) {
        int start = (pageIndex - 1) * pageSize + 1;
        int end = pageIndex * pageSize;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "";
        List<Goods> list = new ArrayList<Goods>();
        Goods goods = null;
        try {
            conn = DBHelper.getConn();
            sql ="select b.* from(select a.*,rownum as rid from( select * from  goods)a)b where b.rid between "+start+" and "+end+"";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()) {
                goods = new Goods(rs.getInt(1), rs.getString(2), rs.getFloat(3), rs.getString(4), rs.getString(5));
                list.add(goods);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBHelper.myClose(conn, ps, rs);
        }
        return list;
    }
    @Override
    public Goods getGoodsByCid(int gid) {
        Connection conn = null;
        PreparedStatement ps = null;
        Goods goods = null;
        String sql = "";
        ResultSet rs = null;
        try {
            conn = DBHelper.getConn();
            sql = "select * from goods where gid = "+gid;
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()) {
                goods = new Goods(rs.getInt(1), rs.getString(2), rs.getFloat(3), rs.getString(4), rs.getString(5));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return goods;
    }

(3)商城首页显示

<table border = "1" width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <th>商品编号</th>
                <th>商品名称</th>
                <th>商品价格</th>
                <th>商品描述</th>
                <th>商品图片</th>
                <th>商品操作</th>
            </tr>
            <!-- 调用dao遍历即可 -->
            <%
                //实例化
                       IGoodsBiz igb = new GoodsBizImpl();
                        //定义变量存储页码以及每一夜显示的数据
                        int pageIndex = 1;
                        int pageSize = 4;
                        int pageMax = 0;//存储最大页码
                        //当点击分页区域的下一页超链接时,获取这个参数
                        String pIndex = request.getParameter("pageIndex");
                        //判断,只有你点击了下一页  才将pIndex赋值给pageIndex
                        if (null != pIndex) {
                            pageIndex = Integer.valueOf(pIndex);
                        }
                        //根据dao求出总记录数
                        int count = igb.getGoodsCount();
                        pageMax = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
                       List<Goods> listGoods = igb.queryGoodsAll(pageIndex, pageSize);
                     for(Goods goods : listGoods){
            %>
            
                    <tr>
                        <td><%=goods.getGid() %></td>
                        <td><%=goods.getGname() %></td>
                        <td><%=goods.getGprice() %></td>
                        <td><%=goods.getGinfo() %></td>
                        <td><img src = "<%=goods.getGpath()%>"/></td>
                        <td>
                            <button οnclick="addCart(<%=goods.getGid()%>)">加入购物车</button>
                        </td>
                    </tr>
            
            <%    
                }
            %>
                            <hr>
                <p align="right" style="font-size: 15px; font-weight: bold">
                    当前页数:[<%=pageIndex%>/<%=pageMax%>]&nbsp; 
                    <a href="index.jsp?pageIndex=1">首页</a> 
                    <a href="index.jsp?pageIndex=<%=pageIndex - 1 < 0 ? 1 : pageIndex - 1%>">上一页</a>
                    <a href="index.jsp?pageIndex=<%=pageIndex + 1 > pageMax ? pageMax : pageIndex + 1%>">下一页</a>
                    <a href="index.jsp?pageIndex=<%=pageMax%>">末页</a>
                    </p>
        </table>

(4)点击加入购物车的事件携带参数进入do处理界面

<script type="text/javascript">
        function addCart(cid) {
            location.href = "doShopping.jsp?cid="+cid;
        }
</script>
2、商品加入购物车 
(1)先编写一个购物车类Cart来存储购物信息

package com.zking.cart.entity;
 
public class Cart {
    private Goods goods;//对象:包含商品所有属性
    private int ccount;//数量  单个商品的数量
    private float ctotal;//单个商品的总价格
    
    public Cart() {
        // TODO Auto-generated constructor stub
    }
 
    public Cart(Goods goods, int ccount, float ctotal) {
        super();
        this.goods = goods;
        this.ccount = ccount;
        this.ctotal = ctotal;
    }
 
    public Goods getGoods() {
        return goods;
    }
 
    public void setGoods(Goods goods) {
        this.goods = goods;
    }
 
    public int getCcount() {
        return ccount;
    }
 
    public void setCcount(int ccount) {
        this.ccount = ccount;
    }
 
    public float getCtotal() {
        return ctotal;
    }
    //计算总价格
    public void setCtotal() {
        this.ctotal = this.getGoods().getGprice() * this.getCcount();
    }
 
    @Override
    public String toString() {
        return "Cart [goods=" + goods + ", ccount=" + ccount + ", ctotal=" + ctotal + "]";
    }
 
}

(2)在商品主页点击加入购物车后携带商品编号进入do处理界面

        a.先获取携带过来的商品编号

//设置编码
        request.setCharacterEncoding("utf-8");
        //获取商品编号
        String id = request.getParameter("cid");
        int cid = 0;
        if(null!=id){
            cid = Integer.valueOf(id);
        }
      b.通过携带的商品编号获取整个商品信息

    //拿到cid  根据cid获取商品的其它信息
        Goods goods = new GoodsBizImpl().getGoodsByCid(cid);
      c.将商品的信息一并封装在Cart实体中

        //数量默认一件
        //总价格    商品的单价 * 数量(1件)
        //将上面获取的信息封装到Cart实体中
        Cart cart = new Cart();
        cart.setGoods(goods);
        cart.setCcount(1);
        cart.setCtotal();
        d.加入购物车

       //1.获取购物车
        List<Cart> listCarts = (List<Cart> )session.getAttribute("listCarts");
        //2.判断非空
        boolean flag = true;
        if(null == listCarts){
            //创建购物车
            listCarts = new ArrayList<Cart>();
        }else{
            //遍历所有的购物车中的商品  
            for(Cart c : listCarts){
                //判断传递的cid与当前listCarts中的每一个cid进行匹配
                if(cid == c.getGoods().getGid()){
                    flag = false;
                    //修改数量
                    c.setCcount(c.getCcount()+1);
                    //修改总价格
                    c.setCtotal();    
                }
            }
        }
        //判断flag标记
        if(flag == true){//说明购物车中没有该商品
            listCarts.add(cart);
        }
        
        //重新保存购物车
        session.setAttribute("listCarts", listCarts);    
        response.sendRedirect("cart.jsp");

————————————————
版权声明:本文为CSDN博主「名字太难~」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_67888923/article/details/124111466


总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值