重温Java持久化(七)

2014-11-24 08:36:39 · 作者: · 浏览: 6
am values
* @return size
*/
public static Object buildProperties(Class clazz, List fileds, List values) throws InstantiationException, IllegalAccessException {
if (values.size() != fileds.size())
throw new RuntimeException("fileds的size和values的size不一致!");
Object obj = clazz.newInstance();
for (int i = 0; i < fileds.size(); i++) {
buildPropertie(obj, fileds.get(i), values.get(i));
}
return obj;
}
/**
* 给对象的某个字段封装数值
*/
public static void buildPropertie(Object obj, Field f, Object value) {
try {
String methodName = buildSetMethod(f.getName());
// 给某个字段封装数值,methodName方法名,obj.getClass().getMethod(buildGetMethod(f.getName())).getReturnType()返回类型
// invoke执行方法,obj要赋值的对象,value要赋的值
obj.getClass().getMethod(methodName, obj.getClass().getMethod(buildGetMethod(f.getName())).getReturnType()).invoke(obj, value);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 构建get方法
* @param fieldName
* @return String
*/
public static String buildGetMethod(String fieldName) {
String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
return methodName;
}
/**
* 构建set方法
* @param fieldName
* @return String
*/
public static String buildSetMethod(String fieldName) {
String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
return methodName;
}
/**
* 根据字段名或对象对应的get方法值
* @param obj 对象
* @param fieldName字段名
* @return Object
*/
public static Object getPropertie(Object obj, String fieldName) {
Object val = "";
try {
String methodName = buildGetMethod(fieldName);
val = obj.getClass().getMethod(methodName).invoke(obj);
} catch (Exception e) {
e.printStackTrace();
}
return val;
}
/**
* 根据字段获取对应的值
* @param entity
* @param allFields
* @return Object[]
*/
public static Object[] buildParams(Object entity, List fields) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Object[] params = new Object[fields.size()];// 字段参数值
for (int i = 0; i < fields.size(); i++) {
Field f = fields.get(i);
String methodName = ClassUtil.buildGetMethod(f.getName());
Object v = entity.getClass().getMethod(methodName).invoke(entity);
if (v instanceof Enum) {
v = ((Enum) v).ordinal();
}
params[i] = v;
}
return params;
}
public static Object buildParams(Object entity, Field field) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
String methodName = ClassUtil.buildGetMethod(field.getName());
Object v = entity.getClass().getMethod(methodName).invoke(entity);
return v;
}
public static Object buildParams(Object entity, String fieldName) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
String methodName = ClassUtil.buildGetMethod(fieldName);
Object v = entity.getClass().getMethod(methodName).invoke(entity);
return v;
}
/**
* 格式化
* @param str
* @param params
* @return String
*/
public static String formartString(String str, Object... params) {
for (int i = 0; i < params.length; i++) {
Object obj