JDBC:如何将resultset的信息自动封装到pojo里面

不多说了  直接上代码 

Java代码   收藏代码
  1. public static List resultSetToList(ResultSet rs) throws java.sql.SQLException{  
  2.        if(rs==null){  
  3.            return null;  
  4.        }  
  5.   
  6.        ResultSetMetaData md = rs.getMetaData();  
  7.        int columnCount = md.getColumnCount();  
  8.   
  9.        List list = new ArrayList();  
  10.        Map rowData;  
  11.        while (rs.next()){  
  12.            rowData = new HashMap(columnCount);  
  13.            for (int i=1; i<=columnCount; i++){  
  14.                  
  15.                    rowData.put(md.getColumnName(i),rs.getObject(i));  
  16.                  
  17.                 
  18.            }  
  19.            list.add(rowData);  
  20.        }  
  21.        return list;  
  22.    }  

首先将ResultSet封装成list  而每条记录对应一个实体Map 
Java代码   收藏代码
  1. import java.lang.reflect.InvocationTargetException;  
  2. import java.lang.reflect.Method;  
  3. import java.util.HashMap;  
  4. import java.util.Iterator;  
  5. import java.util.Map;  
  6. /** 
  7.  * <p>Title:属性封装类</p> 
  8.  * 
  9.  * <p>Description: </p> 
  10.  * 
  11.  * <p>Copyright: Copyright (c) 2008</p> 
  12.  * 
  13.  */  
  14. public class BeanUtils {      
  15.     /** 
  16.      *  
  17.      * @param bean 需要封装的vo 
  18.      * @param map 需要转换的map 
  19.      * @return 已经封装好数据的vo(object) 
  20.      */  
  21.     public static  Object MapToBean(Object bean, Map map) {  
  22.         Map methods = new HashMap();  
  23.         Method m[] = bean.getClass().getMethods();  
  24.         for (int i = 0; i < m.length; i++) {  
  25.             Method method = m[i];  
  26.             String methodName = method.getName().toUpperCase();  
  27.             methods.put(methodName, method);  
  28.         }  
  29.   
  30.         Iterator it = null;  
  31.         String key = "";  
  32.         it = map.keySet().iterator();  
  33.         while (it.hasNext()) {  
  34.             key = (String) it.next();  
  35.             String name = "GET" + key.toUpperCase();  
  36.             if (methods.containsKey(name)) {  
  37.                 Method setMethod = (Method) methods.get("SET" + key.toUpperCase());  
  38.                 try {  
  39.                     if(setMethod!=null){  
  40.                         Object[] obj=null;  
  41.                         obj=new Object[1];  
  42.                         obj[0]=map.get(key);  
  43.                     setMethod.invoke(bean, obj);  
  44.                     }  
  45.                     else{  
  46.                         continue;  
  47.                     }  
  48.                 } catch (IllegalAccessException e) {  
  49.                     e.printStackTrace();  
  50.                 } catch (InvocationTargetException e) {  
  51.                     e.printStackTrace();  
  52.                 }  
  53.   
  54.             }  
  55.         }  
  56.         return bean;  
  57.     }  
  58. }  


此方法是将上面查到的list作为参数  然后再将其list里面的map转换成相对性的pojo 

假设查询的是student表  与之对应的pojo是Student类  那么调用方法如下: 

首先查询数据  获取到resultset  
然后 
List  retultList=resultSetToList(resultset); 
遍历retultList  将list里面的Map都转换成pojo 
Student stu=(Student)BeanUtils.MapToBean(new Student(),(Map)retultList.get(0)); 

注意:查询出来的字段名要和pojo中的属性名相同  若查询出age字段 pojo中需有getAge和setAge方法  方法名大小写不限 

 

转自:http://hfkiss44.iteye.com/blog/568525

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值