java-web京东购物网 加入购物车与查看购物车的实现运用session服务器端(在数据库中查询)

本文介绍了如何在Java Web应用中实现京东购物网站的加入购物车和查看购物车功能。通过使用Session来存储用户购物车信息,并结合数据库查询操作,确保用户数据的持久化和准确性。
摘要由CSDN通过智能技术生成

标题:加入购物车与查看购物车的实现运用session,运用getId的方法获取session的id值,
            设置到cookie中,与session的id值一样,设置页面延长的时间,加入到reponse响应中,
            运用IE11验证, 页面关闭后,购物车与查看购物车的信息,依然存在,实现共享。
1.封装一个从数据库表中映射来的商品类Goods
public class Goods implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;  //商品id
private String  name;  //商品名称
private Double price;  //商品价格
private Date addDate;  //商品日期
private String img;  //商品图片
private String number; //商品编号
public Goods() {
super();
// TODO Auto-generated constructor stub
}
public Goods(Integer id, String name, Double price, Date addDate,
String img, String number) {
super();
this.id = id;
this.name = name;
this.price = price;
this.addDate = addDate;
this.img = img;
this.number = number;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Date getAddDate() {
return addDate;
}
public void setAddDate(Date addDate) {
this.addDate = addDate;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", price=" + price
+ ", addDate=" + addDate + ", img=" + img + ", number="
+ number + "]";
}
}
2. 封装了一个接口类BaseDao,封装了方法
public interface BaseDao<T,PK> {
根据id查询实体
T findByID(PK id);
查询所有
List<T> findAll();
}
3.封装了一个接口GoodsDao,继承了BaseDao
public interface GoodsDao extends BaseDao<Goods, Integer>{}
4.封装一个GoodsDao的实现类GoodsDaoImpl,实现了GoodsDao的所有方法
    // 数据库操作的有关对象声明
private Connection conn;
    //数据库操作对象(得到预处理对象)
private PreparedStatement pstmt;
   //返回的结果集
private ResultSet rs;
public class GoodsDaoImpl implements GoodsDao {
    //根据id查询实体
    @Override
public Goods findByID(Integer id) {
        Goods entity = null;
// 获取连接对象
conn = DBConn.getConn();
// 第二步:定义sql语句
        String sql = "select id,name,price,adddate,number,img from goods where id=?"
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值