用到的技术
- maven
- mybatis
- servlet
- jsp
- axios和少量vue
实现功能
- 登录注册与验证
- 分页查询
- 模糊查询与动态查询
- 逻辑删除
ER图
购物车模块分析
页面结构
//Cart属性
//totalCount总数量,totalMoney总金额,Map<Integer,CartItem> cartItem购物车项
//CartItem属性
//id图书的,img图片,name图书名称,buyCoung购买数量,price单价,totalPrice总价
图书购买与购物车显示
- 选择图书-传入图书的Id
- 根据图书的id获取对应的图书
- 将图书的属性遍历到新建的cartItem中
- 调用Cart的添加方法,如果Map中对应的图书键相同则购买数量加一,总金额加倍
- 在session中传入Cart
//Order属性
//oderNo订单编号,orderDate订单时间,orderUser订单所属用户,orderMoney订单所用总钱,orderStatus订单状态
//OrderItem属性
//id自动生成,img图片,name图书名称,buyCoung购买数量,price单价,totalPrice总价
购物车结账
- 点击结账-传递Cart与user.id
- 将购物车以及用户的id传入新建的Order中,更新数据库的Order-添加的方式;同时将Cart中的购物车项进行遍历,其中的每一个数据经行遍历添加到orderItem中;在遍历中根据cartItem中id获取对应的图书,将图书的销量+cartItem的购买数量,图书的库存-cartItem的购买数量
- 将新的图书对象进行更新
功能代码展示
package com.liu.web.servlet;
import com.alibaba.fastjson.JSON;
import com.liu.pojo.Book;
import com.liu.pojo.Cart;
import com.liu.pojo.CartItem;
import com.liu.pojo.User;
import com.liu.service.BookService;
import com.liu.service.CartItemService;
import com.liu.service.impl.BookServiceImp;
import com.liu.service.impl.CartItemServiceImp;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/cart/*")
public class CartServlet extends BaseServlet {
CartItemService cartItemService = new CartItemServiceImp();
BookService bookService = new BookServiceImp();
public void addCartItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String _bookId = req.getParameter("bookId");
int bookId = Integer.parseInt(_bookId);
Book book = bookService.selectBookById(bookId);
CartItem cartItem = new CartItem(book.getId(), book.getBookImg(), book.getBookName(), 1, book.getPrice(), book.getPrice());
Cart cart = (Cart) req.getSession().getAttribute("cart");
if (cart == null) {
cart = new Cart();
req.getSession().setAttribute("cart", cart);
}
cart.addItem(cartItem);
int count =cart.getTotalCount();
String s =String.valueOf(count);
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write(s);
// System.out.println(cart);
// HttpSession session = req.getSession();
// User currUser = (User) session.getAttribute("currUser");
// CartItem cartItem = new CartItem(new Book(bookId), 1, currUser);
// cartItemService.addOrUpdateCartItem(cartItem, currUser.getCart());
// userMsg.setUser(user);
// userMsg.setMsg("");
// String jsonString = JSON.toJSONString(userMsg);
// System.out.println(jsonString);
// resp.getWriter().write(jsonString);
}
public void getUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User currUser = (User) req.getSession().getAttribute("currUser");
String jsonString = JSON.toJSONString(currUser);
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write(jsonString);
}
public void count(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cart cart = (Cart) req.getSession().getAttribute("cart");
if (cart==null){
return;
}
int count =cart.getTotalCount();
String s =String.valueOf(count);
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write(s);
}
public void getCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cart cart = (Cart) req.getSession().getAttribute("cart");
String jsonString = JSON.toJSONString(cart);
resp.setContentType("text/json;charset=utf-8");
resp.getWriter().write(jsonString);
}
public void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
Cart cart = (Cart) req.getSession().getAttribute("cart");
cart.deleteItem(Integer.parseInt(id));
System.out.println("返回");
resp.sendRedirect(req.getHeader("Referer"));
}
public void updateItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
String count = req.getParameter("count");
Cart cart = (Cart) req.getSession().getAttribute("cart");
cart.updateCount(Integer.parseInt(id),Integer.parseInt(count));
resp.sendRedirect(req.getHeader("Referer"));
}
public void clearCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cart cart = (Cart) req.getSession().getAttribute("cart");
cart.clear();
resp.sendRedirect(req.getHeader("Referer"));
}
}
代码分享
别的功能就不展示了,有需要的小伙伴可以自行下载
链接:https://pan.baidu.com/s/1hZHlehrTD4g2vDIZt7vsbw
提取码:mt7v
视频介绍
B站传送门:
https://www.bilibili.com/video/BV1pe4y1273v/