BeanUtils.populate( Object bean, Map properties )
先遍历map<k,v>中的k 如果k和bean相同 就将v的值赋给bean
补充:map中的数据映射到JavaBean中的get和set方法中(封装数据到JavaBean中)。之后取值就直接从JavaBean中的get和set方法中取值就可以了。
使用BeanUtils.populate(info,map)方法将页面各个属性映射到bean中 我们就可以这样使用bean.getXxxx() bean.setXxxx()。
就如结合下面的例子:BeanUtils.populate(item, map);及时将map中的值映射到item中的getXxx setXxx方法中,因而就对map中的数据封装到了orderItem中去l
例子:
//获得每一个订单的oid
String oid= order.getOid();
//查询该订单的所有订单项--maplist封装的是多个订单项和订单项中的商品信息
List<Map<String, Object>> maplist=service.findAllOrderItemByOid(oid);
//将maplist转换为list<OrderItem> orderItems
for(Map<String,Object> map:maplist){
try {
//从map中取出count subtotal 封装到OrderItem
orderItem item = new orderItem();
// item.setCount(Integer.parseInt(map.get("count").toString()));
BeanUtils.populate(item, map);
//从map中取出pimage pname shop_price 封装到Product中
Product product=new Product();
BeanUtils.populate(product, map);
//将product封装到OrderItem
item.setProduct(product);
//将orderItem封装到order中的orderItemList中
order.getOrderItems().add(item);