• 商城后台管理系统(一)


    后台商品列表展示

    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层的代码实现都存放在一起

  • 相关阅读:
    SPI Flash(W25Q16DV) 驱动
    SPI Flash(W25Q16DV) 基本操作
    SPI OLED 驱动
    dategrip破解
    mysql中find_in_set()函数的使用
    MYSQL中,CAST函数的使用规则
    关于Java对象作为参数传递是传值还是传引用的问题
    easyPOI导出excel报错
    ### Cause: java.lang.reflect.UndeclaredThrowableException
    用count(*)还是count(列名) || Mysql中的count()与sum()区别
  • 原文地址:https://www.cnblogs.com/TaoLeonis/p/6849759.html
Copyright © 2020-2023  润新知