1.项目需求分析
JavaWeb增强:
过滤器、监听器、国际化、文件上传与下载、JavaMail邮件开发
回顾项目开发设计模式之mvc模式:
M model 模型层 【entity/dao/service】
V view 视图层 【jsp/html…】
C control 控制器 【servlet】
优缺点:
每一层各司其职! 更容易更换具体的实现,而对其他层代码影响较小!
目标:
1. 掌握mvc开发模式
2. 需求分析
《需求分析说明书.doc》
3. 设计数据库
每一组用同一个数据库。
4. 编码
组长:分配任务,每个人要做什么 (项目计划)
项目开发生命周期
软件项目开发模式:
螺旋开发模式
适合于项目前期部分需求不确定的情况, 对于每一个模块一个个开发:分析、设计、编码、测试、上线;
好处: 降低软件风险! (做出的产品要尽量满足客户需求!)
瀑布模式:
先进行所有模块的需求分析,当分析结束后,才进入项目下一个阶段, 即设计、编码、测试、上线;
好处: 更容易进行项目把控,即项目质量控制!
需求分析:
软件工程师: 了解需求的途径?
1. 需求文档
2. 项目经理
3. 项目的系统原型 (美工设计师)
4. 客户
“餐馆王”系统功能:
1. 餐桌模块
2. 菜类别模块(菜系)
3. 菜信息 (菜品)
4. 订单
详细分析:
1. 后台录入的餐桌, 要在前台首页显示; 且只显示未预定
2. 后台录入的菜类别, 在前台主页显示
3. 后台录入的菜信息,在前台主页显示
4. 前台生成订单后,在后台显示订单详细
2.数据库设计
-- 创建数据库
CREATE DATABASE hotel CHARACTER SET utf8;
USE hotel;
-- 1. 餐桌表
CREATE TABLE dinnerTable(
id INT PRIMARY KEY AUTO_INCREMENT, -- 餐桌主键
tableName VARCHAR(20), -- 餐桌名
tableStatus INT DEFAULT 0, -- 餐桌状态:0,空闲; 1,预定
orderDate DATETIME
);
-- 2.菜类别表
CREATE TABLE foodType(
id INT PRIMARY KEY AUTO_INCREMENT, -- 类别主键
typeName VARCHAR(20)
);
-- 3. 菜品种表
CREATE TABLE food(
id INT PRIMARY KEY AUTO_INCREMENT, -- 主键
foodName VARCHAR(20), -- 菜名称
foodType_id INT, -- 所属菜系, 外键字段
price DOUBLE, -- 价格
mprice DOUBLE, -- 会员价格
remark VARCHAR(200), -- 简介
img VARCHAR(100) -- 图片
);
-- 4. 订单表 (订单基本信息)
CREATE TABLE orders(
id INT PRIMARY KEY AUTO_INCREMENT, -- 主键
table_id INT, -- 外键: 餐桌编号
orderDate DATETIME, -- 下单日期
totalPrice DOUBLE, -- 订单所有菜需要的总金额
orderStatus INT DEFAULT 0 -- 订单状态: 0,未结账; 1,已结账
);
-- 5. 订单明细表 (主要是菜品种)
CREATE TABLE orderDetail(
id INT PRIMARY KEY AUTO_INCREMENT, -- 主键
orderId INT, -- 外键:引入的是订单表的主键
food_id INT, -- 外键:引用的是菜信息表的主键
foodCount INT -- 菜的数量
);
-- 添加菜品与菜类别的关系约束
ALTER TABLE food ADD CONSTRAINT fk_food_foodType_id FOREIGN KEY(foodType_id) REFERENCES foodType(id);
-- 订单表: 与餐桌表的关系
ALTER TABLE orders ADD CONSTRAINT order_table_id FOREIGN KEY(table_id) REFERENCES dinnertable(id);
-- 订单明细: 与订单表的关系
ALTER TABLE orderDetail ADD CONSTRAINT orderDetail_order_id FOREIGN KEY(orderId) REFERENCES orders(id);
-- 订单明细: 与菜信息的关系
ALTER TABLE orderDetail ADD CONSTRAINT orderDetail_food_id FOREIGN KEY(food_id) REFERENCES food(id);
系统设计
开源组件及jar文件:
数据库驱动包(1个)
C3P0连接池包(2 个)
DbUtils组件 (1个)
BeanUtils组件(2个)
FileUpload组件(2个)
配置
C3p0配置文件
分层:
Entity/dao/service/servlet
3.页面显示如图
4.编码如下
package com.xp.utils;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 封装jdbc常用的操作
* @author xiongpan
* 2017年2月16日
*/
public class JdbcUtils {
// 初始化连接池
private static DataSource dataSource;
static {
dataSource = new ComboPooledDataSource();
}
public static DataSource getDataSource() {
return dataSource;
}
/**
* 创建DbUtils常用工具类对象
*/
public static QueryRunner getQuerrRunner() {
return new QueryRunner(dataSource);
}
}
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///hotel</property>
<property name="user">root</property>
<property name="password">xiongpan</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
</default-config>
<named-config name="oracleConfig">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///hotel</property>
<property name="user">root</property>
<property name="password">xiongpan</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>
package com.xp.entity;
/**
* 1.菜系模块,实体类设计
* @author xiongpan
* 2017年2月16日
*/
public class FoodType {
private int id;//类别主键
private String typeName;//类别名称
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
package com.xp.dao;
import java.util.List;
import com.xp.entity.FoodType;
/**
* 菜系模块,dao接口设计
* @author xiongpan
*/
public interface IFoodTypeDao {
/**
* 添加
*/
void save(FoodType foodType);
/**
* 更新
*/
void update(FoodType foodType);
/**
* 删除
*/
void delete(int id);
/**
* 根据主键查询
*/
FoodType findById(int id);
/**
* 查询全部菜谱
*/
List<FoodType> getAll();
/**
* 按照菜系名称查询
*/
List<FoodType> getAll(String typeName);
}
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.IFoodTypeDao;
import com.xp.entity.FoodType;
import com.xp.utils.JdbcUtils;
public class FoodTypeDao implements IFoodTypeDao {
@Override
public void delete(int id) {
String sql = "delete from foodType where id=?";
try {
JdbcUtils.getQuerrRunner().update(sql, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public FoodType findById(int id) {
String sql = "select * from foodType where id=?";
try {
return JdbcUtils.getQuerrRunner().query(sql, new BeanHandler<FoodType>(FoodType.class), id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<FoodType> getAll() {
String sql = "select * from foodType";
try {
return JdbcUtils.getQuerrRunner().query(sql, new BeanListHandler<FoodType>(FoodType.class));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<FoodType> getAll(String typeName) {
String sql = "select * from foodType where typeName like ?";
try {
return JdbcUtils.getQuerrRunner()
.query(sql, new BeanListHandler<FoodType>(FoodType.class),"%" + typeName + "%");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void save(FoodType foodType) {
String sql = "INSERT INTO foodType(typeName) VALUES(?);";
try {
JdbcUtils.getQuerrRunner().update(sql,foodType.getTypeName());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void update(FoodType foodType) {
String sql = "update foodType set typeName=? where id=?";
try {
JdbcUtils.getQuerrRunner().update(sql, foodType.getTypeName(),foodType.getId());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package com.xp.service;
import java.util.List;
import com.xp.entity.FoodType;
public interface IFoodTypeService {
/**
* 添加
*/
void save(FoodType foodType);
/**
* 更新
*/
void update(FoodType foodType);
/**
* 删除
*/
void delete(int id);
/**
* 根据主键查询
*/
FoodType findById(int id);
/**
* 查询全部
*/
List<FoodType> getAll();
/**
* 根据菜系名称查询
*/
List<FoodType> getAll(String typeName);
}
package com.xp.factory;
import java.util.ResourceBundle;
/**
* 工厂:创建dao或service实例
*/
public class BeanFactory {
// 加载配置文件
private static ResourceBundle bundle;
static {
bundle = ResourceBundle.getBundle("instance");
}
/**
* 根据指定的key,读取配置文件获取类的全路径; 创建对象
* @return
*/
public static <T> T getInstance(String key,Class<T> clazz) {
String className = bundle.getString(key);
try {
return (T) Class.forName(className).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package com.xp.service.impl;
import java.util.List;
import com.xp.dao.IFoodTypeDao;
import com.xp.entity.FoodType;
import com.xp.factory.BeanFactory;
import com.xp.service.IFoodTypeService;
public class FoodTypeService implements IFoodTypeService {
//private IFoodTypeDao foodTypeDao = new FoodTypeDao();// 对象的创建,不能写死。
// 工厂创建对象
private IFoodTypeDao foodTypeDao = BeanFactory.getInstance("foodtypeDao", IFoodTypeDao.class);
@Override
public void save(FoodType foodType) {
try {
foodTypeDao.save(foodType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void update(FoodType foodType) {
try {
foodTypeDao.update(foodType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void delete(int id) {
try {
foodTypeDao.delete(id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public FoodType findById(int id) {
try {
return foodTypeDao.findById(id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<FoodType> getAll() {
try {
return foodTypeDao.getAll();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<FoodType> getAll(String typeName) {
try {
return foodTypeDao.getAll(typeName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Instance.properties
# dao instance
foodtypeDao = com.xp.dao.impl.FoodTypeDao
# service instance
foodTypeService =com.xp.service.impl.FoodTypeService
package com.xp.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xp.entity.FoodType;
import com.xp.factory.BeanFactory;
import com.xp.service.IFoodTypeService;
/**
* 4. 菜系管理Servlet开发
* a. 添加菜系
* b. 菜系列表展示
* c. 进入更新页面
* d. 删除
* e. 更新
*/
public class FoodTypeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 调用的菜系Service
private IFoodTypeService foodTypeService = BeanFactory.getInstance("foodTypeService",IFoodTypeService.class);
// 跳转资源
private Object uri;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取操作的类型
String method = request.getParameter("method");
// 判断
if ("addFoodType".equals(method)) {
// 添加
addFoodType(request, response);
}
else if ("list".equals(method)) {
// 列表展示
list(request, response);
}
else if ("viewUpdate".equals(method)) {
// 进入更新页面
viewUpdate(request, response);
}
else if ("delete".equals(method)) {
// 删除
delete(request, response);
}
else if ("update".equals(method)) {
// 更新
update(request, response);
}
}
//a. 添加菜系
public void addFoodType(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 1. 获取请求数据封装
String foodTypeName = request.getParameter("foodTypeName");
FoodType ft = new FoodType();
ft.setTypeName(foodTypeName);
// 2. 调用service处理业务逻辑
foodTypeService.save(ft);
// 3. 跳转
uri = request.getRequestDispatcher("/foodType?method=list");
} catch (Exception e) {
e.printStackTrace();
uri = "/error/error.jsp";
}
goTo(request, response, uri);
}
//b. 菜系列表展示
public void list(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 调用Service查询所有的类别
List<FoodType> list = foodTypeService.getAll();
// 保存
request.setAttribute("listFoodType", list);
// 跳转的菜系列表页面
uri = request.getRequestDispatcher("/sys/type/foodtype_list.jsp");
} catch (Exception e) {
e.printStackTrace();
uri = "/error/error.jsp";
}
// 跳转
goTo(request, response, uri);
}
//c. 进入更新页面
public void viewUpdate(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 1. 获取请求id
String id = request.getParameter("id");
// 2. 根据id查询对象
FoodType ft = foodTypeService.findById(Integer.parseInt(id));
// 3. 保存
request.setAttribute("foodType", ft);
// 4. 跳转
uri = request.getRequestDispatcher("/sys/type/foodtype_update.jsp");
} catch (Exception e) {
e.printStackTrace();
uri = "/error/error.jsp";
}
goTo(request, response, uri);
}
//d. 删除
public void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 1. 获取请求id
String id = request.getParameter("id");
// 2. 调用Service
foodTypeService.delete(Integer.parseInt(id));
// 3. 跳转
uri = "/foodType?method=list";
} catch (Exception e) {
e.printStackTrace();
uri = "/error/error.jsp";
}
goTo(request, response, uri);
}
// e. 更新
public void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//1. 获取请求数据封装
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("foodTypeName");
FoodType foodType = new FoodType();
foodType.setId(id);
foodType.setTypeName(name);
//2. 调用Service更新
foodTypeService.update(foodType);
//3. 跳转
//list(request,response);
uri = "/foodType?method=list";
} catch (Exception e) {
e.printStackTrace();
uri = "/error/error.jsp";
}
// 跳转
goTo(request, response, uri);
}
/**
* 跳转的通用方法
*/
private 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);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- 包含公共的JSP代码片段 -->
<title>无线点餐平台</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="${pageContext.request.contextPath }/sys/style/js/jquery.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/sys/style/js/page_common.js"></script>
<link href="${pageContext.request.contextPath }/sys/style/css/common_style_blue.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/sys/style/css/index_1.css" />
</head>
<body>
<!-- 页面标题 -->
<div id="TitleArea">
<div id="TitleArea_Head"></div>
<div id="TitleArea_Title">
<div id="TitleArea_Title_Content">
<img border="0" width="13" height="13"
src="${pageContext.request.contextPath }/sys/style/images/title_arrow.gif" /> 菜系列表
</div>
</div>
<div id="TitleArea_End"></div>
</div>
<!-- 过滤条件 -->
<div id="QueryArea">
<form action="/wirelessplatform/cuisine.html" method="get">
<input type="hidden" name="method" value="search">
<input type="text" name="keyword" title="请输入菜系名称">
<input type="submit" value="搜索">
</form>
</div>
<!-- 主内容区域(数据列表或表单显示) -->
<div id="MainArea">
<table class="MainArea_Content" align="center" cellspacing="0" cellpadding="0">
<!-- 表头-->
<thead>
<tr align="center" valign="middle" id="TableTitle">
<td>菜系编号</td>
<td>菜系名称</td>
<td>操作</td>
</tr>
</thead>
<!--显示数据列表 -->
<tbody id="TableData">
<c:choose>
<c:when test="${not empty requestScope.listFoodType}">
<c:forEach var="foodType" items="${requestScope.listFoodType}">
<tr>
<td>${foodType.id }</td>
<td>${foodType.typeName }</td>
<td>
<a href="${pageContext.request.contextPath }/foodType?id=${foodType.id}&method=viewUpdate" class="FunctionButton">更新</a>
<a href="${pageContext.request.contextPath }/foodType?id=${foodType.id}&method=delete" class="FunctionButton">删除</a>
</td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr>
<td colspan="3">没有你要找找的数据,请先保存记录再查看!</td>
</tr>
</c:otherwise>
</c:choose>
</tbody>
</table>
<!-- 其他功能超链接 -->
<div id="TableTail" align="center">
<div class="FunctionButton">
<a href="${pageContext.request.contextPath }/sys/type/foodtype_save.jsp">添加</a>
</div>
</div>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- 包含公共的JSP代码片段 -->
<title>无线点餐平台</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="${pageContext.request.contextPath }/sys/style/js/jquery.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/sys/style/js/page_common.js"></script>
<link href="${pageContext.request.contextPath }/sys/style/css/common_style_blue.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/sys/style/css/index_1.css" />
</head>
<body>
<!-- 页面标题 -->
<div id="TitleArea">
<div id="TitleArea_Head"></div>
<div id="TitleArea_Title">
<div id="TitleArea_Title_Content">
<img border="0" width="13" height="13" src="${pageContext.request.contextPath }/sys/style/images/title_arrow.gif"/> 添加菜系
</div>
</div>
<div id="TitleArea_End"></div>
</div>
<!-- 主内容区域(数据列表或表单显示) -->
<div id="MainArea">
<!-- 表单内容 -->
<form action="${pageContext.request.contextPath }/foodType?method=addFoodType" method="post">
<!-- 本段标题(分段标题) -->
<div class="ItemBlock_Title">
<img width="4" height="7" border="0" src="${pageContext.request.contextPath }/sys/style/images/item_point.gif"> 菜系信息
</div>
<!-- 本段表单字段 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<div class="ItemBlock2">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="80px">菜系名称</td>
<td>
<input type="text" name="foodTypeName" class="InputStyle" value=""/> *
</td>
</tr>
</table>
</div>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<input type="submit" value="添加" class="FunctionButtonInput">
<a href="javascript:history.go(-1);" class="FunctionButton">返回</a>
</div>
</form>
</div>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- 包含公共的JSP代码片段 -->
<title>无线点餐平台</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="${pageContext.request.contextPath }/sys/style/js/jquery.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/sys/style/js/page_common.js"></script>
<link href="${pageContext.request.contextPath }/sys/style/css/common_style_blue.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/sys/style/css/index_1.css" />
</head>
<body>
<!-- 页面标题 -->
<div id="TitleArea">
<div id="TitleArea_Head"></div>
<div id="TitleArea_Title">
<div id="TitleArea_Title_Content">
<img border="0" width="13" height="13" src="${pageContext.request.contextPath }/sys/style/images/title_arrow.gif"/> 更新菜系
</div>
</div>
<div id="TitleArea_End"></div>
</div>
<!-- 主内容区域(数据列表或表单显示) -->
<div id="MainArea">
<!-- 表单内容 -->
<form action="${pageContext.request.contextPath }/foodType?method=update" method="post">
<!-- 本段标题(分段标题) -->
<div class="ItemBlock_Title">
<img width="4" height="7" border="0" src="${pageContext.request.contextPath }/sys/style/images/item_point.gif"> 菜系信息
</div>
<!-- 本段表单字段 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<div class="ItemBlock2">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="80px">菜系名称</td>
<td>
<input type="text" name="foodTypeName" class="InputStyle" value="${foodType.typeName }"/> *
<input type="hidden" name="id" value="${foodType.id }"/>
</td>
</tr>
</table>
</div>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<input type="submit" value="修改" class="FunctionButtonInput">
<a href="javascript:history.go(-1);" class="FunctionButton">返回</a>
</div>
</form>
</div>
</body>
</html>