專業的易買網的後台商品管理模塊講解
數據庫表:SQL Server
相關聯的表有: 1.商品信息表 EASYBUY_PRODUCT 2.商品分類表 EASYBUY_PRODUCT_CATEGORY 項目架構圖: dao編寫接口,書寫實現功能的方法public interface ProductDao_hb { //獲取所有的商品信息 public List<Product> getAllInfo() throws Exception; //獲取當前頁的商品信息 public List<Product> getOneProductData(int pageIndex,int pageSize) throws Exception; //總記錄數 public int getAllCount() throws Exception; //新增商品 public boolean AddProduct(Product p) throws Exception; //修改商品信息 public boolean UpProduct(Product p) throws Exception; //刪除商品 public boolean DelProduct(int id) throws Exception; }
impl實現dao接口並且重寫所有dao方法
這裡一般是執行SQL語句
public class ProductDaoImpl_hb extends BaseDao implements ProductDao_hb{ //查詢ID,照片,商品名稱 public List<Product> getAllInfo() throws Exception { List<Product> list=new ArrayList<Product>(); String sql="select EP_ID,EP_FILE_NAME,EP_NAME from EASYBUY_PRODUCT"; ResultSet rs=executeSelect(sql); if (rs!=null) { while (rs.next()) { //實體類 Product pros=new Product(); pros.setId(rs.getInt("EP_ID")); pros.setFileName(rs.getString("EP_FILE_NAME")); pros.setName(rs.getString("EP_NAME")); list.add(pros); } } return list; } /** * 分頁顯示數據 * pageindex:顯示有幾頁 * pagesize:顯示幾條數據 * @throws Exception */ public List<Product> getOneProductData( int pageIndex, int pageSize) throws Exception { List<Product> list=new ArrayList<Product>(); String sql="select top "+pageSize+" * from EASYBUY_PRODUCT where EP_ID not in (select top "+(pageIndex-1)*pageSize+" EP_ID from EASYBUY_PRODUCT)"; ResultSet rs=executeSelect(sql); if(rs!=null){ while(rs.next()){ Product pro=new Product(); pro.setId(rs.getInt("EP_ID")); pro.setFileName(rs.getString("EP_FILE_NAME")); pro.setName(rs.getString("EP_NAME")); list.add(pro); } } return list; } //查詢總記錄數 public int getAllCount() throws Exception { int result=0; String sql="select count(1) as num from EASYBUY_PRODUCT"; ResultSet rs=executeSelect(sql); if(rs!=null){ if (rs.next()) { result=rs.getInt("num"); } } return result; } //添加商品信息 public boolean AddProduct( Product p) throws Exception { boolean flag=false; String sql="select EPC_PARENT_ID from dbo.EASYBUY_PRODUCT_CATEGORY where EPC_ID="+p.getChildCategoryId()+""; ResultSet rs=this.executeSelect(sql); if (rs!=null) { while (rs.next()) { p.setCategoryId(rs.getInt("EPC_PARENT_ID")); flag=true; } } //添加列的名稱 //(EP_ID, EP_NAME, EP_DESCRIPTION, EP_PRICE, EP_STOCK, EPC_ID, EPC_CHILD_ID, EP_FILE_NAME) String Addsql="insert into EASYBUY_PRODUCT values(?,?,?,?,?,?,?)"; Object[] objs={ p.getName(), p.getDescription(), p.getPrice(), p.getStock(), p.getCategoryId(), p.getChildCategoryId(), p.getFileName(), }; flag =this.executeUpdate(Addsql, objs); return flag; } //修改商品信息 public boolean UpProduct( Product p) throws Exception { boolean flag=false; String sql="update EASYBUY_PRODUCT set EP_NAME=?,EP_DESCRIPTION=?,EPC_ID=?,EPC_CHILD_ID=?,EP_PRICE=?,EP_STOCK=?,EP_FILE_NAME=? where EP_ID=?"; Object[] paras={ p.getName(), p.getDescription(), p.getCategoryId(), p.getChildCategoryId(), p.getPrice(), p.getStock(), p.getFileName(), p.getId() }; flag=executeUpdate(sql, paras); return flag; } //不修改圖片的方法 //修改商品信息 public boolean UpPic( Product p) throws Exception { boolean flag=false; String sql="update EASYBUY_PRODUCT set EP_NAME=?,EP_DESCRIPTION=?,EPC_ID=?,EPC_CHILD_ID=?,EP_PRICE=?,EP_STOCK=? where EP_ID=?"; Object[] paras={ p.getName(), p.getDescription(), p.getCategoryId(), p.getChildCategoryId(), p.getPrice(), p.getStock(), p.getId() }; flag=executeUpdate(sql, paras); return flag; } //刪除商品信息 public boolean DelProduct( int id) throws Exception { String sql ="delete from EASYBUY_PRODUCT where EP_ID=?"; Object[] prams={id}; return executeUpdate(sql, prams); } //通過id查詢出商品信息並保存到list裡面 public List<Product> updateInfo(int id) throws Exception{ List<Product> list=new ArrayList<Product>(); String sql="select * from EASYBUY_PRODUCT where EP_ID=?"; Object[] paras={id}; ResultSet rs=executeSelect(sql,paras); if(rs!=null){ while(rs.next()){ Product pro=new Product(); pro.setId(rs.getInt("EP_ID")); pro.setFileName(rs.getString("EP_FILE_NAME")); pro.setName(rs.getString("EP_NAME")); pro.setDescription(rs.getString("EP_DESCRIPTION")); pro.setPrice(rs.getFloat("EP_PRICE")); pro.setStock(rs.getInt("EP_STOCK")); pro.setCategoryId(rs.getInt("EPC_ID")); pro.setChildCategoryId(rs.getInt("EPC_CHILD_ID")); list.add(pro); } } return list; } }
在servlet用來:請求對象的信息,處理請求,訪問其他資源,獲得需要的信息
public class Servlet extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } ProductDaoImpl_hb pdimpl=new ProductDaoImpl_hb(); ProoductCategoryDaoImpl_hyj pcdimpl=new ProoductCategoryDaoImpl_hyj(); public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String opr=request.getParameter("opr"); if(opr!=null){ if(opr.equals("addProduct")){ //----------加載下拉框中的信息 String name=request.getParameter("name"); try { //1獲取一級分類 List<ProductCategory> listOnes=pcdimpl.getAllOneLeveInfo(3); List<ProductCategory> listOne=pcdimpl.getAllOneLeveInfo(0); //2.獲取二級分類 List<ProductCategory> listTwo=pcdimpl.getAllTowLeveInfo(0); for ( ProductCategory item : listOne) { System.out.println(item.getName()); } //3.設置到作用域中 request.getSession().setAttribute("listOne",listOne); request.getSession().setAttribute("listTwo",listTwo); request.getSession().setAttribute("listOnes",listOnes); //4.跳轉到添加頁面 進行判斷跳轉頁面 if (name.equals("in")) { //修改頁面 String id=request.getParameter("nid"); int nid=0; if (id!=null&&!(id.equals(""))) { nid=Integer.parseInt(id); } try { List<Product> list=pdimpl.updateInfo(nid); request.setAttribute("cid",list.get(0).getChildCategoryId()); System.out.println(list.get(0).getChildCategoryId()); request.setAttribute("list",list); request.getRequestDispatcher("/manage/product-modify.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); } }else{//新增頁面 request.getRequestDispatcher("/manage/product-add.jsp").forward(request, response); } } catch ( Exception e) { e.getMessage(); } } if(opr.equals("addNewProduct")){ //添加商品 try { Product p=AddProduct(request,response); boolean flag=pdimpl.AddProduct(p); if (flag) { request.getRequestDispatcher("/manage/manage-result.jsp").forward(request, response); } } catch ( Exception e) { e.printStackTrace(); } } //刪除 if(opr.equals("delete")){ String pid=request.getParameter("name"); if(pid!=null){ int id =Integer.parseInt(pid); try { boolean flag=pdimpl.DelProduct(id); if (flag) { request.getRequestDispatcher("/manage/manage-result.jsp").forward(request, response); } } catch (Exception e) { e.printStackTrace(); } } } //修改 if (opr.equals("update")) { try { Product p=new Product(); boolean flag=false; p= AddProduct(request,response); p.setId(Integer.parseInt(request.getParameter("id"))); if (p.getFileName()==null) { flag=pdimpl.UpPic(p); }else{ flag=pdimpl.UpProduct(p); } if (flag) { request.getRequestDispatcher("/manage/manage-result.jsp").forward(request, response); } } catch (Exception e) { e.printStackTrace(); } } if(opr.equals("list")){ ProductDaoImpl_hb dao=new ProductDaoImpl_hb(); //分頁 try { //實例化page對象,給page的index和size賦值 //顯示第一頁 Page_hb page=new Page_hb(); //顯示的數量 int pageSize=3; page.setPageSize(pageSize); int myindex=1; //當前頁 String pageIndex=request.getParameter("pageIndex"); if (pageIndex!=null&&(!pageIndex.equals(""))) { myindex=Integer.parseInt(pageIndex); int mypageCount=0; //總頁數 int pageCount=dao.getAllCount(); if (pageCount%pageSize==0) { mypageCount=pageCount/pageSize; }else { mypageCount=pageCount/pageSize+1; } page.setPageCount(mypageCount); } if (myindex<1) { myindex=1; }else if(myindex>page.getPageCount()){ myindex=page.getPageCount(); } //當前頁賦值 page.setPageIndex(myindex); List<Product> list=dao.getOneProductData(page.getPageIndex(), page.getPageSize()); page.setList(list); request.setAttribute("listone", page); request.getRequestDispatcher("/manage/product.jsp").forward(request, response); } catch ( Exception e) { e.printStackTrace(); } } } } //添加商品方法 public Product AddProduct( HttpServletRequest request, HttpServletResponse response) throws Exception { Product p = new Product(); request.setCharacterEncoding("utf-8"); //文件上傳 //上傳文件名 String uploadFileName=""; //表單字段元素 String fieldName=""; //請求信息中的額內容是否是multipart類型 boolean isMultipart=ServletFileUpload.isMultipartContent(request); //上傳文件存儲路徑(服務器文件系統上的絕對路徑) String uploadFilePath=request.getSession().getServletContext().getRealPath("/images/product/"); if (isMultipart) { request.setCharacterEncoding("utf-8"); FileItemFactory factory=new DiskFileItemFactory(); //解析器 ServletFileUpload upload=new ServletFileUpload(factory); try { //解析from表單中所有文件 List<FileItem> items = upload.parseRequest(request); Iterator<FileItem> iter =items.iterator(); while (iter.hasNext()) { //依次處理每個文件 FileItem item=(FileItem) iter.next(); //普通表單字段 if (item.isFormField()) { fieldName= item.getFieldName(); //表單字段的name屬性 if(fieldName.equals("productName")){ //輸出表單阻斷的值 //System.out.println(item.getString("utf-8")); p.setName(item.getString("utf-8")); }else if(fieldName.equals("productDetail")){ //System.out.println(item.getString("utf-8")); p.setDescription(item.getString("utf-8")); }else if(fieldName.equals("parentId")){ //System.out.println(item.getString("utf-8")); p.setChildCategoryId(Integer.parseInt(item.getString("utf-8"))); }else if(fieldName.equals("productPrice")){ //System.out.println(item.getString("utf-8")); p.setPrice(Float.valueOf(item.getString("utf-8"))); }else if(fieldName.equals("productNumber")){ p.setStock(Integer.parseInt(item.getString("utf-8"))); } }else{ //文件表單字段 String fileName=item.getName(); //文件名字 if(fileName!=null && !fileName.equals("")){ File fullFile=new File(item.getName()); File saveFile = new File(uploadFilePath,fullFile.getName()); item.write(saveFile); uploadFileName=fullFile.getName(); //上傳文件名 p.setFileName(uploadFileName); } } } } catch ( Exception e) { e.printStackTrace(); } } return p; } }
這裡我講解一下分頁吧
//分頁 try { //實例化page對象,給page的index和size賦值 //顯示第一頁 Page_hb page=new Page_hb(); //顯示的數量 int pageSize=3; page.setPageSize(pageSize); int myindex=1; //當前頁 String pageIndex=request.getParameter("pageIndex"); if (pageIndex!=null&&(!pageIndex.equals(""))) { myindex=Integer.parseInt(pageIndex); int mypageCount=0; //總頁數 int pageCount=dao.getAllCount(); if (pageCount%pageSize==0) { mypageCount=pageCount/pageSize; }else { mypageCount=pageCount/pageSize+1; } page.setPageCount(mypageCount); } if (myindex<1) { myindex=1; }else if(myindex>page.getPageCount()){ myindex=page.getPageCount(); } //當前頁賦值 page.setPageIndex(myindex); List<Product> list=dao.getOneProductData(page.getPageIndex(), page.getPageSize()); page.setList(list); request.setAttribute("listone", page); request.getRequestDispatcher("/manage/product.jsp").forward(request, response); } catch ( Exception e) { e.printStackTrace(); }
這是分頁的代碼,眾所周知當你在網上happy(搜索信息)的時候,當前的頁面沒有你需要信息你會點擊下一頁來繼續尋找信息,我所書寫的功能和這個類似吧
工具類:
/** *提供分頁使用的工具類 * @author HYJ * */ public class Page_hb { private int pageIndex=1;//當前顯示第幾頁數據 private int pageSize=3;//每頁顯示幾條數據 private int pageSum;//總記錄數 private int pageCount;//總頁數 private List<Product> list;//保存商品分頁信息的集合 public Page_hb() { super(); // TODO Auto-generated constructor stub } public Page_hb(int pageIndex, int pageSize, int pageSum, int pageCount, List<Product> list) { super(); this.pageIndex = pageIndex; this.pageSize = pageSize; this.pageSum = pageSum; this.pageCount = pageCount; this.list = list; } public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { //pageindex是外界調用這個方法傳過來的值,當pageIndex的值大於總頁數的,就讓他顯示最後一頁 if(pageIndex>pageCount){ pageIndex=pageCount;//把pageIndex的值改為總頁數顯示最後一頁 } else if(pageIndex<0){//如果傳過來的值小於0,就讓pageIndex的值為1,顯示第一頁的數據 pageIndex=1; } else{ this.pageIndex = pageIndex; } } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageSum() { return pageSum; } public void setPageSum(int pageSum) { this.pageSum = pageSum; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public List<Product> getList() { return list; } public void setList(List<Product> list) { this.list = list; } }
根據工具類:設置設置顯示的數量
//實例化page對象,給page的index和size賦值 //顯示第一頁 Page_hb page=new Page_hb(); //顯示的數量 int pageSize=3; page.setPageSize(pageSize);
設置當前頁,和根據從數據庫中查詢的count值除每頁顯示的數量=總頁數
並且判斷當前頁的值和1去比較如果小於1那麼就顯示第一頁的數據
沒有理由別問我
int myindex=1; //當前頁 String pageIndex=request.getParameter("pageIndex"); if (pageIndex!=null&&(!pageIndex.equals(""))) { myindex=Integer.parseInt(pageIndex); int mypageCount=0; //總頁數 int pageCount=dao.getAllCount(); if (pageCount%pageSize==0) { mypageCount=pageCount/pageSize; }else { mypageCount=pageCount/pageSize+1; } page.setPageCount(mypageCount); } if (myindex<1) { myindex=1; }else if(myindex>page.getPageCount()){ myindex=page.getPageCount(); }
把當前頁的值賦給頁面
//當前頁賦值 page.setPageIndex(myindex); List<Product> list=dao.getOneProductData(page.getPageIndex(), page.getPageSize()); page.setList(list); request.setAttribute("listone", page); request.getRequestDispatcher("/manage/product.jsp").forward(request, response);
寫這個項目的大概流程就是這些了
可能寫的不是太好,請大家多多理解
謝謝欣賞
public interface ProductDao_hb {
//獲取所有的商品信息
public List<Product> getAllInfo() throws Exception;
//獲取當前頁的商品信息
public List<Product> getOneProductData(int pageIndex,int pageSize) throws Exception;
//總記錄數
public int getAllCount() throws Exception;
//新增商品
public boolean AddProduct(Product p) throws Exception;
//修改商品信息
public boolean UpProduct(Product p) throws Exception;
//刪除商品
public boolean DelProduct(int id) throws Exception;
}