how2j.cn

步骤 1 : ProductDAO.java   
步骤 2 : 基本的CRUD操作   
步骤 3 : 非CRUD方法   

ProductDAO用于建立对于Product对象的ORM映射
package tmall.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Date; import java.util.List; import tmall.bean.Category; import tmall.bean.Product; import tmall.bean.ProductImage; import tmall.util.DBUtil; import tmall.util.DateUtil; public class ProductDAO { public int getTotal(int cid) { int total = 0; try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) { String sql = "select count(*) from Product where cid = " + cid; ResultSet rs = s.executeQuery(sql); while (rs.next()) { total = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return total; } public void add(Product bean) { String sql = "insert into Product values(null,?,?,?,?,?,?,?)"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setString(1, bean.getName()); ps.setString(2, bean.getSubTitle()); ps.setFloat(3, bean.getOrignalPrice()); ps.setFloat(4, bean.getPromotePrice()); ps.setInt(5, bean.getStock()); ps.setInt(6, bean.getCategory().getId()); ps.setTimestamp(7, DateUtil.d2t(bean.getCreateDate())); ps.execute(); ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { int id = rs.getInt(1); bean.setId(id); } } catch (SQLException e) { e.printStackTrace(); } } public void update(Product bean) { String sql = "update Product set name= ?, subTitle=?, orignalPrice=?,promotePrice=?,stock=?, cid = ?, createDate=? where id = ?"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setString(1, bean.getName()); ps.setString(2, bean.getSubTitle()); ps.setFloat(3, bean.getOrignalPrice()); ps.setFloat(4, bean.getPromotePrice()); ps.setInt(5, bean.getStock()); ps.setInt(6, bean.getCategory().getId()); ps.setTimestamp(7, DateUtil.d2t(bean.getCreateDate())); ps.setInt(8, bean.getId()); ps.execute(); } catch (SQLException e) { e.printStackTrace(); } } public void delete(int id) { try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) { String sql = "delete from Product where id = " + id; s.execute(sql); } catch (SQLException e) { e.printStackTrace(); } } public Product get(int id) { Product bean = new Product(); try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) { String sql = "select * from Product where id = " + id; ResultSet rs = s.executeQuery(sql); if (rs.next()) { String name = rs.getString("name"); String subTitle = rs.getString("subTitle"); float orignalPrice = rs.getFloat("orignalPrice"); float promotePrice = rs.getFloat("promotePrice"); int stock = rs.getInt("stock"); int cid = rs.getInt("cid"); Date createDate = DateUtil.t2d( rs.getTimestamp("createDate")); bean.setName(name); bean.setSubTitle(subTitle); bean.setOrignalPrice(orignalPrice); bean.setPromotePrice(promotePrice); bean.setStock(stock); Category category = new CategoryDAO().get(cid); bean.setCategory(category); bean.setCreateDate(createDate); bean.setId(id); setFirstProductImage(bean); } } catch (SQLException e) { e.printStackTrace(); } return bean; } public List<Product> list(int cid) { return list(cid,0, Short.MAX_VALUE); } public List<Product> list(int cid, int start, int count) { List<Product> beans = new ArrayList<Product>(); Category category = new CategoryDAO().get(cid); String sql = "select * from Product where cid = ? order by id desc limit ?,? "; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, cid); ps.setInt(2, start); ps.setInt(3, count); ResultSet rs = ps.executeQuery(); while (rs.next()) { Product bean = new Product(); int id = rs.getInt(1); String name = rs.getString("name"); String subTitle = rs.getString("subTitle"); float orignalPrice = rs.getFloat("orignalPrice"); float promotePrice = rs.getFloat("promotePrice"); int stock = rs.getInt("stock"); Date createDate = DateUtil.t2d( rs.getTimestamp("createDate")); bean.setName(name); bean.setSubTitle(subTitle); bean.setOrignalPrice(orignalPrice); bean.setPromotePrice(promotePrice); bean.setStock(stock); bean.setCreateDate(createDate); bean.setId(id); bean.setCategory(category); setFirstProductImage(bean); beans.add(bean); } } catch (SQLException e) { e.printStackTrace(); } return beans; } public List<Product> list() { return list(0,Short.MAX_VALUE); } public List<Product> list(int start, int count) { List<Product> beans = new ArrayList<Product>(); String sql = "select * from Product limit ?,? "; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, start); ps.setInt(2, count); ResultSet rs = ps.executeQuery(); while (rs.next()) { Product bean = new Product(); int id = rs.getInt(1); int cid = rs.getInt("cid"); String name = rs.getString("name"); String subTitle = rs.getString("subTitle"); float orignalPrice = rs.getFloat("orignalPrice"); float promotePrice = rs.getFloat("promotePrice"); int stock = rs.getInt("stock"); Date createDate = DateUtil.t2d( rs.getTimestamp("createDate")); bean.setName(name); bean.setSubTitle(subTitle); bean.setOrignalPrice(orignalPrice); bean.setPromotePrice(promotePrice); bean.setStock(stock); bean.setCreateDate(createDate); bean.setId(id); Category category = new CategoryDAO().get(cid); bean.setCategory(category); beans.add(bean); } } catch (SQLException e) { e.printStackTrace(); } return beans; } public void fill(List<Category> cs) { for (Category c : cs) fill(c); } public void fill(Category c) { List<Product> ps = this.list(c.getId()); c.setProducts(ps); } public void fillByRow(List<Category> cs) { int productNumberEachRow = 8; for (Category c : cs) { List<Product> products = c.getProducts(); List<List<Product>> productsByRow = new ArrayList<>(); for (int i = 0; i < products.size(); i+=productNumberEachRow) { int size = i+productNumberEachRow; size= size>products.size()?products.size():size; List<Product> productsOfEachRow =products.subList(i, size); productsByRow.add(productsOfEachRow); } c.setProductsByRow(productsByRow); } } public void setFirstProductImage(Product p) { List<ProductImage> pis= new ProductImageDAO().list(p, ProductImageDAO.type_single); if(!pis.isEmpty()) p.setFirstProductImage(pis.get(0)); } public void setSaleAndReviewNumber(Product p) { int saleCount = new OrderItemDAO().getSaleCount(p.getId()); p.setSaleCount(saleCount); int reviewCount = new ReviewDAO().getCount(p.getId()); p.setReviewCount(reviewCount); } public void setSaleAndReviewNumber(List<Product> products) { for (Product p : products) { setSaleAndReviewNumber(p); } } public List<Product> search(String keyword, int start, int count) { List<Product> beans = new ArrayList<Product>(); if(null==keyword||0==keyword.trim().length()) return beans; String sql = "select * from Product where name like ? limit ?,? "; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setString(1, "%"+keyword.trim()+"%"); ps.setInt(2, start); ps.setInt(3, count); ResultSet rs = ps.executeQuery(); while (rs.next()) { Product bean = new Product(); int id = rs.getInt(1); int cid = rs.getInt("cid"); String name = rs.getString("name"); String subTitle = rs.getString("subTitle"); float orignalPrice = rs.getFloat("orignalPrice"); float promotePrice = rs.getFloat("promotePrice"); int stock = rs.getInt("stock"); Date createDate = DateUtil.t2d( rs.getTimestamp("createDate")); bean.setName(name); bean.setSubTitle(subTitle); bean.setOrignalPrice(orignalPrice); bean.setPromotePrice(promotePrice); bean.setStock(stock); bean.setCreateDate(createDate); bean.setId(id); Category category = new CategoryDAO().get(cid); bean.setCategory(category); setFirstProductImage(bean); beans.add(bean); } } catch (SQLException e) { e.printStackTrace(); } return beans; } }
步骤 2 :

