步骤 2 : 基本的CRUD 步骤 3 : 产品图片分类属性 步骤 4 : 非CRUD方法
ProductImageDAO用于建立对于ProductImage对象的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.List;
import tmall.bean.Product;
import tmall.bean.ProductImage;
import tmall.util.DBUtil;
public class ProductImageDAO {
public static final String type_single = "type_single";
public static final String type_detail = "type_detail";
public int getTotal() {
int total = 0;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select count(*) from ProductImage";
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
public void add(ProductImage bean) {
String sql = "insert into ProductImage values(null,?,?)";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, bean.getProduct().getId());
ps.setString(2, bean.getType());
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(ProductImage bean) {
}
public void delete(int id) {
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "delete from ProductImage where id = " + id;
s.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public ProductImage get(int id) {
ProductImage bean = new ProductImage();
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select * from ProductImage where id = " + id;
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
int pid = rs.getInt("pid");
String type = rs.getString("type");
Product product = new ProductDAO().get(pid);
bean.setProduct(product);
bean.setType(type);
bean.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bean;
}
public List<ProductImage> list(Product p, String type) {
return list(p, type,0, Short.MAX_VALUE);
}
public List<ProductImage> list(Product p, String type, int start, int count) {
List<ProductImage> beans = new ArrayList<ProductImage>();
String sql = "select * from ProductImage where pid =? and type =? order by id desc limit ?,? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, p.getId());
ps.setString(2, type);
ps.setInt(3, start);
ps.setInt(4, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
ProductImage bean = new ProductImage();
int id = rs.getInt(1);
bean.setProduct(p);
bean.setType(type);
bean.setId(id);
beans.add(bean);
}
} catch (SQLException e) {
e.printStackTrace();
}
return beans;
}
}
基本的CRUD操作与CategoryDAO的CRUD一样,在此不做赘述
两种静态属性分别表示单个图片和详情图片
public static final String type_single = "type_single"; public static final String type_detail = "type_detail";
除开CRUD之外,ProductImageDAO还提供了一些其他用于支持业务的方法
查询指定产品下,某种类型的ProductImage public List<ProductImage> list(Product p, String type) public List<ProductImage> list(Product p, String type, int start, int count) 注:部分非CRUD的业务方法,需要结合业务场景才能更好地理解,现在理解不透彻也很正常,在后续学习到相关场景的时候,再回过头来看,就明白了。
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|