大家好,欢迎来到IT知识分享网。
后台商品列表展示
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层的代码实现都存放在一起
转载于:https://www.cnblogs.com/TaoLeonis/p/6849759.html
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/12241.html