java 自动填冲bean

package common.bean;

import java.util.*;
import java.sql.*;
import java.lang.reflect.*;
import org.apache.commons.beanutils.*;
import com.jspsmart.upload.*;
import javax.servlet.http.*;

/**
 * classname Bean
 * action 此类主要负责对Bean的一些操作,example:自动冲bean
 * @author 传说
 * @version 2007/7/25
 * */
public class Bean {

  /**
   * methodName popConvert
   * action 把rs冲到bean中,以list返回
   * scope public static
   * @param rs ResultSet
   * @param c Bean的反射对象
   * @return List
   * */
  public static List popConvert(ResultSet rs, Class c) throws Exception {
    ResultSetMetaData metaData = rs.getMetaData();
    int count = metaData.getColumnCount();
    List ret = new ArrayList();
    Field[] f = c.getDeclaredFields();
    Object n = null;
    while (rs.next()) {
      n = c.newInstance();
      for (int i = 1; i <= count; i++) {
        Object v = rs.getObject(i);
        for (int j = 0; j < f.length; j++) {
          Field f1 = f[j];
          if (f1.getName().equalsIgnoreCase(metaData.getColumnName(i))) {
            BeanUtils.copyProperty(n, f1.getName(), v);
            break;
          }
        }
      }
      ret.add(n);
    }
    return ret;
  }

  /**
   * methodName popConvert
   * action 把rs冲到bean中,以list返回
   * scope public static
   * @param req Request
   * @param c Bean的反射对象
   * @return Object
   * */
  public static Object popConvert(Request req, Class c) throws Exception {
    Enumeration enumObj = req.getParameterNames();
    List list = Collections.list(enumObj);
    int count = list.size();
    Field[] f = c.getDeclaredFields();
    Object n = null;
    n = c.newInstance();
    for (int i = 0; i < count; i++) {
      Object v = list.get(i);
      for (int j = 0; j < f.length; j++) {
        Field f1 = f[j];
        if (f1.getName().equalsIgnoreCase(v.toString())) {
          BeanUtils.copyProperty(n, f1.getName(), req.getParameter(v.toString()));
          break;
        }
      }
    }
    return n;
  }

  /**
   * methodName popConvert
   * action 把rs冲到bean中,以list返回
   * scope public static
   * @param req HttpServletRequest
   * @param c Bean的反射对象
   * @return Object
   * */
  public static Object popConvert(HttpServletRequest req, Class c) throws
      Exception {
    Enumeration enumObj = req.getParameterNames();
    List list = Collections.list(enumObj);
    int count = list.size();
    Field[] f = c.getDeclaredFields();
    Object n = null;
    n = c.newInstance();
    for (int i = 0; i < count; i++) {
      Object v = list.get(i);
      for (int j = 0; j < f.length; j++) {
        Field f1 = f[j];
        if (f1.getName().equalsIgnoreCase(v.toString())) {
          BeanUtils.copyProperty(n, f1.getName(),common.webclass.RequestInformation.getValueOfParamWithString(req,v.toString()));
          break;
        }
      }
    }
    return n;
  }

  /**
   * methodName popConvert
   * action 把rs冲到bean中,以list返回
   * scope public static
   * @param rs ResultSet
   * @param c Bean的反射对象
   * @param replaceValue 因为在middlegen生成bean的时候把_都删除掉了,所以这里
   * 增加了替换字符 这里的replaceValue是正则表达式
   * @return List
   * */
  public static List popConvert(ResultSet rs, Class c, String replaceValue) throws
      Exception {
    ResultSetMetaData metaData = rs.getMetaData();
    int count = metaData.getColumnCount();
    List ret = new ArrayList();
    Field[] f = c.getDeclaredFields();
    Object n = null;
    while (rs.next()) {
      n = c.newInstance();
      for (int i = 1; i <= count; i++) {
        Object v = rs.getObject(i);
        for (int j = 0; j < f.length; j++) {
          Field f1 = f[j];
          if (f1.getName().equalsIgnoreCase(metaData.getColumnName(i).
                                            replaceAll(replaceValue, ""))) {
            BeanUtils.copyProperty(n, f1.getName(), v);
            break;
          }
        }
      }
      ret.add(n);
    }
    return ret;
  }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值