第一次冲刺

网上购书系统:第一次冲刺

引言

本篇博客旨在完成网上购书系统的最小可用产品。

第一次冲刺的目标是构建一个最小可用系统,涵盖以下高优先级功能:

  • 用户功能
    • 用户注册和登录。
    • 书籍浏览和搜索。
    • 购物车管理(添加、查看、更新、删除)。
  • 管理员功能
    • 添加和删除书籍。

这些功能的实现确保了系统能够满足基本的在线购书需求,同时为未来扩展提供了基础。

架构设计

系统采用单体架构,将前端、后端和数据库整合到一个应用中。该种架构简化了开发和部署过程。

技术栈

  • 前端:使用 JSP构建动态网页,提供用户友好的界面。
  • 后端:使用 Servlet 处理业务逻辑,响应前端请求。
  • 数据库:MySQL 用于存储用户、书籍、购物车和订单数据。
  • 服务器:Tomcat8.0 作为部署服务器,运行整个应用。
  • 会话管理:通过 HTTP 会话(session)或 cookie 管理用户状态。
  • 支付功能:在 MVP 中,支付功能简化为模拟订单生成

数据库设计

系统使用 MySQL 数据库,设计了以下主要表:

表名 字段 描述
t_user uid, loginname, loginpass, email, status, activationCode 存储用户信息
t_category cid, name, pid, desc, orderBy 存储书籍分类信息
t_admin adminId, adminname, adminpwd 存储管理员用户信息
t_orderitem orderitemId, subtotal, bid, bname, currPrice, image_b, oid 存储订单中的书籍详情
t_order oid, ordertime, total, status, uid 存储订单信息
t_cartitem cartitemId, quantity, bid, uid, orderBy 存储购物车中的书籍
t_book bid, bname, author, price, currPrice, discount, press, publishtime, edition, pageNum, wordNum, printtime, booksize, cid, image_w, image_b, orderBy 存储书籍信息

实现

在第一次冲刺中,我们实现了以下关键组件

用户管理

  • 功能
    • 注册:用户可以通过输入用户名、邮箱和密码创建账户。
    • 登录:用户使用凭证登录系统,系统通过会话(session)或 cookie 管理用户状态。

java

public class AdminServlet extends BaseServlet {
    private AdminService adminService = new AdminService();
    
