import java.lang.reflect.Field; /** *@desc 根据结果集获取其setVO的各个字符串str * 例如:类名xx.xx.xx.Testvo ,属性 name, 返回:TestVO.setName(StringUtil.nullToEmpty(rowSet.getString("name"))); *@author xxx *@since 2014-9-29 */ public class Str { public static void main(String[] args) { String classVOPackageName = "com.xxx.xxx.xxx.vo.TestVO";//类的全路径 getVOFromRowSet(classVOPackageName); } /** *@desc 根据结果集获取其setVO的各个字符串str *@author xxx *@since 2014-9-29 *@return void */ public static void getVOFromRowSet(String classVOPackageName){ String[] strs = getFieldArray(classVOPackageName);//获取属性array String classVOName = getMethodName(classVOPackageName.substring(classVOPackageName.lastIndexOf(".")+1));//获取纯类名 for(String str: strs){ sql2String(classVOName,str); } } /** *@desc 根据VO全类名获取属性的string数组 *@author xxx *@since 2014-9-29 *@param ClassVOPackageName VO全类名 *@return String[] 属性的string数组 */ public static String[] getFieldArray(String ClassVOPackageName){ String[] fieldsStr = null; try { Class<?> voCls = Class.forName(ClassVOPackageName); Field[] fields = voCls.getDeclaredFields(); fieldsStr = new String[fields.length]; int count = 0; for(Field field:fields){ fieldsStr[count] = field.getName(); count ++; } } catch (Exception e) { e.printStackTrace(); } return fieldsStr; } /** *@desc 根据属性名称将其首字母大写 *@author xxx *@since 2014-9-29 *@param field 属性名称 *@return String 该属性的get方法str */ private static String getMethodName(String field){ byte[] items = field.getBytes(); if((char)items[0]>='A'&&(char)items[0]<='Z'){ items[0] += 32; }else if((char)items[0]>='a'&&(char)items[0]<='z'){ items[0] -= 32; } return new String(items); } /** *@desc 获取字各个属性的set字符串 *@author xxx *@since 2014-9-29 *@param vo *@param field *@return void */ private static void sql2String(String vo,String field) { String str =vo+".set"+getMethodName(field)+"(StringUtil.nullToEmpty(rowSet.getString(\""+field+"\")));"; System.out.println(str); } }
原文地址:http://blog.csdn.net/hzk1562110692/article/details/40028149