您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > jsp+servlet+javabean-实现的简单网上购物车(附源码)
简单购物车案例(附源码)下面具体流程,很多功能都还未完善,之后会实现更多功能,敬请期待使用jsp的MVC模型开发购物车(jsp+servlet+javabean)必须有三层架构思想:web层负责与用户打交道业务处理层(服务层service)数据访问层(dao)1.搭建开发环境1)导入开发包:jdbc驱动(jstl的jar包)2)创建程序所在的包,如下:3)在MYSQL下创建库和表字段有:book,name,author,price,description2.编码1)开发实体类bean:Book2)开发dao(创建工具类)3)开发service4)web层3.实现效果如下:1)在浏览器输入:后如下图:2)进入后显示图书列表3)加入两种书进入购物车后4)当点击删除后5)点击清空购物车后6)结算功能还未实现4.附源码://BookDao.javapackagecom.hbsi.dao;importjava.util.List;importcom.hbsi.domain.Book;publicinterfaceBookDao{//获取所有的书publicListBookgetAll();//根据id获取书publicBookfind(Stringid);}//BookDaoImpl.javapackagecom.hbsi.dao;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.util.ArrayList;importjava.util.List;importcom.hbsi.domain.Book;importcom.hbsi.utils.DBManager;publicclassBookDaoImplimplementsBookDao{@OverridepublicBookfind(Stringid){Connectionconn=null;PreparedStatementpt=null;ResultSetrs=null;try{conn=DBManager.getConnection();Stringsql=select*frombookwhereid=?;pt=conn.prepareStatement(sql);pt.setString(1,id);rs=pt.executeQuery();//Bookb=null;if(rs.next()){Bookb=newBook();b.setId(rs.getString(id));b.setName(rs.getString(name));b.setAuthor(rs.getString(author));b.setPrice(rs.getDouble(price));b.setDescription(rs.getString(description));returnb;}returnnull;}catch(Exceptione){thrownewRuntimeException(e);}finally{DBManager.closeDB(conn,pt,rs);}}@OverridepublicListBookgetAll(){Connectionconn=null;PreparedStatementpt=null;ResultSetrs=null;try{conn=DBManager.getConnection();Stringsql=selectid,name,author,price,descriptionfrombook;pt=conn.prepareStatement(sql);rs=pt.executeQuery();ListBooklist=newArrayListBook();while(rs.next()){Bookb=newBook();b.setId(rs.getString(id));b.setName(rs.getString(name));b.setAuthor(rs.getString(author));b.setPrice(rs.getDouble(price));b.setDescription(rs.getString(description));list.add(b);}returnlist;}catch(Exceptione){thrownewRuntimeException(e);}finally{DBManager.closeDB(conn,pt,rs);}}}//Book.javapackagecom.hbsi.domain;publicclassBook{privateStringid;privateStringname;privateStringauthor;privatedoubleprice;privateStringdescription;publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetAuthor(){returnauthor;}publicvoidsetAuthor(Stringauthor){this.author=author;}publicdoublegetPrice(){returnprice;}publicvoidsetPrice(doubleprice){this.price=price;}publicStringgetDescription(){returndescription;}publicvoidsetDescription(Stringdescription){this.description=description;}}//Cart.javapackagecom.hbsi.domain;importjava.util.LinkedHashMap;importjava.util.Map;publicclassCart{privateMapString,CartItemmap=newLinkedHashMapString,CartItem();privatedoubleprice;//所有购物项的价格总计publicvoidadd(Bookbook){CartItemitem=map.get(book.getId());if(item!=null){item.setQuantity(item.getQuantity()+1);}else{item=newCartItem();item.setBook(book);item.setQuantity(1);//把新的购物项添加到map集合中map.put(book.getId(),item);}}publicMapString,CartItemgetMap(){returnmap;}publicvoidsetMap(MapString,CartItemmap){this.map=map;}publicdoublegetPrice(){doubletotalprice=0;for(Map.EntryString,CartItemme:map.entrySet()){CartItemitem=me.getValue();totalprice+=item.getPrice();}this.price=totalprice;returnprice;}publicvoidsetPrice(doubleprice){this.price=price;}}//CartItem.javapackagecom.hbsi.domain;//用于代表购买的商品(书)。包括书的数量。(购物项,购物车的一行)publicclassCartItem{privateBookbook;privateintquantity;privatedoubleprice;//对此类书的价格计算(小计)publicBookgetBook(){returnbook;}publicvoidsetBook(Bookbook){this.book=book;}publicintgetQuantity(){returnquantity;}publicvoidsetQuantity(intquantity){this.quantity=quantity;this.price=this.book.getPrice()*this.quantity;//书的单价乘以数量}publicdoublegetPrice(){returnprice;}publicvoidsetPrice(doubleprice){this.price=price;}}//BusinessService.javapackagecom.hbsi.service;importjava.util.List;importcom.hbsi.domain.Book;importcom.hbsi.domain.Cart;publicinterfaceBusinessService{publicListBookgetAllBook();//获取指定id的书publicBookfindBook(Stringid);//删除购物项publicvoiddeleteCartItem(Stringsid,Cartcart);//清空购物车publicvoidclearCart(Cartcart);//改变数量publicvoidchangeQuantity(Stringsid,Stringquantity,Cartcart);}//BusinessServiceImpl.javapackagecom.hbsi.service;importjava.util.List;importcom.hbsi.dao.BookDao;importcom.hbsi.dao.BookDaoImpl;importcom.hbsi.domain.Book;importcom.hbsi.domain.Cart;importcom.hbsi.domain.CartItem;publicclassBusinessServiceImplimplementsBusinessService{BookDaodao=newBookDaoImpl();@OverridepublicListBookgetAllBook(){returndao.getAll();}@OverridepublicvoiddeleteCartItem(Stringsid,Cartcart){cart.getMap().remove(sid);}@OverridepublicBookfindBook(Stringid){returndao.find(id);}@OverridepublicvoidclearCart(Cartcart){cart.getMap().clear();}@OverridepublicvoidchangeQuantity(Stringsid,Stringquantity,Cartcart){CartItemitem=cart.getMap().get(sid);item.setQuantity(Integer.parseInt(quantity));}}//db.propertiesdriver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/bookdbusername=rootpassword=root//Bu
本文标题:jsp+servlet+javabean-实现的简单网上购物车(附源码)
链接地址:https://www.777doc.com/doc-4758294 .html