汎型BaseDAO


属性の多いbeanに対しては、ヒベルナやibatisなどの恒久化解決策を使わずに、照会関数を書くときに、単にResultSetをObjectに変換するのは苦痛なことですので、java反射機でBaseDaoを作って、ResultSetをObjectに写像する操作をパッケージ化し、DAOの開発を簡略化しました.
本文は[url]を参照してくださいhttp://blog.csdn.net/cping1982/archive/2007/10/01/1808647.aspx
[/url]を少し改善しました.
public static String[][] proterty2Column = 
	    {{"member_id","memberId"},		
		{"name", "name"}};
	/**
	 * change the first char to upper case
	 * @param s
	 * @return
	 */
	private static String upperFirstChar(String s){
		char [] chars = s.toCharArray();
		chars[0] = Character.toUpperCase(chars[0]);
		return new String(chars);
	}
	/**
* ResultSet     Object
	 * @param clzz
	 * @param rs 
	 * @param obj
	 * @param fieldName
	 * @param varibleName
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SQLException
	 * @throws NoSuchFieldException 
	 * @throws SecurityException 
	 */
    private void  mapResultSet2Object(ResultSet rs, Object obj, String propertyName,
 String columName) 
throws NoSuchMethodException, IllegalAccessException, 
InvocationTargetException, SQLException, 
SecurityException, NoSuchFieldException {
    	 Class < ? extends Object >clazz = obj.getClass();
         Class < ? extends Object >propertyType = clazz.getDeclaredField(propertyName).
getType();
       Method method = clazz.getDeclaredMethod("set"+ 
     upperFirstChar( propertyName ), new Class[] { propertyType });
       if (propertyType == String.class)
           method.invoke(obj, (Object [])new String[] 
{rs.getString( columName ) });
       else if (propertyType == int.class)
           method.invoke(obj, (Object [])new Integer[] 
{new Integer( rs.getInt( columName )) });
       else if (propertyType == float.class)
           method.invoke(obj, (Object [])new Float[] 
{new Float( rs.getFloat( columName )) });
       else if (propertyType == long.class)
           method.invoke(obj, (Object [])new Long[]
 {new Long( rs.getLong( columName )) });
       else if (propertyType == double.class)
           method.invoke(obj, (Object [])new Double[] 
{new Double( rs.getDouble( columName )) });
       else if(propertyType == Date.class){
    	   method.invoke(obj, (Object [])new Date[] 
{rs.getDate(columName)});
       }
   }
    /**
     *     
     * @param 
     * @param conn
     * @param sql
     * @param clazz
     * @param params
     * @return
     * @throws Exception
     */
    public  <T> T getItem(Connection conn, String sql, Class<T> clazz, 
  String params[])throws Exception{
    	PreparedStatement pstmt = null;
    	ResultSet rset = null;
    	T t  = null;
    	try {
    		pstmt = conn.prepareStatement(sql);
    		//set parameter 
               for(int i=0; i<params.length; i++){
    			pstmt.setString(i+1, (String)params[i]);
    		}
              rset = pstmt.executeQuery();
            if (rset.next()) {
                t =  clazz.newInstance();
    		for(int i=0; i< proterty2Column.length; i++) {
                	String propertyName = proterty2Column[i][1].trim();
                    String ColumnName = proterty2Column[i][0].trim();
                    mapResultSet2Object(rset, t, propertyName, ColumnName);
                }
            }
        } catch (Exception e) {
        	e.printStackTrace();
	    } finally {
	        DBUtil.closeAll(conn, pstmt, rset);
	    }
     return t;
    }

   /**
     *     
     * @param 
     * @param conn
     * @param sql
     * @param proterty2Column
     * @param clazz
     * @return
     * @throws Exception
     */
    public   <T> List <T> getItems(Connection conn, String sql, Class<T> clazz,  
String[] params) throws Exception{
    	if (proterty2Column.length == 0)
            return null;
    	PreparedStatement pstmt = null;
    	ResultSet rset = null;
    	List<T> list = new ArrayList<T>();
    	try {
    		pstmt = conn.prepareStatement(sql);
               for(int i=0; i<params.length; i++){
    			pstmt.setString(i+1, (String)params[i]);
    		}
    		rset = pstmt.executeQuery();
            while (rset.next()) {
    		for(int i=0; i< proterty2Column.length; i++) {
                	String propertyName = proterty2Column[i][1].trim();
                    String ColumnName = proterty2Column[i][0].trim();
                    fetchResult2Object(rset, obj, propertyName, ColumnName);
                }
                if( null == obj ) continue;
                list.add(obj);
            }
        } catch (Exception e) {
        	e.printStackTrace();
	    } finally {
	        DBUtil.closeAll(conn, pstmt, rset);
	    }
     return list;
    }