寫這篇博客的時候很激動,按耐不住心情,為什么呢。因為經過兩周的時間終於完成了這個對於現在我來說比較龐大的一個項目。那么就切入正題吧,跟大家講講這個項目。
首先我需要講清楚:(1)這里展示只是部分功能
(2)如果需要詳細的功能可以私聯
包括:數據庫腳本及全部代碼
(3)聯系方式:
郵箱:ay_nzz@163.com
QQ: ay_nzz@163.com(是的你沒有看錯,跟郵箱一樣哦~)
簡介下關於易買網的各個功能模塊:
前台:
(1)登錄,注冊(詳解)
(2)按照分類查詢商品
(3)加入購物車(詳解)
(4)買家留言
(5)結賬
后台:
(1)賬號管理
(2)商品管理
(3)商品分類
(4)回復買家留言
(5)訂單管理(詳解)
一:項目都是由一層一層組織起來的,所以就用到分層
二:簡單的分層后就需要來解決各個層的功能,首先是我們經常使用的BaseDao(連接數據庫,增刪改的應用)
友情提示:大部分的程序都可以用BaseDao但是需要注意數據庫名稱的更改以及數據庫登錄密碼
1 /** 2 * 數據訪問工具類 3 * @version 4 * @author 5 * 6 */ 7 public class BaseDao { 8 // 01. 基礎內容的准備 9 private static final String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; 10 private static final String url="jdbc:sqlserver://localhost:1433;DataBaseName=easybuy_group"; 11 private static final String username="sa"; 12 private static final String pwd="123"; 13 //02, 接口對象的准備 14 Connection con=null; 15 PreparedStatement ps=null; 16 public ResultSet rs=null; 17 /** 18 * 01.寫一個用戶獲取到一個連接對象的方法,方法的返回值是Connection類型 19 * @return 連接對象 20 * @throws Exception 21 */ 22 public Connection getConnection() throws Exception{ 23 Class.forName(driver); 24 //什么條件下,構建connection對象 25 if (con==null||con.isClosed()) { 26 con=DriverManager.getConnection(url, username, pwd); 27 } 28 //同志們碰到一個 29 return con; 30 } 31 32 /** 33 * 通過連接池獲取連接對象 34 * @return 35 * @throws Exception 36 */ 37 38 /** 39 * 執行查詢操作 目的:返回一個讀取器 40 * @param sql sql語句 41 * @param objs 參數列表 42 * @return 讀取器對象 43 * @throws Exception 44 */ 45 public ResultSet executeQuery(String sql,Object... objs) throws Exception{ 46 con=getConnection(); 47 ps = con.prepareStatement(sql); 48 for (int i = 0; i < objs.length; i++) { 49 ps.setObject(i+1, objs[i]); 50 } 51 rs= ps.executeQuery(); 52 return rs; 53 } 54 55 /** 56 * 執行增刪該操作 57 * @param sql sql語句 58 * @param objs 參數列表 59 * @return 受影響行數 60 * @throws Exception 61 */ 62 public int executeUpdate(String sql,Object... objs) throws Exception{ 63 con=getConnection(); 64 ps = con.prepareStatement(sql); 65 for (int i = 0; i < objs.length; i++) { 66 ps.setObject(i+1, objs[i]); 67 } 68 int count = ps.executeUpdate(); 69 return count; 70 } 71 /** 72 * 回收連接資源 73 * @throws Exception 74 */ 75 public void closeAll() throws Exception{ 76 //倒着回收 77 if(rs!=null){ 78 rs.close(); 79 } 80 if (ps!=null) { 81 ps.close(); 82 } 83 if(con!=null){ 84 con.close(); 85 } 86 } 87 88 }
三:entity實體類在這里不詳細解釋了。
四:impl包(實現功能較多不在這里做多解釋)
前台:注冊登錄比較簡單,但是注冊用到ajax功能,相當於遠程對比注冊的用戶名是否已經存在,話不多說上代碼~
1 //功能模塊二:注冊功能 2 //添加到SQL 3 public int register(User user) throws Exception { 4 String sql = "insert into dbo.EASYBUY_USER(EU_USER_ID,EU_USER_NAME,EU_PASSWORD,EU_SEX,EU_BIRTHDAY, EU_IDENTITY_CODE, EU_EMAIL, EU_MOBILE, EU_ADDRESS, EU_STATUS, EU_LOGIN) values(?,?,?,?,?,?,?,?,?,?,?)"; 5 Object[] param={user.getEu_user_id(),user.getEu_user_name(),user.getEu_password(),user.isEu_sex(),user.getEu_birthday(),user.getEu_identity_code(),user.getEu_mobile(),user.getEu_email(),user.getEu_address(),user.getEu_status(),user.isEu_login()}; 6 return executeUpdate(sql,param); 7 }
1 //ajax的遠端控制 2 public void doPost(HttpServletRequest request, HttpServletResponse response) 3 throws ServletException, IOException { 4 //解決中文亂碼 5 request.setCharacterEncoding("utf-8"); 6 //實現UserDaoImpl類的對象 7 UserDaoImpl impl=new UserDaoImpl(); 8 String uName=request.getParameter("name"); 9 //String uName="admin"; 10 String msg=null; 11 if (uName!=null) { 12 try { 13 List<User> list=impl.GetUnameById(uName); 14 for (User user : list) { 15 if (uName.equals(user.getEu_user_id())) { 16 msg="true"; 17 break; 18 }else { 19 msg="false"; 20 } 21 } 22 } catch (Exception e) { 23 // 異常抓取 24 e.printStackTrace(); 25 } 26 } 27 response.getWriter().print(msg); 28 }
1 $(function(){ 2 //焦點移出表單時 3 $("#userId").blur(function(){ 4 ajax(); 5 }); 6 }); 7 function ajax(){ 8 //獲取用戶名 9 var uname=$("#userId").val(); 10 $.ajax({ 11 url:'<%=path%>/servlet/RegisterServlet', 12 type:'POST', 13 data:'name='+uname, 14 //data是從servlet回送的內容 15 success:function(data){ 16 if(data=="true"){ 17 $("#msg").html("該用戶名已存在").addClass("error"); 18 }else{ 19 $("#msg").html("用戶名可用").addClass("error"); 20 } 21 } 22 }); 23 }
那就挑一個難點吧,(后台)訂單實現功能:
1 添加訂單詳情 2 public int addOrder_Detail(Order_Detail orde) throws Exception { 3 //EOD_ID, EO_ID, EP_ID, EOD_QUANTITY, EOD_COST 4 String sql="insert into easybuy_order_detail values(?,?,?,?)";//?代表數據庫訂單表有幾列 5 Object[] objs={orde.getEo_id(),orde.getEp_id(),orde.getEod_quantity(),orde.getEod_cost()}; 6 return executeUpdate(sql, objs); 7 }
Servlet:
1 OrderGroupDaoImpl_wth ogdi=new OrderGroupDaoImpl_wth(); 2 public void doPost(HttpServletRequest request, HttpServletResponse response) 3 throws ServletException, IOException { 4 request.setCharacterEncoding("utf-8"); 5 String name=request.getParameter("userName"); 6 String id=request.getParameter("entityId"); 7 if (!name.isEmpty()&&!id.isEmpty()) { 8 //雙條件查詢 9 System.out.println("哈哈"); 10 try { 11 12 13 Map<Order, List<OrderGroup>> ordermap = ogdi.getOrderInfoByidAndName(id, name); 14 request.getSession().setAttribute("ordermap", ordermap); 15 request.getRequestDispatcher("/manage/order.jsp").forward(request,response); 16 } catch (Exception e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } 20 21 }else if(name.isEmpty()&&!id.isEmpty()){ 22 //單號 23 24 try { 25 Map<Order, List<OrderGroup>> ordermap = ogdi.getOrderInfoByid(id); 26 request.getSession().setAttribute("ordermap", ordermap); 27 request.getRequestDispatcher("/manage/order.jsp").forward(request,response); 28 } catch (Exception e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 33 }else if(!name.isEmpty()&&id.isEmpty()){ 34 //訂貨人 35 try { 36 Map<Order, List<OrderGroup>> ordermap = ogdi.getOrderInfoName(name); 37 request.getSession().setAttribute("ordermap", ordermap); 38 request.getRequestDispatcher("/manage/order.jsp").forward(request,response); 39 } catch (Exception e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 }else{ 44 //所有 45 try { 46 Map<Order, List<OrderGroup>> ordermap= ogdi.getOrderInfo(); 47 request.getSession().setAttribute("ordermap", ordermap); 48 request.getRequestDispatcher("/manage/order.jsp").forward(request,response); 49 } catch (Exception e) { 50 // TODO Auto-generated catch block 51 e.printStackTrace(); 52 } 53 54 } 55 56 } 57 58 }
再說一個加入購物車的功能:
1 Product_CategroyDaoImpl_wth si=new Product_CategroyDaoImpl_wth(); 2 public Product UploadPhoto(HttpServletRequest request, HttpServletResponse response){ 3 Product p1=new Product(); 4 try { 5 6 //上傳的文件名 7 String uploadFileName=""; 8 //表單字段元素的name屬性值 9 String filedName=""; 10 //請求信息中的內容是否是multipart類型 11 boolean ismultipart = ServletFileUpload.isMultipartContent(request); 12 //上傳文件的儲存路徑(服務器文件系統上的絕對路徑) 13 String pathString=request.getSession().getServletContext().getRealPath("/images/product"); 14 if (ismultipart) { 15 FileItemFactory factory=new DiskFileItemFactory(); 16 ServletFileUpload upload=new ServletFileUpload(factory); 17 try { 18 //解析form表單中所有文件 19 List<FileItem> items = upload.parseRequest(request); 20 Iterator<FileItem> iter = items.iterator(); 21 while (iter.hasNext()) { //依次處理每個文件 22 FileItem item = iter.next(); 23 //獲取商品的編號 24 25 if (item.isFormField()) { //普通表單字段 26 filedName = item.getFieldName(); //表單字段的name屬性值 27 if (filedName.equals("productName")) { 28 //商品名稱 29 p1.setEp_name(item.getString("utf-8")); 30 System.out.println(p1.getEp_name()); 31 }else if(filedName.equals("productPrice")){ 32 //商品價格 33 p1.setEp_price(Float.valueOf(item.getString("utf-8"))); 34 System.out.println(p1.getEp_price()); 35 }else if(filedName.equals("productDetail")){ 36 //商品描述 37 p1.setEp_description(item.getString("utf-8")); 38 System.out.println(p1.getEp_description()); 39 }else if(filedName.equals("parentId")){ 40 int fenleiid=Integer.parseInt(item.getString("utf-8")); 41 //商品分類 二級分類 42 p1.setEpc_child_id(fenleiid); 43 //根據二級獲取一級 44 int oneIdOfTwoId = si.OneIdOfTwoId(fenleiid); 45 //商品分類 一級分類 46 p1.setEpc_id(oneIdOfTwoId); 47 System.out.println(p1.getEpc_child_id()); 48 System.out.println(p1.getEpc_id()); 49 }else if(filedName.equals("productNumber")){ 50 //商品庫存 51 p1.setEp_stock(Integer.parseInt(item.getString("utf-8"))); 52 System.out.println(p1.getEp_stock()); 53 } 54 }else { //文件表單字段 55 String fileName = item.getName(); 56 if (fileName!=null && !fileName.equals("")) { 57 File fullfile=new File(item.getName()); 58 File savFile=new File(pathString,fullfile.getName()); 59 item.write(savFile); 60 uploadFileName=fullfile.getName(); 61 p1.setEp_file_name(uploadFileName); 62 System.out.println("上傳成功后的文件名是:"+uploadFileName); 63 64 } 65 } 66 } 67 68 } catch (FileUploadException e) { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 } 72 } 73 }catch (Exception e) { 74 // TODO Auto-generated catch block 75 e.printStackTrace(); 76 } 77 return p1; 78 } 79 80 }
(這里用到部分功能包在這里不做詳細講解需要功能包的可以私聯)
五:在這里透露一下關於這個項目的jsp頁面
總體講一個比較精美的網站是少不了各個功能模塊的“裝飾”,只有亮點更多的網站才會收到大家的青睞。從易買網這個不大不小的項目可以看出來團隊合作是很重要的,將來也好,現在也罷。這樣的一個項目交給我們可能一個人是解決不了的,當然未來並不一定。
首先很感謝你可以看完這篇博客,其次如果你對這篇博客不滿或者有有問題可以在下面提出來。如果有需要可以私聯哦~