第一次沖刺
網(wǎng)上購書系統(tǒng):第一次沖刺
引言
本篇博客旨在完成網(wǎng)上購書系統(tǒng)的最小可用產(chǎn)品。
第一次沖刺的目標(biāo)是構(gòu)建一個(gè)最小可用系統(tǒng),涵蓋以下高優(yōu)先級功能:
- 用戶功能:
- 用戶注冊和登錄。
- 書籍瀏覽和搜索。
- 購物車管理(添加、查看、更新、刪除)。
- 管理員功能:
- 添加和刪除書籍。
這些功能的實(shí)現(xiàn)確保了系統(tǒng)能夠滿足基本的在線購書需求,同時(shí)為未來擴(kuò)展提供了基礎(chǔ)。
架構(gòu)設(shè)計(jì)
系統(tǒng)采用單體架構(gòu),將前端、后端和數(shù)據(jù)庫整合到一個(gè)應(yīng)用中。該種架構(gòu)簡化了開發(fā)和部署過程。
技術(shù)棧
- 前端:使用 JSP構(gòu)建動態(tài)網(wǎng)頁,提供用戶友好的界面。
- 后端:使用 Servlet 處理業(yè)務(wù)邏輯,響應(yīng)前端請求。
- 數(shù)據(jù)庫:MySQL 用于存儲用戶、書籍、購物車和訂單數(shù)據(jù)。
- 服務(wù)器:Tomcat8.0 作為部署服務(wù)器,運(yùn)行整個(gè)應(yīng)用。
- 會話管理:通過 HTTP 會話(session)或 cookie 管理用戶狀態(tài)。
- 支付功能:在 MVP 中,支付功能簡化為模擬訂單生成
數(shù)據(jù)庫設(shè)計(jì)
系統(tǒng)使用 MySQL 數(shù)據(jù)庫,設(shè)計(jì)了以下主要表:
| 表名 | 字段 | 描述 |
|---|---|---|
| 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 | 存儲書籍信息 |

實(shí)現(xiàn)
在第一次沖刺中,我們實(shí)現(xiàn)了以下關(guān)鍵組件
用戶管理
- 功能:
- 注冊:用戶可以通過輸入用戶名、郵箱和密碼創(chuàng)建賬戶。
- 登錄:用戶使用憑證登錄系統(tǒng),系統(tǒng)通過會話(session)或 cookie 管理用戶狀態(tài)。
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", "用戶名或密碼錯(cuò)誤!");
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");//服務(wù)器主機(jī)名
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);
}
}
書籍管理
- 功能:
-
瀏覽:支持分頁顯示書籍列表。
-
搜索:用戶可以通過關(guān)鍵詞搜索書籍,。
-
詳情:每個(gè)書籍都有顯示標(biāo)題、作者、價(jià)格和描述。
javapublic class BookServlet extends BaseServlet { private BookService bookService = new BookService(); /** * 獲取當(dāng)前頁碼 */ 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");//獲取鏈接的參數(shù)bid Book book = bookService.load(bid);//通過bid得到book對象 req.setAttribute("book", book);//保存到req中 return "f:/jsps/book/desc.jsp";//轉(zhuǎn)發(fā)到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"; } }
-
購物車管理
-
功能
- 添加到購物車:用戶可以將書籍添加到購物車。
- 查看購物車:顯示購物車中的書籍及其數(shù)量。
- 更新購物車:用戶可以調(diào)整書籍?dāng)?shù)量或刪除書籍。
javapublic class CartItemServlet extends BaseServlet { private CartItemService cartItemService = new CartItemService(); /** * 加載多個(gè)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"; } }
管理員功能
-
功能:
- 添加刪除書籍:管理員可以輸入書籍信息并保存和刪除。
javapublic 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"; } }
總結(jié)
第一次沖刺成功地為網(wǎng)上購書系統(tǒng)建立了堅(jiān)實(shí)的基礎(chǔ)。我們實(shí)現(xiàn)了核心功能,包括用戶注冊、登錄、書籍瀏覽、購物車管理、以及基本的管理員功能。但仍有許多改進(jìn)和擴(kuò)展的空間。

浙公網(wǎng)安備 33010602011771號