package com.jwy.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.jwy.dao.Commodity;
import com.jwy.dao.CommodityDao;
/**
* @author Jingweiyu
* @function 跟据参数调用操作数据库的方法
* @datatime 2009-4-21 上午11:02:37
*/
public class CommodityServlet extends HttpServlet {
private CommodityDao commodityDao;
/**
* 此servlet被初始化的时候执行的方法 从Spring容器中获取commodityDao对象
**/
public void init() throws ServletException {
WebApplicationContext webContext = WebApplicationContextUtils
.getRequiredWebApplicationContext(getServletContext());
commodityDao = (CommodityDao) webContext.getBean("commodityDao");
}
/*
* (non-Javadoc)
*
* @see javax.servlet.GenericServlet#destroy()
*/
public void destroy() {
super.destroy();
}
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
* , javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
* , javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int action = Integer.parseInt(request.getParameter("action"));
switch (action) {
case 1:
doFindAll(request, response); // 查找全部记录
break;
case 2:
doInsert(request, response); // 插入记录
break;
case 3:
doUpdate(request, response); // 更新记录
break;
case 4:
doDelete(request, response); // 删除记录
break;
case 5:
doFindById(request, response); // 按照主键id查找记录
break;
}
}
/**
* 向数据表中插入记录
*
* @param request
* @param response
* @throws Exception
* @throws IOException
*/
public void doInsert(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Commodity commodity = new Commodity();
commodity.setName(request.getParameter("name"));
commodity.setPrice(Double.valueOf(request.getParameter("price")));
commodity.setCount(Integer.valueOf(request.getParameter("count")));
commodity.setRemark(request.getParameter("remark"));
commodity.setId(0);
commodityDao.insert(commodity);
doFindAll(request, response);
}
/**
* 跟据id编号更新表中数据
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void doUpdate(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Commodity commodity = new Commodity();
commodity.setId(Integer.valueOf(request.getParameter("id")));
commodity.setName(request.getParameter("name"));
commodity.setPrice(Double.valueOf(request.getParameter("price")));
commodity.setCount(Integer.valueOf(request.getParameter("count")));
commodity.setRemark(request.getParameter("remark"));
commodityDao.update(commodity);
doFindAll(request, response);
}
/**
* 跟据id编号删除表中数据
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
commodityDao.delete(Integer.valueOf(request.getParameter("id")));
doFindAll(request, response);
}
/**
* 跟据id编号查询对象,保存在request对象的commodity属性中
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void doFindById(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Commodity commodity = commodityDao.findById(Integer.valueOf(request
.getParameter("id")));
request.setAttribute("commodity", commodity);
request.getRequestDispatcher("../update.jsp")
.forward(request, response);
}
/**
* 查询出数据表中所有的记录,保存在request对象的list属性中
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void doFindAll(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List list = commodityDao.findAll();
request.setAttribute("list", list);
request.getRequestDispatcher("../show.jsp").forward(request, response);
return;
}
}
Spring整合Hibernate操作商品库存表
5星 · 超过95%的资源 需积分: 10 133 浏览量
2013-05-14
14:10:28
上传
评论
收藏 8.64MB RAR 举报
Leoyoe~
- 粉丝: 5
- 资源: 60
最新资源
- Springboot开发-将springboot与mongodb数据库相结合.zip
- 基于深度学习图像分割Unet的肿瘤区域识别辅助诊断系统python源码(高分项目).zip
- 基于ssm+jsp的母婴用品网站+数据库(毕业设计,包括源码,教程).zip
- Android简单的页面启动器,不用再使用onActivityResult来取得返回值 Kotlin + androidx 实现
- 基于深度学习图像分割Unet的肿瘤区域识别辅助诊断系统python源码(含模型构建、后端架设和前端访问功能).zip
- 各种fortran代码
- Springboot开发-使用springboot实现发邮件功能.zip
- Java SSM 生鲜果蔬电商系统【优质毕业设计分享】.rar
- 20、Java SSM 生鲜果蔬电商系统【优质毕业设计分享】.rar
- Python桌面应用开发图书信息管理系统 应用Python开发,基于最新的QT6桌面开发 用于本科专科阶段毕设模板
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