hello,大家好,我来更新今天的知识啦,今天分享的是之前使用web做的一个购物车项目,因为一个项目有点庞大,所以我们分为两个部分来讲,今天讲的是登录功能和增加购物车功能,大家好好听课噢.
目录
一.前言:
购物车主要思想:用户可以选择一些商品到购物车去
1.购物车并不是一直放数据库,如果一直放数据库,会导致运行速度慢,压力太大,会卡
2.选择使用的技术:
session:
好处:快(放在内存当中),可以直接存对象
坏处:占用内存,如果服务器意外宕机,数据会丢失cookie:
好处:不占用内存,可以存储很久
坏处:存在客户端,只能存String类型,数据有泄露风险
注: 现在企业上都是用内存数据库
BigDecimal是一个专门用来做小数计算的类
在此次项目的前半段中,我们还需要用到vo包和pojo包,让我们来理解一下吧
pojo类是持久层,就是跟数据库打交道的层
vo包是view object,就是专门给前端用的
二.准备工作
1.首先我们来导入jar包和一个美化包
2.点击web.xml,然后把默认的首页选项设置成login.jsp界面
3.数据库准备工作;新建两个表,并插入数据
User表
shop_goods表
4.这是我们接下来要建的一些包
三.购物车案例.
1.首先新建一个DBHelper包(com.xxx.util)
package com.zking.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import oracle.jdbc.driver.OracleDriver;
public class DBHelper {
//静态代码块
static {
try {//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
//2.编写连接语句
private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
public static Connection getCon() {
try {
return DriverManager.getConnection(URL, "scott","tlt1516131462qmt");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//3.关闭资源
public static void getClose(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(con!=null&&!con.isClosed()) con.close();
if(ps!=null) ps.close();
if(rs!=null) rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(DBHelper.getCon());
}
}
2.创建一个实体类 User(com.xxx.pojo)
package com.zking.pojo;
public class User {
private Integer uuid;
private String uname;
private String upwd;
public User() {
// TODO Auto-generated constructor stub
}
public User(Integer uuid, String uname, String upwd) {
super();
this.uuid = uuid;
this.uname = uname;
this.upwd = upwd;
}
public Integer getUuid() {
return uuid;
}
public void setUuid(Integer uuid) {
this.uuid = uuid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public String toString() {
return "User [uuid=" + uuid + ", uname=" + uname + ", upwd=" + upwd + "]";
}
}
3.创建一个IUserDao数据库的接口类(com.xxx.dao)
package com.zking.dao;
import com.zking.pojo.Goods;
import com.zking.pojo.User;
public interface IUserDao {
User login(User user);
}
4.创建一个UserDaoImpl 接口的实现类(com.xxx.dao.impl)
package com.zking.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.zking.dao.IUserDao;
import com.zking.pojo.Goods;
import com.zking.pojo.User;
import com.zking.util.DBHelper;
public class UserDaoImpl implements IUserDao {
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
public User login(User user) {
try {
con=DBHelper.getCon();
System.out.println(con);
ps=con.prepareStatement("select * from web01_user where uname=?");
ps.setString(1,user.getUname());
System.out.println(user.getUname());
rs=ps.executeQuery();
if(rs.next()) {
User u=new User();
u.setUuid(rs.getInt(1));
u.setUname(rs.getString(2));
u.setUpwd(rs.getString(3));
return u;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.getClose(con, ps, rs);
}
return null;
}
}
5.创建 IUserBiz 一个业务逻辑层 (com.xxx.biz)
package com.zking.biz;
import com.zking.pojo.User;
public interface IUserBiz {
User login(User user);
}
6.创建UserBizImpl (com.xxx.biz.impl)
package com.zking.biz.impl;
import com.zking.biz.IUserBiz;
import com.zking.dao.IUserDao;
import com.zking.dao.impl.UserDaoImpl;
import com.zking.pojo.User;
public class UserBizImpl implements IUserBiz{
private IUserDao userDao=new UserDaoImpl();
public User login(User user) {
User u=userDao.login(user);
if(u!=null) {
if(u.getUpwd().equals(user.getUpwd())) {
return u;
}
}
return null;
}
}
7.创建一个Goods实体类(com.xxx.pojo)
package com.zking.pojo;
public class Goods {
private Integer id;
private String name;
private Integer price;
private String info;
public Goods() {
// TODO Auto-generated constructor stub
}
public Goods(Integer id, String name, Integer price, String info) {
super();
this.id = id;
this.name = name;
this.price = price;
this.info = info;
}
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 Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", price=" + price + ", info=" + info + "]";
}
}
8.创建一个IGoodsDao接口的类 (com.xxx.dao)
package com.zking.dao;
import java.util.List;
import com.zking.pojo.Goods;
public interface IGoodsDao {
//查询所有集合
List<Goods> getAll();
//查询单个(根据id拿)
Goods getOne(Integer id);
}
9.创建一个GoodsDaoImpl接口的实现类 (com.xxx.dao.impl)
package com.zking.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.dao.IGoodsDao;
import com.zking.pojo.Goods;
import com.zking.util.DBHelper;
public class GoodsDaoImpl implements IGoodsDao{
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
//查询所有
public List<Goods> getAll(){
List<Goods> list=new ArrayList<Goods>();
try {
con=DBHelper.getCon();
ps=con.prepareStatement("select * from shop_goods");
rs=ps.executeQuery();
while(rs.next()) {
Goods goods=new Goods();
goods.setId(rs.getInt(1));
goods.setName(rs.getString(2));
goods.setPrice(rs.getInt(3));
goods.setInfo(rs.getString(4));
list.add(goods);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Goods getOne(Integer id) {
try {
con=DBHelper.getCon();
ps=con.prepareStatement("select * from shop_goods where id=?");
ps.setInt(1,id);
rs=ps.executeQuery();
if(rs.next()) {
Goods goods =new Goods();
goods.setId(rs.getInt(1));
goods.setName(rs.getString(2));
goods.setPrice(rs.getInt(3));
goods.setInfo(rs.getString(4));
return goods;
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.getClose(con, ps, rs);
}
return null;
}
}
10.创建一个业务逻辑层 :IGoodsDao类(com.xxx.biz)
package com.zking.biz;
import java.util.List;
import com.zking.pojo.Goods;
public interface IGoodsBiz {
List<Goods> getAll();
//查询单个(根据id拿)
Goods getOne(Integer id);
}
11.创建一个GoodsDaoImpl类(com.xxx.biz.impl)
package com.zking.biz.impl;
import java.util.List;
import com.zking.biz.IGoodsBiz;
import com.zking.dao.IGoodsDao;
import com.zking.dao.impl.GoodsDaoImpl;
import com.zking.pojo.Goods;
public class GoodsBizImpl implements IGoodsBiz{
private IGoodsDao goodsDao=new GoodsDaoImpl();
public List<Goods> getAll(){
return goodsDao.getAll();
}
public Goods getOne(Integer id) {
return goodsDao.getOne(id);
}
}
12.创建一个doLogin.jsp类(处理登录功能的类)
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.pojo.User"%>
<%@page import="com.zking.biz.impl.UserBizImpl"%>
<%@page import="com.zking.biz.IUserBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String uname=request.getParameter("uname");
String upwd=request.getParameter("upwd");
IUserBiz userBiz=new UserBizImpl();
User user=userBiz.login(new User(0,uname,upwd));
if(user==null){
response.sendRedirect("login.jsp");
}else{
//登进去之后,首页需要登录数据
session.setAttribute("user",user);
//分配购物车
List<CarItem> car=new ArrayList<>();
//放到session中(把购物车给我)
session.setAttribute("car",car);
response.sendRedirect("index.jsp");
}
%>
13.创建一个index.jsp的类(首页)
<%@page import="com.zking.pojo.Goods"%>
<%@page import="com.zking.biz.impl.GoodsBizImpl"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="com.zking.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
td:nth-child(3)::before{
content: "$";
}
</style>
</head>
<body>
<%
Object obj=session.getAttribute("user");
if(obj==null){
response.sendRedirect("login.jsp");
return;
}
%>
<div class="jumbotron">
<div class="container">
<h1>欢迎光临 🌌源星球SuperMarket</h1>
<p>尊贵的<%=((User)obj).getUname() %></p>
</div>
</div>
<%=session.getAttribute("car")%>
<table class="table">
<tr>
<th>商品序号</th>
<th>商品名称</th>
<th>商品单价</th>
<th>商品描述</th>
<th>商品图片</th>
<th>操作</th>
</tr>
<%
IGoodsBiz goodsBiz=new GoodsBizImpl();
for(Goods goods:goodsBiz.getAll()){
%>
<tr>
<td><%=goods.getId() %></td>
<td><%=goods.getName()%></td>
<td><%=goods.getPrice()%></td>
<td><%=goods.getInfo()%></td>
<td><img src="" alt=""></td>
<td>
<div class="btn-group btn-group-xs">
<button class="btn btn-primary"><span class="glyphicon glyphicon-tags"></span></button>
<%--链接这里需要带id过去 --%>
<a href="doAddCar.jsp?id=<%=goods.getId()%>>" class="btn btn-primary">添加购物车</a>
</div>
</td>
</tr>
<%
}
%>
</table>
</body></html>
14.创建一个CarItem类(com.xxx.vo)
package com.zking.vo;
import com.zking.pojo.Goods;
/**
* 购物车的每一项,每一条商品数据
* @author zjjt
*
*通过继承来继承Goods的所有属性 extends Goods
*/
public class CarItem {
private Integer count; //数量
private Integer sum; //条目总价
private Goods goods;//对应的商品
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getSum() {
return sum;
}
public void setSum(Integer sum) {
this.sum = sum;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public CarItem() {
// TODO Auto-generated constructor stub
}
public CarItem(Integer count, Integer sum, Goods goods) {
super();
this.count = count;
this.sum = sum;
this.goods = goods;
}
@Override
public String toString() {
return "CarItem [count=" + count + ", sum=" + sum + ", goods=" + goods + "]";
}
}
15.创建一个增加购物车功能( )
<%@page import="java.util.List"%>
<%@page import="com.zking.biz.impl.GoodsBizImpl"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="com.zking.vo.CarItem"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//1.拿购物车
List<CarItem> car=(List<CarItem>)session.getAttribute("car");
if(car==null){
response.sendRedirect("login.jsp");
}
IGoodsBiz goodsBiz=new GoodsBizImpl();
//2.得知道是哪件商品
String str=request.getParameter("id");
int id=-1;
//1.如果没带id,那就让id等于-1,如果id等于-1的话,那就不会让它进去了
if(str!=null){
id=Integer.parseInt(str);
}
//2-1判断该商品是否存在
boolean f=true;
for(CarItem item:car){
//item.getGoods().getId();条目的商品id
if(id==item.getGoods().getId()){
//商品应该是已经被添加购物车了【购物车中某个条目的商品id和你添加的商品id一样】
item.setCount(item.getCount()+1);//数量+1
item.setSum(item.getCount()*item.getGoods().getPrice());
f=false;
break;
}
}
//只要判断f是否发生了改变
if(f){
//2-2.生成一个CarItem(购物车选项)[如果购物车没有该商品,就生成一个CarItem]
CarItem carItem=new CarItem();
//设置对应的商品数据
carItem.setGoods(goodsBiz.getOne(id));
carItem.setCount(1);//数量
//carItem.getCount() 商品加车的数量
//carItem.getCoods().getPrice 商品的单价
carItem.setSum(carItem.getCount()*carItem.getGoods().getPrice());//数量*单价=总价
//将购物条目【carItem】绑定到【car】中
car.add(carItem);
}
//更新购物车
session.setAttribute("car",car);
//跳回首页
response.sendRedirect("index.jsp");
%>
今天的知识就分享到这里啦!期待一下 下期的项目后半段吧!
今天的快乐记得收藏起来呀.