62,628
社区成员
发帖
与我相关
我的任务
分享
public class Dishe {
private String did;
private String dname;
private double price;
private Category category;
}
public class Category {
private String cid;
private String cname;
}
public List<Dishe> findByCategoryList(String cid) {
String sql = "SELECT * FROM dishes WHERE cid=?";
try {
List<Dishe> disheList = qr.query(sql, new BeanListHandler<Dishe>(
Dishe.class), cid);
for (Dishe dishe : disheList) {
loadCategory(dishe);
System.out.println(dishe);
}
return disheList;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private void loadCategory(Dishe dishe) {
String sql = "SELECT * FROM dishes d,category c WHERE d.cid=c.cid AND did=?";
try {
Map<String, Object> map = qr.query(sql, new MapHandler(),
dishe.getDid());
toCategory(map);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private void toCategory(Map<String, Object> map) {
Dishe dishe = CommonUtils.toBean(map, Dishe.class); //传个map转换为对象
Category category = CommonUtils.toBean(map, Category.class);
dishe.setCategory(category);
}
public List<Dishe> findByCategoryList(String cid) {
String sql = "SELECT * FROM dishes WHERE cid=?";
try {
List<Dishe> disheList = qr.query(sql, new BeanListHandler<Dishe>(
Dishe.class), cid);
for (Dishe dishe : disheList) {
loadCategory(dishe);
System.out.println(dishe);
}
return disheList;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private void loadCategory(Dishe dishe) {
String sql = "SELECT * FROM dishes d,category c WHERE d.cid=c.cid AND did=?";
try {
Map<String, Object> map = qr.query(sql, new MapHandler(),
dishe.getDid());
dishe.setCategory( toCategory(map) );
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private void toCategory(Map<String, Object> map) {
return CommonUtils.toBean(map, Category.class);
}