本文整理汇总了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);
}
}