标题:加入购物车与查看购物车的实现运用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=?"
java-web京东购物网 加入购物车与查看购物车的实现运用session服务器端(在数据库中查询)
最新推荐文章于 2024-04-30 13:29:47 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)