javaWeb项目2_网上商城_day6

一:商品添加页面的类别ajax显示

	<script>
		$(function(){
			$.post(
			"${pageContext.request.contextPath}/adminfindAllCategoryServlet",
			{},
			function(data){
				var content="";
				for(var i=0;i<data.length;i++){
					content+="<option value='"+data[i].cid+"'>"+data[i].cname+"</option> ";
				}
				$("#cid").html(content);
			},
			"json"
			);
		});
	</script>



public class AdminfindAllCategoryServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminProductService service=new AdminProductService();
		List<Category> categoryList=service.findAllCategory();
		Gson gson=new Gson();
		String json=gson.toJson(categoryList);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().write(json);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}


public class AdminProductService {
	public List<Category> findAllCategory() {
		AdminProductDao dao=new AdminProductDao();
		 List<Category> categoryList=null;
		try {
			categoryList=dao.findAllCategory();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return categoryList;
	}

}


public class AdminProductDao {

	public List<Category> findAllCategory() throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select * from category";
		return runner.query(sql, new BeanListHandler<Category>(Category.class));
	}
}

二:商品添加

public class AdminAddProductServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//目的:收集表单的数据 封装一个Product实体 将上传图片存到服务器磁盘上
		Product product=new Product();
		//收集数据的容器
		Map<String,Object> map=new HashMap<>();
		try {
			//创建磁盘文件项工厂
			DiskFileItemFactory factory=new DiskFileItemFactory();
			//创建文件上传核心对象
			ServletFileUpload upload=new ServletFileUpload(factory);
			upload.setHeaderEncoding("utf-8");
			//解析request获得文件项对象集合
			List<FileItem> parseRequest = upload.parseRequest(request);
			 for (FileItem item : parseRequest) {
				//判断是否是普通表单项
				boolean formField = item.isFormField();
				if(formField){
					//普通表单项 获得表单的数据 封装到Product实体中
					String fieldName = item.getFieldName();
					String fieldValue = item.getString("UTF-8");
					map.put(fieldName, fieldValue);
				}else{
					//文件上传项 获得文件名称 获得文件的内容
					String fileName=item.getName();
					InputStream in = item.getInputStream();
					String path=this.getServletContext().getRealPath("upload");
					OutputStream out=new FileOutputStream(path+"/"+fileName);
					IOUtils.copy(in, out);
					item.delete();
					map.put("pimage", "upload/"+fileName);
				}
			}
			 BeanUtils.populate(product, map);
				//是否product对象封装数据完全
				//private String pid;
				product.setPid(CommUtils.getUUID());
				//private Date pdate;
				product.setPdate(new Date());
				//private int pflag;
				product.setPflag(0);
				//private Category category;
				Category category = new Category();
				category.setCid(map.get("cid").toString());
				product.setCategory(category);
				//将product传递给service
				AdminProductService service=new AdminProductService();
				service.addProduct(product);
				//添加成功跳转到商品的Servlet省略了
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}


public class AdminProductService {
public void addProduct(Product product) {
		AdminProductDao dao=new AdminProductDao();
		try {
			dao.addProduct(product);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}


public class AdminProductDao {
public void addProduct(Product product) throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)";
		System.out.println(product.getCategory().getCid()+"==========");
		runner.update(sql, product.getPid(),product.getPname(),product.getMarket_price(),
				product.getShop_price(),product.getPimage(),product.getPdate(),			product.getIs_hot(),product.getPdesc(),product.getPflag(),product.getCategory().getCid());
	}

}

三:全部订单查询

public class AdminfindAllOrders extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminProductService servcie=new AdminProductService();
		List<Order> orderList=servcie.findAllOrders();
		request.setAttribute("orderList", orderList);
		request.getRequestDispatcher("/admin/order/list.jsp").forward(request, response);
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}


public class AdminProductService {
public List<Order> findAllOrders() {
		AdminProductDao dao=new AdminProductDao();
		List<Order> orderList=null;
		try {
			orderList=dao.findAllOrders();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return orderList;
	}

}

public class AdminProductDao {
	public List<Order> findAllOrders() throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from orders";
		return runner.query(sql, new BeanListHandler<Order>(Order.class));
	}

}

四:后台查询订单详情

public class FindOrderInfoByOidServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminProductService service=new AdminProductService();
		String oid=request.getParameter("oid");
		List<Map<String,Object>> mapList=service.findOrderInfoByOid(oid);
		Gson gson=new Gson();
		String json=gson.toJson(mapList);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(json);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}


public class AdminProductService {
public List<Map<String, Object>> findOrderInfoByOid(String oid) {
		AdminProductDao dao=new AdminProductDao();
		List<Map<String, Object>> mapList=null;
		try {
			mapList = dao.findOrderInfoByOid(oid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mapList;
	}

}

public class AdminProductDao {
public List<Map<String, Object>> findOrderInfoByOid(String oid) throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select p.pimage,p.pname,p.shop_price,o.count,o.subtotal  from orderitem o,product p where o.pid=p.pid and o.oid=?";
		List<Map<String, Object>> mapList=runner.query(sql, new MapListHandler(),oid);
		return mapList;
	}

}

五:页面实现查询订单详情

六:模拟数据加载比较慢

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

guoyebing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值