public class test{ // 設置bean的某個屬性值 public static void setProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName, String value) throws Exception { // 獲取bean的某個屬性的描述符 PropertyDescriptor propDesc = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class); // 獲得用於寫入屬性值的方法 Method methodSetUserName = propDesc.getWriteMethod(); // 寫入屬性值 methodSetUserName.invoke(materialPriceLineVO, value); } // 獲取bean的某個屬性值 public static String getStringProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName) throws Exception { // 獲取Bean的某個屬性的描述符 PropertyDescriptor proDescriptor = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class); // 獲得用於讀取屬性值的方法 Method methodGet = proDescriptor.getReadMethod(); // 讀取屬性值 Object objValue = methodGet.invoke(materialPriceLineVO); if (ObjectUtils.isEmpty(objValue) || "null".equals(objValue)){ return null; }else { return objValue.toString(); } } public static String getIntegerProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName) throws Exception { // 獲取Bean的某個屬性的描述符 PropertyDescriptor proDescriptor = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class); // 獲得用於讀取屬性值的方法 Method methodGet = proDescriptor.getReadMethod(); // 讀取屬性值 Object objValue = methodGet.invoke(materialPriceLineVO); if (ObjectUtils.isEmpty(objValue) || "null".equals(objValue)){ return null; }else { return objValue.toString(); } } public static String getBigDecimalProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName) throws Exception { // 獲取Bean的某個屬性的描述符 PropertyDescriptor proDescriptor = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class); // 獲得用於讀取屬性值的方法 Method methodGet = proDescriptor.getReadMethod(); // 讀取屬性值 Object objValue = methodGet.invoke(materialPriceLineVO); if (ObjectUtils.isEmpty(objValue) || "null".equals(objValue)){ return null; }else { return objValue.toString(); } } }