Web开发之Servlet版购物车项目02

Web开发之Servlet版购物车项目02,第1张

目录

🐕前期回顾

项目建表代码

在上一期文章基础上增加的功能

1,查看订单 

2,订单结算 


🐕前期回顾

web开发之Servlet版购物车项目01_小阿飞_的博客-CSDN博客不一样的购物车项目https://blog.csdn.net/yifei_345678/article/details/124440574?spm=1001.2014.3001.5501🐖本期文章就是在上一期文章项目的基础上增加了几个功能👆👆

项目相关:

开发软件:支持web开发的eclipseOracle数据库

开发模式:三层架构

将java代码从jsp文件中分离:使用Servlet技术实现

访问权限:使用Filter过滤器实现

项目建表代码
create table car_user--用户表
(
    id       number primary key,
    account  varchar2(30) not null,
    password varchar(32)  not null
);

comment on column car_user.ID is '用户编号';
comment on column car_user.account is '用户账户';
comment on column car_user.password is '用户密码(MD5)';

insert into car_user--用户账号密码表
values (1, '2890@fox.com', 'ff9830c42660c1dd1942844f8069b74a');-- root123
insert into car_user
values (2, '2357@fox.com', 'e10adc3949ba59abbe56e057f20f883e');-- 123456

create table car_goods--商品表
(
    id       number primary key,
    name     varchar2(20)                        not null,
    describe varchar2(100) default '此商品暂时没有介绍🤣' not null,
    price    number                              not null
);

comment on column car_goods.ID is '商品编号';
comment on column car_goods.name is '商品名称';
comment on column car_goods.describe is '商品描述';
comment on column car_goods.price is '用户价格';

create table car_order--订单表
(
    id      number primary key,
    user_id number not null,
    total   number not null
);

comment on column car_order.ID is '订单编号';
comment on column car_order.user_id is '谁的订单!';
comment on column car_order.total is '订单总价';

create table car_order_item
(
    id       number primary key,
    order_id number not null,
    goods_id number not null,
    quantity number not null,
    total    number not null
);
在上一期文章基础上增加的功能 1,查看订单 

查看订单界面展示👇👇

查看订单界面代码👇👇

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>







    
    
    
    
    
    我的订单


	
		
		您好${user.account}, 这是订单界面
	
	
		继续购买
		返回购物车
	
	
			
订单编号 订单总价 订单 *** 作
${orderList.id } ${orderList.total } 查看订单详情

🦀查看订单功能相关代码

🦀OrderServlet代码

package com.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.biz.IOrderBiz;
import com.biz.impl.OrderBizImpl;
import com.pojo.Order;
import com.pojo.User;
/**
 *	实现查看订单功能
 */
@WebServlet("/order.do")
public class OrderServlet extends HttpServlet{
		private IOrderBiz orderBiz=new OrderBizImpl();
		@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User user = (User) req.getSession().getAttribute("user");
		//订单集合
		List orderList= orderBiz.list(user.getId());
		req.setAttribute("orderList", orderList);
		req.getRequestDispatcher("order.jsp").forward(req,resp);
 	}
}

☁ IOrderBiz接口 

package com.biz;
import java.util.List;
import com.pojo.Order;
/**
 * 订单业务逻辑性接口
 */
public interface IOrderBiz {
    int maxPK();
    int insert(Order order);
    List list(int userId);
}

☁OrderBizImpl实现类

package com.biz.impl;
import java.util.List;
import com.biz.IOrderBiz;
import com.biz.IOrderItemBiz;
import com.dao.IOrderDao;
import com.dao.impl.OrderDaoImpl;
import com.pojo.Order;
import com.pojo.OrderItem;
/**
 * 订单业务逻辑层
 */
public class OrderBizImpl implements IOrderBiz{
		private IOrderDao orderDao=new OrderDaoImpl();
	@Override
	public int maxPK() {
		return orderDao.maxPK();
	}
	@Override
	public int insert(Order order) {
		return orderDao.insert(order);
	}
	@Override
	public List list(int userId) {
		return orderDao.list(userId);
	}	
}

☁ IOrderDao接口

package com.dao;
import java.util.List;
import com.pojo.Order;
/**
 * 订单数据库访问层接口
 */
public interface IOrderDao {
	  int maxPK();
	  int insert(Order order);
	  List list(int userId);
}

☁OrderDaoImpl实现类

package com.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.dao.IOrderDao;
import com.pojo.Order;
import com.util.DBHelper;
/**
 * 订单数据库访问层
 */
