PO实体类和表单form的相互填充工具类
近期由于领导要求减少工作量,提高工作效率,让做出一个通用的方法,能让PO和form相互填充,而不必一个一个set,工具类具体代码如下:
package com.hnjz.app.common;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import com.hnjz.base.util.DateUtil;
/**
* po实体类和form相互转换工具类
* @author cjl
*
*/
public class PoFormConverse {
/**
* 根据po填充form
* form支持的类型有,String,Long,Integer,Double,如果form上有其他特殊类型,不支持,请自行单独处理
* 如果页面上的输入框里的值有格式要求,也请再调用此方法后再自行处理一次
* @param po
* @param form
*/
public static void poToForm(Object po,Object form){
try {
Field[] po_fields = po.getClass().getDeclaredFields();//获得所有的属性
Method[] po_methods = po.getClass().getDeclaredMethods();//获得所有的方法
ArrayList<Method> po_get_methods = new ArrayList<Method>();//所有的get方法
ArrayList<Method> po_set_methods = new ArrayList<Method>();//所有的set方法
Method[] frm_methods = form.getClass().getDeclaredMethods();//获得所有的方法
ArrayList<Method> frm_get_methods = new ArrayList<Method>();//所有的get方法
ArrayList<Method> frm_set_methods = new ArrayList<Method>();//所有的set方法
//循环所有的方法,区分get方法,和set方法
for(int j = 0;j<po_methods.length;j++){
if(po_methods[j].getName().startsWith("get")){
po_get_methods.add(po_methods[j]);
}else if(po_methods[j].getName().startsWith("set")){
po_set_methods.add(po_methods[j]);
}
}
for(Method m:frm_methods){
if(m.getName().startsWith("get")){
frm_get_methods.add(m);
}else if(m.getName().startsWith("set")){
frm_set_methods.add(m);
}
}
//循环po的get方法,和form的set方法,把相同属性的po的值赋给form
for(Method poget:po_get_methods){
for(Method frmset:frm_set_methods){
if(poget.getName().substring(3).equals(frmset.getName().substring(3))){
Field poFieldName = po.getClass().getDeclaredField(lowerCaseFirstChar(poget.getName().substring(3)));
Field frmFieldName = form.getClass().getDeclaredField(lowerCaseFirstChar(frmset.getName().substring(3)));
String poFieldType = poFieldName.getGenericType().toString();// class java.lang.String
String frmFieldType = frmFieldName.getGenericType().toString();
String frmtemp = frmFieldType.substring(16);
String potemp = poFieldType.substring(16);
if(null!=poget.invoke(po)){
String pov = poget.invoke(po).toString();
if(poFieldType.equals(frmFieldType)){
frmset.invoke(form, poget.invoke(po));//两个对象相同的属性的类型相同呢,直接赋值
}else{
//类型不相同,则把po的属性类型变成和form的属性类型相同再赋值
if("String".equals(frmtemp)&&!"Date".equals(potemp)){
frmset.invoke(form, String.valueOf(pov));
}else if("Integer".equals(frmtemp)){
frmset.invoke(form, Integer.valueOf(pov));
}else if("Long".equals(frmtemp)){
frmset.invoke(form, Long.valueOf(pov));
}else if("Double".equals(frmtemp)){
frmset.invoke(form, Double.valueOf(pov));
}else if("String".equals(frmtemp)&&"Date".equals(potemp)){
frmset.invoke(form, pov);
}else{
continue;
}
}
}
}
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 根据form填充po
* po支持的类型有,String,Long,Integer,Double,Date 如果po有其他特殊类型,不支持,请自行单独处理
* 如果po的值有格式要求,也请再调用此方法后再自行处理一次
* Date类型保存格式,yyyy-MM-dd
* @param form
* @param po
*/
public static void formToPo(Object form,Object po){
try {
Method[] po_methods = po.getClass().getDeclaredMethods();//获得所有的方法
ArrayList<Method> po_get_methods = new ArrayList<Method>();//所有的get方法
ArrayList<Method> po_set_methods = new ArrayList<Method>();//所有的set方法
Method[] frm_methods = form.getClass().getDeclaredMethods();//获得所有的方法
ArrayList<Method> frm_get_methods = new ArrayList<Method>();//所有的get方法
ArrayList<Method> frm_set_methods = new ArrayList<Method>();//所有的set方法
//循环所有的方法,区分get方法,和set方法
for(int j = 0;j<po_methods.length;j++){
//System.out.println("method "+po_methods[j].getName());
if(po_methods[j].getName().startsWith("get")){
po_get_methods.add(po_methods[j]);
}else if(po_methods[j].getName().startsWith("set")){
po_set_methods.add(po_methods[j]);
}
}
for(Method m:frm_methods){
if(m.getName().startsWith("get")){
frm_get_methods.add(m);
}else if(m.getName().startsWith("set")){
frm_set_methods.add(m);
}
}
//循环form的get方法,和Po的set方法,如属性名字相同,则把form的值给po
for(Method frmget:frm_get_methods){
for(Method poset:po_set_methods){
if(poset.getName().substring(3).equals(frmget.getName().substring(3))){
Field poFieldName = po.getClass().getDeclaredField(lowerCaseFirstChar(poset.getName().substring(3)));
Field frmFieldName = form.getClass().getDeclaredField(lowerCaseFirstChar(frmget.getName().substring(3)));
String poFieldType = poFieldName.getGenericType().toString();// 如:class java.lang.String
String frmFieldType = frmFieldName.getGenericType().toString();
String frmtemp = frmFieldType.substring(16); //如:String
String potemp = poFieldType.substring(16);
if(null!=frmget.invoke(form)){
String frmv = frmget.invoke(form).toString();
if(poFieldType.equals(frmFieldType)){
poset.invoke(po, frmget.invoke(form));//两个对象相同的属性的类型相同呢,直接赋值
}else{
//类型不相同,则把form的属性类型变成和po的属性类型相同再赋值
if("String".equals(potemp)){
poset.invoke(po, String.valueOf(frmv));
}else if("Integer".equals(potemp)){
poset.invoke(po, Integer.valueOf(frmv));
}else if("Long".equals(potemp)){
poset.invoke(po, Long.valueOf(frmv));
}else if("Double".equals(potemp)){
poset.invoke(po, Double.valueOf(frmv));
}else if("Date".equals(potemp)){
poset.invoke(po, DateUtil.convertStringToDate(frmv));
}else{
continue;
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//首字母大写
public static String upCaseFirstChar(String str){
char[] chars = str.toCharArray();
if (chars[0] >= 'a' && chars[0] <= 'z') {
chars[0] = (char)(chars[0] - 32);
}
return new String(chars);
}
//首字母小写
public static String lowerCaseFirstChar(String str){
char[] chars = str.toCharArray();
if (chars[0] >= 'A' && chars[0] <= 'Z') {
chars[0] = (char)(chars[0] + 32);
}
return new String(chars);
}
}