浏览 5571 次
锁定老帖子 主题:泛型BaseDAO
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-05-06
本文参照了[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; } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-05-11
。。。DbUtils里面有类似的封装哦。。
|
|
返回顶楼 | |
发表时间:2008-05-11
lz又重复造轮子了
|
|
返回顶楼 | |