在我们实际项目开发过程中,经常有大量的对单表的基本维护。在做这一部分工作时,最耗费我们宝贵时间的就是formbean与hibernate pojo之间的转换问题了。经过一天的努力,初见成效。贴出来,让大家拍拍砖。
源代码:
import java.lang.reflect.Field;
import java.lang.reflect.Method;- /**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author 赵志宽
* @version 1.0
*/
public class DataForm { - /**
* VOToForm
*
* @param vo Object
* @param form Object
* @throws ClassNotFoundException
*/
public static void VOToForm(Object vo, Object form) throws
ClassNotFoundException { - Field[] voFileds;
Field[] formFields;
Class beanVo = vo.getClass();
voFileds = beanVo.getFields();
Class formData = form.getClass();
formFields = formData.getFields();
try {
DataTrants(voFileds, formFields, vo, form, "0");
} catch (Exception ex) {
System.out.println("转换失败" + ex.getMessage());
}
} - /**
* FormToVO
*
* @param vo Object
* @param form Object
*/
public static void FormToVO(Object vo, Object form) {
Field[] voFileds;
Field[] formFields;
Class beanVo = vo.getClass();
voFileds = beanVo.getFields();
Class formData = form.getClass();
formFields = formData.getFields(); - try {
DataTrants(voFileds, formFields, vo, form, "1"); - } catch (Exception ex) {
System.out.println("转换失败" + ex.getMessage());
} - }
- /**
* DataTrants
* flag = "0" 从VO到FORM flag = "1" 从form到VO
* @param voFileds Field[]
* @param formFields Field[]
*/
public static void DataTrants(Field[] voFileds, Field[] formFields,
Object vo,
Object form, String flag) throws
Exception {
String voFieldName = "";
String formFieldName = "";
Method m = null;
Method v = null;
String value = "";
Class param[] = new Class[1];
Class param1[] = new Class[0];
Object[] paramValue = new Object[1];
if (flag.equals("0")) {
for (int i = 0; i < voFileds.length; i++) {
voFieldName = voFileds[i].getName();
for (int j = 0; j < formFields.length; j++) {
formFieldName = formFields[j].getName();
if (voFieldName.equals(formFieldName.toLowerCase())) {
v = vo.getClass().getMethod(getMethodName("get",
voFieldName),
param1);
value = String.valueOf(v.invoke(vo, param1));
param[0] = formFields[j].getType();
m = form.getClass().getMethod("set" +
voFieldName.toUpperCase(), param);
paramValue[0] = value;
if (String.valueOf(param[0].getName()).equals(
"java.lang.Integer")) {
paramValue[0] = new Integer(value);
}
m.invoke(form, paramValue);
}
}
}
} else {
if (flag.equals("1")) {
for (int i = 0; i < formFields.length; i++) {
formFieldName = formFields[i].getName();
v = form.getClass().getMethod("get" + formFieldName, param1);
value = String.valueOf(v.invoke(form, param1));
param[0] = vo.getClass().getField(formFieldName.toLowerCase()).
getType();
m = vo.getClass().getMethod(getMethodName("set",
formFieldName), param);
paramValue[0] = value;
if (String.valueOf(param[0].getName()).equals(
"java.lang.Integer")) {
paramValue[0] = new Integer(value);
}
if (String.valueOf(param[0].getName()).equals(
"java.util.Date")) {
int year = new Integer(value.substring(0, 2)).intValue();
int month = new Integer(value.substring(4, 6)).intValue();
int days = new Integer(value.substring(8, 10)).intValue();
paramValue[0] = new java.util.Date(year, month, days);
}
m.invoke(vo, paramValue);
}
} else {
throw new Exception("数据转换标志不对");
} - }
} - /**
* getMethodName
*
* @param flag String
* @param columName String
* @return String
*/
private static String getMethodName(String flag, String columName) {
String pre = columName.substring(0, 1).toUpperCase();
String next = columName.substring(1, columName.length()).toLowerCase();
StringBuffer methodName = new StringBuffer();
methodName.append(flag);
methodName.append(pre);
methodName.append(next);
return new String(methodName); - }