public class OrderDaoImpl implements IOrderDao{	
	 private Connection con;
	 private PreparedStatement ps;
	 private ResultSet rs;	
	@Override
	public int maxPK() {
		  try {
	            con = DBHelper.getCon();
	            ps = con.prepareStatement("select nvl(max(id),0)+1 from car_order");
	            rs = ps.executeQuery();
	            if (rs.next()) {
	               return rs.getInt(1);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            DBHelper.close(con, ps, rs);
	        }
		return 0;
	}
	@Override
	public int insert(Order order) {
		  try {
	            con = DBHelper.getCon();
	            ps = con.prepareStatement("insert into car_order values(?,?,?)");
	            ps.setInt(1,order.getId());
	            ps.setInt(2,order.getUserId());
	            ps.setInt(3,order.getTotal());
	            return ps.executeUpdate();
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            DBHelper.close(con, ps, rs);
	        }
		return 0;
	}
	@Override
	public List list(int userId) {
		List list = new ArrayList<>();
        try {
            con = DBHelper.getCon();
            ps = con.prepareStatement("select * from car_order where user_id=?");
            ps.setInt(1,userId);
            rs=ps.executeQuery();
            while(rs.next()){
                Order order=new Order();
                order.setId(rs.getInt(1));
                order.setUserId(rs.getInt(2));
                order.setTotal(rs.getInt(3));
                list.add(order);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBHelper.close(con, ps, rs);
        }
        return list;
	}
}
2,订单结算 

订单结算界面展示👇👇

当点击订单结算按钮时就会清空购物车👇👇

并且会将数据插入Oracle数据库中对应的订单表和订单项表

🐱订单表

 

🐱订单项表 

🐱订单结算相关代码

🐱CalcServlet代码

package com.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.biz.IOrderBiz;
import com.biz.IOrderItemBiz;
import com.biz.impl.OrderBizImpl;
import com.biz.impl.OrderItemBizImpl;
import com.pojo.Order;
import com.pojo.OrderItem;
import com.pojo.User;
/**
 * 处理订单结算界面的功能
 */
@WebServlet("/calc.do")
public class CalcServlet extends HttpServlet {
	private IOrderBiz orderBiz = new OrderBizImpl();
	private IOrderItemBiz orderItemBiz = new OrderItemBizImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List car = (List) req.getSession().getAttribute("car");
		User user = (User) req.getSession().getAttribute("user");
		// 结算:将数据放到【订单】【订单项】
		if (car.isEmpty()) {
			resp.sendRedirect("car.jsp");
			return;
		}
		// 计算购物车中所有商品的价格总和
		int sum = 0;
		for (OrderItem item : car) {
			sum += item.getTotal();
		}
		// 先将订单保存到数据库
		Order order = new Order();
		order.setId(orderBiz.maxPK());
		order.setUserId(user.getId());
		order.setTotal(sum);
		// 先将订单放到数据库订单表
		int i = orderBiz.insert(order);
		if (i > 0) {
			for (OrderItem item : car) {
				// car中只有订单项id和订单id
				item.setId(orderItemBiz.maxPK());
				item.setOrderId(order.getId());// 订单id
				// 再将订单项放到数据库的订单项表
				orderItemBiz.insert(item);
			}
			// 结算完毕
			car.clear();
			req.getSession().setAttribute("total", 0);
		}
		resp.sendRedirect("car.jsp");
	}
}

🐱IOrderItemBiz接口 

package com.biz;
import java.util.List;
import com.pojo.OrderItem;
/**
 * 订单项业务逻辑层接口
 */
public interface IOrderItemBiz {
	//最大id
	 int maxPK();
	 //插入订单项
	 int insert(OrderItem item);
	 //根据id查询订单项
	 List list(Integer id);
}

 🐱OrderItemBizImpl实现类

package com.biz.impl;
import java.util.List;
import com.biz.IOrderItemBiz;
import com.dao.IOrderItemDao;
import com.dao.impl.OrderItemDaoImpl;
import com.pojo.OrderItem;
/**
 * 订单项业务逻辑层
 */
public class OrderItemBizImpl implements IOrderItemBiz{
	private IOrderItemDao orderItemDao=new OrderItemDaoImpl();	
	@Override
	public int maxPK() {
		return orderItemDao.maxPK();
	}
	@Override
	public int insert(OrderItem item) {
		return orderItemDao.insert(item);
	}
	@Override
	public List list(Integer id) {
		return orderItemDao.list(id);
	}
}

🐱IOrderItemDao接口 

package com.dao;
import java.util.List;
import com.pojo.OrderItem;
/**
 * 订单项数据库访问层接口
 */
public interface IOrderItemDao {
	  int maxPK();
	  int insert(OrderItem item);
	  List list(Integer id);
}

🐱OrderItemDaoImpl实现类


感谢阅读,感兴趣的话就点个赞呗😜

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/786424.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存