javaBean属性为对象时赋值问题

lazy_yy 2018-03-27 11:05:05

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);
}


我通过findByCategoryList() 这个方法查询返回一个List<Dishe>集合, 但是Dishe里面的Category对象为空 我测试的时候分开调用这三个方法,Category的有值的对象就可以set到Dishe里 没发现问题在哪,求解决。。。
...全文
662 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wcwtitxu 2018-03-28
  • 打赏
  • 举报
回复
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);
}
  • 打赏
  • 举报
回复
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()); Category category = CommonUtils.toBean(map, Category.class); dishe.setCategory(category); } catch (SQLException e) { throw new RuntimeException(e); } } 试试这样吧。你已经中间把dishe对象换过了,,,,保存到别的对象里了。。。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