zoukankan      html  css  js  c++  java
  • 商城后台管理系统(一)

    后台商品列表展示

    1.功能截图

    2.代码实现

    web层
    package com.itheima.web;
    
    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.itheima.domain.Product;
    import com.itheima.service.AdminProductListService;
    
    public class AdminProductListServlet extends HttpServlet {
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		request.setCharacterEncoding("UTF-8");
    		response.setContentType("text/html; charset=UTF-8");
    		AdminProductListService service = new AdminProductListService();
    		List<Product> productList = null;
    		try {
    			productList = service.findAllProduct();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		request.setAttribute("productList", productList);
    		request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);
    	}
    
    	protected void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		doGet(request, response);
    	}
    
    }
    
     service层 
    package com.itheima.service;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import com.itheima.dao.AdminProductDao;
    import com.itheima.domain.Category;
    import com.itheima.domain.Product;
    
    public class AdminProductListService {
    	//商品列表
    	public List<Product> findAllProduct() throws SQLException {
    		AdminProductDao dao = new AdminProductDao();
    		List<Product> productList = dao.findAllProduct();
    		return productList;
    	}
    	//增加商品
    	public void addProduct(Product product) throws SQLException {
    		AdminProductDao dao = new AdminProductDao();
    		dao.addProduct(product);
    		
    	}
    	//删除商品
    	public void delProduct(String pid) throws SQLException {
    		AdminProductDao dao = new AdminProductDao();
    		dao.delProduct(pid);
    		
    	}
    	//商品种类
    	public List<Category> findAllCategory() throws SQLException {
    		AdminProductDao dao = new AdminProductDao();
    		List<Category> categoryList = dao.findAllCategory();
    		return categoryList;
    	}
    	
    	public Product findProductById(String pid) throws SQLException {
    		AdminProductDao dao = new AdminProductDao();
    		Product product = dao.findProductById(pid);
    		return product;
    	}
    
    }
    

     dao层

    package com.itheima.dao;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanHandler;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    
    import com.itheima.domain.Category;
    import com.itheima.domain.Product;
    import com.itheima.utils.DataSourceUtils;
    
    public class AdminProductDao {
    	//商品列表
    	public List<Product> findAllProduct() throws SQLException {
    		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    		String sql = "select * from product";
    		return qr.query(sql, new BeanListHandler<Product>(Product.class));
    	}
    	//增加商品
    	public void addProduct(Product product) throws SQLException {
    		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    		String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)";
    		qr.update(sql, product.getPid(),product.getPname(),product.getMarket_price(),product.getShop_price(),
    				product.getPimage(),product.getPdate(),product.getIs_hot(),product.getPdesc(),product.getPflag(),
    				product.getCid());
    		
    	}
    	//删除商品
    	public void delProduct(String pid) throws SQLException {
    		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    		String sql = "delete from product where pid = ?";
    		qr.update(sql, pid);
    		
    	}
    	//查询商品种类
    	public List<Category> findAllCategory() throws SQLException {
    		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    		String sql = "select * from category";
    		List<Category> categoryList = qr.query(sql, new BeanListHandler<Category>(Category.class));
    		return categoryList;
    	}
    	public Product findProductById(String pid) throws SQLException {
    		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
    		String sql="select * from product where pid = ?";
    		Product product = qr.query(sql, new BeanHandler<Product>(Product.class),pid);		
    		return product;
    	}
    
    }
    

      

     这里将后续几种功能的service层和dao层的代码实现都存放在一起

  • 相关阅读:
    AppWidget应用(四)---PendingIntent 之 getService
    AppWidget应用(三)---PendingIntent 之 getBroadcast
    AppWidget应用(二)---PendingIntent 之 getActivity
    JZOJ 3231. 海明距离
    JZOJ 1422. 猴子摘桃
    JZOJ 1421. 二叉树
    SSLOJ 1318.地铁重组
    SSLOJ 1319.埃雷萨拉斯寻宝
    SSLOJ 1317.灵魂分流药剂
    SSLOJ 1316.血色先锋军
  • 原文地址:https://www.cnblogs.com/TaoLeonis/p/6849759.html
Copyright © 2011-2022 走看看