/**
* 封装条件
*
* @author zm
*
*/
public class Condition {
* 封装条件
*
* @author zm
*
*/
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;
}
}
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;
}
}
/**
* 封装分页参数
* @author zm
*
*/
public class PageBean<T> {
* 封装分页参数
* @author zm
*
*/
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;
}
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
this.currentPage = currentPage;
}
public int getPageCount() {
return pageCount;
}
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
this.pageCount = pageCount;
}
public int getTotalCount() {
return totalCount;
}
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
this.totalCount = totalCount;
}
public List<T> getPageData() {
return pageData;
}
return pageData;
}
public void setPageData(List<T> pageData) {
this.pageData = pageData;
}
this.pageData = pageData;
}
public Condition getCondition() {
return condition;
}
return condition;
}
public void setCondition(Condition condition) {
this.condition = condition;
}
}
this.condition = condition;
}
}
//DAO
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);
}
}
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);
}
}
// 获取条件对象
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);
}
}
//Service
public void getAll(PageBean<Food> pb) {
try {
foodDao.getAll(pb);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
try {
foodDao.getAll(pb);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//Servlet
/**
* 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);
* 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");
}
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");
}