本文整理匯總了Java中org.apache.commons.beanutils.BeanUtils.copyProperty方法的典型用法代碼示例。如果您正苦於以下問題:Java BeanUtils.copyProperty方法的具體用法?Java BeanUtils.copyProperty怎麽用?Java BeanUtils.copyProperty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.beanutils.BeanUtils
的用法示例。
在下文中一共展示了BeanUtils.copyProperty方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: copyProperties
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException {
PropertyDescriptor[] origDescriptors =
PropertyUtils.getPropertyDescriptors(orig);
for (PropertyDescriptor origDescriptor : origDescriptors) {
String name = origDescriptor.getName();
if ("class".equals(name)) {
continue; // No point in trying to set an object's class
}
if (PropertyUtils.isReadable(orig, name) &&
PropertyUtils.isWriteable(dest, name)) {
try {
Class origPropClass = PropertyUtils.getPropertyType(orig, name);
Class destPropClass = PropertyUtils.getPropertyType(dest, name);
if (destPropClass.isAssignableFrom(origPropClass)) {
Object value =
PropertyUtils.getSimpleProperty(orig, name);
BeanUtils.copyProperty(dest, name, value);
}
} catch (NoSuchMethodException e) {
// Should not happen
}
}
}
}
示例2: mapResults
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public <R> Iterable<R> mapResults(ResultSet resultSet, final Class<R> clz) throws SQLException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Set<String> metadataColumns = extractColumnNames(resultSet);
List<R> resultsList = new ArrayList<R>();
boolean resultIsNumber = Number.class.isAssignableFrom(clz);
while (resultSet.next()) {
R instance = null;
if (!resultIsNumber) {
instance = clz.newInstance();
}
for (String columnName : metadataColumns) {
Object value = resultSet.getObject(columnName);
if (value != null) {
log.debug(value.toString());
if (resultIsNumber) {
instance = (R) value;
break;
}
EntityPropertyBinding entityProperty = entityPropertiesResolver.resolveEntityPropertyBindingByStoreMappingName(columnName, clz);
if (entityProperty != null) {
BeanUtils.copyProperty(instance, entityProperty.getNameFullPath(), value);
}
}
}
resultsList.add(instance);
}
return resultsList;
}
示例3: insert
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
@Transactional(propagation = Propagation.REQUIRED)
public Object insert(Object entity) {
try{
BeanUtils.setProperty(entity,"projectGroupCode",SecurityContextUtil.getUserAccount().getProjectGroup().getProjectGroupCode());
BeanUtils.setProperty(entity, "updatedAt", LocalDateTime.now());
BeanUtils.setProperty(entity, "createdAt", LocalDateTime.now());
BeanUtils.copyProperty(entity, "user", SecurityContextUtil.getUserAccount().getUsername());
return getCurrentSession().save(entity);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
示例4: update
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
public Object update(Object entity) {
try {
BeanUtils.setProperty(entity, "updatedAt", LocalDateTime.now());
BeanUtils.copyProperty(entity, "user", SecurityContextUtil.getUserAccount().getUsername());
getCurrentSession().update(entity);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return null;
}
示例5: delete
import org.apache.commons.beanutils.BeanUtils; //導入方法依賴的package包/類
public void delete(Object entity) {
try {
BeanUtils.copyProperty(entity, "user", SecurityContextUtil.getUserAccount().getUsername());
BeanUtils.copyProperty(entity, "updatedAt", LocalDateTime.now());
BeanUtils.copyProperty(entity, "deleted",true);
getCurrentSession().update(entity);
} catch (Exception e) {
getCurrentSession().delete(entity);
}
}