1.共性问题:
1. 菜系管理:
如果删除某个菜系:
先判断当前的菜系有没有被菜品引用,如果有不能删除!
如果当前菜系没有被菜品引用,可以删除!
2. 菜品更新时候,图片显示
存储时候,存储图片的名称;
显示时候, 拼接:
相对目录/图片名称
/项目名/目录/*.jpg
注意:
图片名称不能有特殊字符;
图片名称不能太长
3. 编码
--à 数据库编码指定
-à 程序要处理编码
GET/POST
-à 指定在创建连接的时候,想数据库发送sql语句采用的编码
jdbc:MySQL:///hotel?useUnicode=true&characterEncoding=utf8
2.前台-餐桌dao实现
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 { /** * 根据预定状态查询 */ List<DinnerTable> findByStatus(TableStatus ts); /** * 主键查询 */ DinnerTable findById(int id);}package com.xp.entity;/** * 餐桌状态 * @author Jie.Yuan * */public enum TableStatus { /** * Free 餐车处于未预定 * PlanIn 餐车处于已预定状态 */ 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; // 未预定状态// } else {// status = 1;// }// try { return JdbcUtils.getQuerrRunner().query(sql, new BeanListHandler<DinnerTable>(DinnerTable.class), ts.ordinal()); } catch (Exception e) { throw new RuntimeException(e); } }}3.前台-餐桌显示完整实现package com.xp.service;import java.util.List;import com.xp.entity.DinnerTable;public interface IDinnerTableService { /** * 查询所有未预定的餐桌 */ List<DinnerTable> findNoUseTable(); /** * 根据主键查询 */ 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, 通常工厂创建实例 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的根据状态查询的方法,查询没有预定的餐桌 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;/** * 封装分页参数 */public class PageBean<T> { // 当前页 private int currentPage = 1; // 每页显示的行数 private int pageCount = 6; // 总记录数 private int totalCount; // 总页数 private int totalPage; // 每页的数据 private List<T> pageData; // 封装所有的查询条件 private Condition condition; public int getTotalPage() { // 总页数 = 总记录 / 每页显示行数 (+ 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); /** * 按条件统计菜品的总记录数 */ int getTotalCount(PageBean<Food> pb); /** * 根据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; 当当前页>总页数,设置当前页为总页数 ******/ // 先查询总记录数 int totalCount = getTotalCount(pb); // ? // 设置分页bean参数之总记录数 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()); // 把查询到的数据设置到分页对象中 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 { // 创建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 { /* * // 创建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) { // 默认执行的方法: 进入前台列表的首页 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 { // 保存跳转资源(转发/重定向) 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 { // 创建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. 获取当前运行类的字节码 Class clazz = this.getClass(); // 2. 获取当前执行的方法的Method类型 Method method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class); // 3. 执行方法 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,根据ID查询,再把查询到的结果保存到session (生成订单用) // 只需要执行一次即可: 先从session获取看有没有餐桌对象; 如果没有,就执行根据主键查询; // 如果sesison中已经有餐桌对象,就不执行主键查询 Object obj = session.getAttribute("dinnerTable"); // 判断 if (obj == null) { // 只在第一次执行的时候,查询餐桌对象 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>(); // 分页参数: 获取当前页参数 String curPage = request.getParameter("currentPage"); // 判断 if (curPage == null || "".equals(curPage.trim())) { // 第一次访问,设置当前页为1 pb.setCurrentPage(1); } else { // 【设置当前页参数】 pb.setCurrentPage(Integer.parseInt(curPage)); } // 条件对象 Condition condition = new Condition(); // 分页参数: 菜系id String foodTypeId = request.getParameter("foodTypeId"); if (foodTypeId != null) { // 如果类别为null,不作为条件,那就查询全部 // 设置条件 condition.setFoodTypeId(Integer.parseInt(foodTypeId)); } // 分页参数: 菜名称 String foodName = request.getParameter("foodName"); // 设置菜品参数 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 跳转(转发) return request.getRequestDispatcher("/app/caidan.jsp"); }}
新闻热点
疑难解答