1.共性問題:
1. 菜系管理:
如果刪除某個菜系:
先判斷當前的菜系有沒有被菜品引用,如果有不能刪除!
如果當前菜系沒有被菜品引用,可以刪除!
2. 菜品更新時候,圖片顯示
存儲時候,存儲圖片的名稱;
顯示時候, 拼接:
相對目錄/圖片名稱
/項目名/目錄/*.jpg
注意:
圖片名稱不能有特殊字符;
圖片名稱不能太長
3. 編碼
--à 數(shù)據(jù)庫編碼指定
-à 程序要處理編碼
GET/POST
-à 指定在創(chuàng)建連接的時候,想數(shù)據(jù)庫發(fā)送sql語句采用的編碼
jdbc:MySQL:///hotel?useUnicode=true&characterEncoding=utf8
2.前臺-餐桌dao實現(xiàn)


package com.xp.entity;import java.util.Date;public class DinnerTable { PRivate int id; private String tableName; private int tableStatus; private Date orderDate; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTableName() { return tableName; } public void setTableName(String tableName) { this.tableName = tableName; } public int getTableStatus() { return tableStatus; } public void setTableStatus(int tableStatus) { this.tableStatus = tableStatus; } public Date getOrderDate() { return orderDate; } public void setOrderDate(Date orderDate) { this.orderDate = orderDate; } @Override public String toString() { return "DinnerTable [id=" + id + ", tableName=" + tableName + ", tableStatus=" + tableStatus + ", orderDate=" + orderDate + "]"; }}package com.xp.dao;import java.util.List;import com.xp.entity.DinnerTable;import com.xp.entity.TableStatus;public interface IDinnerTableDao { /** * 根據(jù)預定狀態(tài)查詢 */ List<DinnerTable> findByStatus(TableStatus ts); /** * 主鍵查詢 */ DinnerTable findById(int id);}package com.xp.entity;/** * 餐桌狀態(tài) * @author Jie.Yuan * */public enum TableStatus { /** * Free 餐車處于未預定 * PlanIn 餐車處于已預定狀態(tài) */ Free,PlanIn;}package com.xp.dao.impl;import java.util.List;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import com.xp.dao.IDinnerTableDao;import com.xp.entity.DinnerTable;import com.xp.entity.TableStatus;import com.xp.utils.JdbcUtils;public class DinnerTableDao implements IDinnerTableDao { @Override public DinnerTable findById(int id) { String sql = "select * from dinnerTable where id=?"; try { return JdbcUtils.getQuerrRunner().query(sql, new BeanHandler<DinnerTable>(DinnerTable.class), id); } catch (Exception e) { throw new RuntimeException(e); } } @Override public List<DinnerTable> findByStatus(TableStatus ts) { String sql = "select * from dinnerTable where tableStatus=?";// int status = -1; // 判斷 // if (ts == TableStatus.Free) {// status = 0; // 未預定狀態(tài)// } else {// status = 1;// }// try { return JdbcUtils.getQuerrRunner().query(sql, new BeanListHandler<DinnerTable>(DinnerTable.class), ts.ordinal()); } catch (Exception e) { throw new RuntimeException(e); } }}3.前臺-餐桌顯示完整實現(xiàn)package com.xp.service;import java.util.List;import com.xp.entity.DinnerTable;public interface IDinnerTableService { /** * 查詢所有未預定的餐桌 */ List<DinnerTable> findNoUseTable(); /** * 根據(jù)主鍵查詢 */ DinnerTable findById(int id);}package com.xp.service.impl;import java.util.List;import com.xp.dao.IDinnerTableDao;import com.xp.entity.DinnerTable;import com.xp.entity.TableStatus;import com.xp.factory.BeanFactory;import com.xp.service.IDinnerTableService;public class DinnerTableService implements IDinnerTableService{ // 調用的Dao, 通常工廠創(chuàng)建實例 private IDinnerTableDao dinnerTableDao = BeanFactory.getInstance( "dinnerTableDao", IDinnerTableDao.class); @Override public DinnerTable findById(int id) { try { return dinnerTableDao.findById(id); } catch (Exception e) { throw new RuntimeException(e); } } @Override public List<DinnerTable> findNoUseTable() { try { // 調用dao的根據(jù)狀態(tài)查詢的方法,查詢沒有預定的餐桌 return dinnerTableDao.findByStatus(TableStatus.Free); } catch (Exception e) { throw new RuntimeException(e); } }}package com.xp.utils;import java.io.IOException;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class WebUtils { /** * 跳轉的通用方法 */ public static void goTo(HttpServletRequest request, HttpServletResponse response, Object uri) throws ServletException, IOException { if (uri instanceof RequestDispatcher){ ((RequestDispatcher)uri).forward(request, response); } else if (uri instanceof String) { response.sendRedirect(request.getContextPath() + uri); } }}package com.xp.entity;public class Food { private int id;// INT PRIMARY KEY AUTO_INCREMENT, -- 主鍵 private String foodName;// VARCHAR(20), -- 菜名稱 private int foodType_id;// INT, -- 所屬菜系, 外鍵字段 private double price;// DOUBLE, -- 價格 private double mprice;// DOUBLE, -- 會員價格 private String remark;// VARCHAR(200), -- 簡介 private String img;// VARCHAR(100) -- 圖片 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFoodName() { return foodName; } public void setFoodName(String foodName) { this.foodName = foodName; } public int getFoodType_id() { return foodType_id; } public void setFoodType_id(int foodTypeId) { foodType_id = foodTypeId; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getMprice() { return mprice; } public void setMprice(double mprice) { this.mprice = mprice; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getImg() { return img; } public void setImg(String img) { this.img = img; }}package com.xp.utils;/*** 封裝條件* * */public class Condition { // 菜的類別作為條件 private int foodTypeId; // 才的名稱作為條件 private String foodName; public int getFoodTypeId() { return foodTypeId; } public void setFoodTypeId(int foodTypeId) { this.foodTypeId = foodTypeId; } public String getFoodName() { return foodName; } public void setFoodName(String foodName) { this.foodName = foodName; }}package com.xp.utils;import java.util.List;/** * 封裝分頁參數(shù) */public class PageBean<T> { // 當前頁 private int currentPage = 1; // 每頁顯示的行數(shù) private int pageCount = 6; // 總記錄數(shù) private int totalCount; // 總頁數(shù) private int totalPage; // 每頁的數(shù)據(jù) private List<T> pageData; // 封裝所有的查詢條件 private Condition condition; public int getTotalPage() { // 總頁數(shù) = 總記錄 / 每頁顯示行數(shù) (+ 1) if (totalCount % pageCount == 0) { totalPage = totalCount / pageCount; } else { totalPage = totalCount / pageCount + 1; } return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List<T> getPageData() { return pageData; } public void setPageData(List<T> pageData) { this.pageData = pageData; } public Condition getCondition() { return condition; } public void setCondition(Condition condition) { this.condition = condition; }}package com.xp.dao;import com.xp.entity.Food;import com.xp.utils.PageBean;public interface IFoodDao { /** * 分頁且按條件查詢所有的菜品 */ void getAll(PageBean<Food> pb); /** * 按條件統(tǒng)計菜品的總記錄數(shù) */ int getTotalCount(PageBean<Food> pb); /** * 根據(jù)id查詢 */ Food findById(int id);}package com.xp.dao.impl;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import org.apache.commons.dbutils.handlers.ScalarHandler;import com.xp.dao.IFoodDao;import com.xp.entity.Food;import com.xp.utils.Condition;import com.xp.utils.JdbcUtils;import com.xp.utils.PageBean;public class FoodDao implements IFoodDao { @Override public Food findById(int id) { StringBuffer sb = new StringBuffer(); sb.append("select"); sb.append(" f.id,"); sb.append(" f.foodName,"); sb.append(" f.price,"); sb.append(" f.mprice,"); sb.append(" f.remark,"); sb.append(" f.img,"); sb.append(" f.foodType_id,"); sb.append(" t.typeName "); sb.append("from "); sb.append(" food f, "); sb.append(" foodtype t "); sb.append("where 1=1"); sb.append(" and f.foodType_id=t.id"); try { return JdbcUtils.getQuerrRunner().query(sb.toString(), new BeanHandler<Food>(Food.class)); } catch (Exception e) { throw new RuntimeException(e); } } @Override public void getAll(PageBean<Food> pb) { // 獲取條件對象 Condition condition = pb.getCondition(); // 條件之類別id int typeId = condition.getFoodTypeId(); // 條件之菜品名稱 String foodName = condition.getFoodName(); StringBuffer sb = new StringBuffer(); sb.append("select"); sb.append(" f.id,"); sb.append(" f.foodName,"); sb.append(" f.price,"); sb.append(" f.mprice,"); sb.append(" f.remark,"); sb.append(" f.img,"); sb.append(" f.foodType_id,"); sb.append(" t.typeName "); sb.append("from "); sb.append(" food f, "); sb.append(" foodtype t "); sb.append("where 1=1"); sb.append(" and f.foodType_id=t.id "); // 存儲查詢條件對應的值 List<Object> list = new ArrayList<Object>(); /******* 拼接查詢條件 *********/ // 類別id條件 if (typeId > 0) { sb.append(" and f.foodType_id=?"); list.add(typeId); // 組裝條件值 } // 菜品名稱 if (foodName != null && !"".equals(foodName.trim())) { sb.append(" and f.foodName like ?"); list.add(foodName); // 組裝條件值 } /********* 分頁條件 **********/ sb.append(" LIMIT ?,?"); /***** 判斷:當當前頁< 1, 設置當前頁為1; 當當前頁>總頁數(shù),設置當前頁為總頁數(shù) ******/ // 先查詢總記錄數(shù) int totalCount = getTotalCount(pb); // ? // 設置分頁bean參數(shù)之總記錄數(shù) pb.setTotalCount(totalCount); if (pb.getCurrentPage() < 1) { pb.setCurrentPage(1); } else if (pb.getCurrentPage() > pb.getTotalPage()) { pb.setCurrentPage(pb.getTotalPage()); } // 起始行 int index = (pb.getCurrentPage() - 1) * pb.getPageCount(); // 返回記錄行 int count = pb.getPageCount(); list.add(index); // 組裝條件值 - 起始行 list.add(count); // 組裝條件值 - 查詢返回的行 // 按條件、分頁查詢 try { List<Food> pageData = JdbcUtils.getQuerrRunner().query( sb.toString(), new BeanListHandler<Food>(Food.class), list.toArray()); // 把查詢到的數(shù)據(jù)設置到分頁對象中 pb.setPageData(pageData); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public int getTotalCount(PageBean<Food> pb) { // 獲取條件對象 Condition condition = pb.getCondition(); // 條件之類別id int typeId = condition.getFoodTypeId(); // 條件之菜品名稱 String foodName = condition.getFoodName(); StringBuffer sb = new StringBuffer(); sb.append("select"); sb.append(" count(*) "); sb.append("from "); sb.append(" food f, "); sb.append(" foodtype t "); sb.append("where 1=1"); sb.append(" and f.foodType_id=t.id "); // 存儲查詢條件對應的值 List<Object> list = new ArrayList<Object>(); /******* 拼接查詢條件 *********/ // 類別id條件 if (typeId > 0) { sb.append(" and f.foodType_id=?"); list.add(typeId); // 組裝條件值 } // 菜品名稱 if (foodName != null && !"".equals(foodName.trim())) { sb.append(" and f.foodName like ?"); list.add(foodName); // 組裝條件值 } try { // 查詢 Long num = JdbcUtils.getQuerrRunner().query(sb.toString(), new ScalarHandler<Long>(), list.toArray()); return num.intValue(); } catch (SQLException e) { throw new RuntimeException(e); } }}package com.xp.service;import com.xp.entity.Food;import com.xp.utils.PageBean;public interface IFoodService { /** * 主鍵查詢 */ Food findById(int id); /** * 分頁查詢 */ void getAll(PageBean<Food> pb);}package com.xp.service.impl;import com.xp.dao.IFoodDao;import com.xp.entity.Food;import com.xp.factory.BeanFactory;import com.xp.service.IFoodService;import com.xp.utils.PageBean;public class FoodService implements IFoodService { // 創(chuàng)建dao private IFoodDao foodDao = BeanFactory.getInstance("foodDao", IFoodDao.class); @Override public Food findById(int id) { try { return foodDao.findById(id); } catch (Exception e) { throw new RuntimeException(e); } } @Override public void getAll(PageBean<Food> pb) { try { foodDao.getAll(pb); } catch (Exception e) { throw new RuntimeException(e); } }}package com.xp.servlet;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.xp.entity.DinnerTable;public class IndexServlet extends BaseServlet { /* * // 創(chuàng)建Service private IDinnerTableService dinnerTableService = * BeanFactory.getInstance("dinnerTableService", IDinnerTableService.class); * * * * public void doGet(HttpServletRequest request, HttpServletResponse * response) throws ServletException, IOException { * * // 獲取操作的類型 String method = request.getParameter("method"); if (method == * null) { // 默認執(zhí)行的方法: 進入前臺列表的首頁 method = "listTable"; } * * // 判斷 if ("listTable".equals(method)) { // 1. 前臺首頁:顯示所有未預定的餐桌 * listTable(request,response); } * * } * * public void doPost(HttpServletRequest request, HttpServletResponse * response) throws ServletException, IOException { this.doGet(request, * response); } */ private static final long serialVersionUID = 1L; // 1. 前臺首頁:顯示所有未預定的餐桌 public Object listTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 保存跳轉資源(轉發(fā)/重定向) Object uri = null; // 查詢所有未預定餐桌 List<DinnerTable> list = dinnerTableService.findNoUseTable(); // 保存到request request.setAttribute("listDinnerTable", list); // 跳轉到首頁顯示 uri = request.getRequestDispatcher("/app/index.jsp"); return uri; // 跳轉 // WebUtils.goTo(request, response, uri); }}package com.xp.servlet;import java.io.IOException;import java.lang.reflect.Method;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.xp.factory.BeanFactory;import com.xp.service.IDinnerTableService;import com.xp.service.IFoodService;import com.xp.service.IFoodTypeService;import com.xp.utils.WebUtils;/** * 項目中通用的Servlet,希望所有的servlet都繼承此類 * @author Jie.Yuan */public abstract class BaseServlet extends HttpServlet { // 創(chuàng)建Service protected IDinnerTableService dinnerTableService = BeanFactory.getInstance("dinnerTableService", IDinnerTableService.class); protected IFoodTypeService foodTypeService = BeanFactory.getInstance("foodTypeService",IFoodTypeService.class); protected IFoodService foodService = BeanFactory.getInstance("foodService",IFoodService.class); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // (保存跳轉的資源) 方法返回值 Object returnValue = null; // 獲取操作類型; 【約定 > 俗成: 操作類型的值,必須對應servlet中的方法名稱】 String methodName = request.getParameter("method"); // listTable try { // 1. 獲取當前運行類的字節(jié)碼 Class clazz = this.getClass(); // 2. 獲取當前執(zhí)行的方法的Method類型 Method method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class); // 3. 執(zhí)行方法 returnValue = method.invoke(this, request,response); } catch (Exception e) { e.printStackTrace(); returnValue = "/error/error.jsp"; } // 跳轉 WebUtils.goTo(request, response, returnValue); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}package com.xp.servlet;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Httpsession;import com.xp.entity.DinnerTable;import com.xp.entity.Food;import com.xp.entity.FoodType;import com.xp.utils.Condition;import com.xp.utils.PageBean;public class FoodServlet extends BaseServlet { /** * 1. 進入主頁,顯示菜品以及菜系 */ public Object foodDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); // 1.1 獲取餐桌ID,根據(jù)ID查詢,再把查詢到的結果保存到session (生成訂單用) // 只需要執(zhí)行一次即可: 先從session獲取看有沒有餐桌對象; 如果沒有,就執(zhí)行根據(jù)主鍵查詢; // 如果sesison中已經有餐桌對象,就不執(zhí)行主鍵查詢 Object obj = session.getAttribute("dinnerTable"); // 判斷 if (obj == null) { // 只在第一次執(zhí)行的時候,查詢餐桌對象 String tableId = request.getParameter("tableId"); DinnerTable dt = dinnerTableService.findById(Integer .parseInt(tableId)); // 保存到session session.setAttribute("dinnerTable", dt); } // 1.2 查詢所有的“菜品信息”, 保存 PageBean<Food> pb = new PageBean<Food>(); // 分頁參數(shù): 獲取當前頁參數(shù) String curPage = request.getParameter("currentPage"); // 判斷 if (curPage == null || "".equals(curPage.trim())) { // 第一次訪問,設置當前頁為1 pb.setCurrentPage(1); } else { // 【設置當前頁參數(shù)】 pb.setCurrentPage(Integer.parseInt(curPage)); } // 條件對象 Condition condition = new Condition(); // 分頁參數(shù): 菜系id String foodTypeId = request.getParameter("foodTypeId"); if (foodTypeId != null) { // 如果類別為null,不作為條件,那就查詢全部 // 設置條件 condition.setFoodTypeId(Integer.parseInt(foodTypeId)); } // 分頁參數(shù): 菜名稱 String foodName = request.getParameter("foodName"); // 設置菜品參數(shù) condition.setFoodName(foodName); // 【設置條件對象到pb中】 pb.setCondition(condition); // ---->分頁查詢 foodService.getAll(pb); // 保存查詢后的pb對象 request.setAttribute("pb", pb); // 1.3 查詢所有的“菜系信息”, 保存 List<FoodType> listFoodType = foodTypeService.getAll(); request.setAttribute("listFoodType", listFoodType); // 1.4 跳轉(轉發(fā)) return request.getRequestDispatcher("/app/caidan.jsp"); }}
新聞熱點
疑難解答