    /**
     * 登录功能
     */
    public String login(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {
       Admin form = CommonUtils.toBean(req.getParameterMap(), Admin.class);
       Admin admin = adminService.login(form);
       if(admin == null) {
          req.setAttribute("msg", "用户名或密码错误!");
          return "/adminjsps/login.jsp";
       }
       req.getSession().setAttribute("admin", admin);
       return "r:/adminjsps/admin/index.jsp";
    }
}
/**
	 * 注册功能
	 * @param user
	 */
	public void regist(User user) {
		user.setUid(CommonUtils.uuid());
		user.setStatus(false);
		user.setActivationCode(CommonUtils.uuid() + CommonUtils.uuid());
		try {
			userDao.add(user);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		Properties prop = new Properties();
		try {
			prop.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
		} catch (IOException e1) {
			throw new RuntimeException(e1);
		}
		String host = prop.getProperty("host");//服务器主机名
		String name = prop.getProperty("username");//登录名
		String pass = prop.getProperty("password");//登录密码
		Session session = MailUtils.createSession(host, name, pass);
		String from = prop.getProperty("from");
		String to = user.getEmail();
		String subject = prop.getProperty("subject");
		String content = MessageFormat.format(prop.getProperty("content"), user.getActivationCode());
		Mail mail = new Mail(from, to, subject, content);
		try {
			MailUtils.send(session, mail);
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

书籍管理

  • 功能
    • 浏览:支持分页显示书籍列表。

    • 搜索:用户可以通过关键词搜索书籍,。

    • 详情:每个书籍都有显示标题、作者、价格和描述。

      java

      
      public class BookServlet extends BaseServlet {
      	private BookService bookService = new BookService();
      	
      	/**
      	 * 获取当前页码
      	 */
      	private int getPc(HttpServletRequest req) {
      		int pc = 1;
      		String param = req.getParameter("pc");
      		if(param != null && !param.trim().isEmpty()) {
      			try {
      				pc = Integer.parseInt(param);
      			} catch(RuntimeException e) {}
      		}
      		return pc;
      	}
      	
      	
      	/**
      	 * 按bid查询
      	 */
      	public String load(HttpServletRequest req, HttpServletResponse resp)
      			throws ServletException, IOException {
      		String bid = req.getParameter("bid");//获取链接的参数bid
      		Book book = bookService.load(bid);//通过bid得到book对象
      		req.setAttribute("book", book);//保存到req中
      		return "f:/jsps/book/desc.jsp";//转发到desc.jsp
      	}
      	
      	/**
      	 * 按分类查
      	 */
      	public String findByCategory(HttpServletRequest req, HttpServletResponse resp)
      			throws ServletException, IOException {
      		int pc = getPc(req);
      		String url = getUrl(req);
      		String cid = req.getParameter("cid");
      		PageBean<Book> pb = bookService.findByCategory(cid, pc);
      		pb.setUrl(url);
      		req.setAttribute("pb", pb);
      		return "f:/jsps/book/list.jsp";
      	}
      	
      	/**
      	 * 按作者查
      	 */
      	public String findByAuthor(HttpServletRequest req, HttpServletResponse resp)
      			throws ServletException, IOException {
      		int pc = getPc(req);
      		String url = getUrl(req);
      		String author = req.getParameter("author");
      		PageBean<Book> pb = bookService.findByAuthor(author, pc);
      		pb.setUrl(url);
      		req.setAttribute("pb", pb);
      		return "f:/jsps/book/list.jsp";
      	}
      	
      	/**
      	 * 按出版社查询
      	 */
      	public String findByPress(HttpServletRequest req, HttpServletResponse resp)
      			throws ServletException, IOException {
      		int pc = getPc(req);
      		String url = getUrl(req);
      		String press = req.getParameter("press");
      		PageBean<Book> pb = bookService.findByPress(press, pc);
      		pb.setUrl(url);
      		req.setAttribute("pb", pb);
      		return "f:/jsps/book/list.jsp";
      	}
      	
      	/**
      	 * 按图名查
      	 */
      	public String findByBname(HttpServletRequest req, HttpServletResponse resp)
      			throws ServletException, IOException {
      		int pc = getPc(req);
      		String url = getUrl(req);
      		String bname = req.getParameter("bname");
      		PageBean<Book> pb = bookService.findByBname(bname, pc);
      		pb.setUrl(url);
      		req.setAttribute("pb", pb);
      		return "f:/jsps/book/list.jsp";
      	}
      	
      }
      
      

购物车管理

  • 功能

    • 添加到购物车:用户可以将书籍添加到购物车。
    • 查看购物车:显示购物车中的书籍及其数量。
    • 更新购物车:用户可以调整书籍数量或删除书籍。

    java

    public class CartItemServlet extends BaseServlet {
        private CartItemService cartItemService = new CartItemService();
        
        /**
         * 加载多个CartItem
         */
        public String loadCartItems(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
           String cartItemIds = req.getParameter("cartItemIds");
           double total = Double.parseDouble(req.getParameter("total"));
           List<CartItem> cartItemList = cartItemService.loadCartItems(cartItemIds);
           req.setAttribute("cartItemList", cartItemList);
           req.setAttribute("total", total);
           req.setAttribute("cartItemIds", cartItemIds);
           return "f:/jsps/cart/showitem.jsp";
        }
        /**
         	*更新
         	*/
        public String updateQuantity(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
           String cartItemId = req.getParameter("cartItemId");
           int quantity = Integer.parseInt(req.getParameter("quantity"));
           CartItem cartItem = cartItemService.updateQuantity(cartItemId, quantity);
           StringBuilder sb = new StringBuilder("{");
           sb.append("\"quantity\"").append(":").append(cartItem.getQuantity());
           sb.append(",");
           sb.append("\"subtotal\"").append(":").append(cartItem.getSubtotal());
           sb.append("}");
           resp.getWriter().print(sb);
           return null;
        }
        
        /**
         * 批量删除功能
         */
        public String batchDelete(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
           String cartItemIds = req.getParameter("cartItemIds");
           cartItemService.batchDelete(cartItemIds);
           return myCart(req, resp);
        }
        
        /**
         * 添加购物车条目
         */
        public String add(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
           Map map = req.getParameterMap();
           CartItem cartItem = CommonUtils.toBean(map, CartItem.class);
           Book book = CommonUtils.toBean(map, Book.class);
           User user = (User)req.getSession().getAttribute("sessionUser");
           cartItem.setBook(book);
           cartItem.setUser(user);
           cartItemService.add(cartItem);
           return myCart(req, resp);
        }
        
        /**
         * 我的购物车
         */
        public String myCart(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
           User user = (User)req.getSession().getAttribute("sessionUser");
           String uid = user.getUid();
           List<CartItem> cartItemLIst = cartItemService.myCart(uid);
           req.setAttribute("cartItemList", cartItemLIst);
           return "f:/jsps/cart/list.jsp";
        }
    }
    

管理员功能

  • 功能

    • 添加删除书籍:管理员可以输入书籍信息并保存和删除。

    java

    public class AdminBookServlet extends BaseServlet {
        private BookService bookService = new BookService();
        private CategoryService  categoryService = new CategoryService();
        
        /**
         * 删除图书
         */
        public String delete(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
           String bid = req.getParameter("bid");
           
        
        /**
         * 修改图书
         */
        public String edit(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
           Map map = req.getParameterMap();
           Book book = CommonUtils.toBean(map, Book.class);
           Category category = CommonUtils.toBean(map, Category.class);
           book.setCategory(category);
           
           bookService.edit(book);
           req.setAttribute("msg", "修改图书成功!");
           return "f:/adminjsps/msg.jsp";
        }
        
        /**
         * 加载图书
         */
        public String load(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
         
           String bid = req.getParameter("bid");
           Book book = bookService.load(bid);
           req.setAttribute("book", book);
    
           req.setAttribute("parents", categoryService.findParents());
    
           String pid = book.getCategory().getParent().getCid();
           req.setAttribute("children", categoryService.findChildren(pid));
           return "f:/adminjsps/admin/book/desc.jsp";
        }
        
        /**
         * 添加图书
         */
        public String addPre(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
           List<Category> parents = categoryService.findParents();
           req.setAttribute("parents", parents);
           return "f:/adminjsps/admin/book/add.jsp";
        }
    
    }
    

总结

第一次冲刺成功地为网上购书系统建立了坚实的基础。我们实现了核心功能,包括用户注册、登录、书籍浏览、购物车管理、以及基本的管理员功能。但仍有许多改进和扩展的空间。

posted @ 2025-06-07 20:24  node77  阅读(100)  评论(0)    收藏  举报