基本的CRUD操作

edit
基本的CRUD操作与CategoryDAO的CRUD一样,在此不做赘述
除开CRUD之外,ProductDAO还提供了一些其他用于支持业务的方法
查询分类下的产品

public List<Product> list(int cid)
public List<Product> list(int cid, int start, int count)


获取某种分类下的产品数量

public int getTotal(int cid)


为分类填充产品集合

public void fill(Category c)
public void fill(List<Category> cs)


为多个分类设置productsByRow属性

public void fillByRow(List<Category> cs)

这个fillByRow方法问的同学比较多,详细讲一讲:
假设一个分类恰好对应40种产品,那么这40种产品本来是放在一个集合List里。 可是,在页面上显示的时候,需要每8种产品,放在一列 为了显示的方便,我把这40种产品,按照每8种产品方在一个集合里的方式,拆分成了5个小的集合,这5个小的集合里的每个元素是8个产品。 这样到了页面上,显示起来就很方便了。 否则页面上的处理就会复杂不少。


根据关键字查询产品

public List<Product> search(String keyword, int start, int count)


一个产品有多个图片,但是只有一个主图片,把第一个图片设置为主图片

public void setFirstProductImage(Product p)


为产品设置销售和评价数量

public void setSaleAndReviewNumber(List<Product> products)
public void setSaleAndReviewNumber(Product p)


注:部分非CRUD的业务方法,需要结合业务场景才能更好地理解,现在理解不透彻也很正常,在后续学习到相关场景的时候,再回过头来看,就明白了。


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 回收站-删除掉的-ProductDAO 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图