本文整理汇总了Java中org.apache.commons.beanutils.BeanUtils.populate方法的典型用法代码示例。如果您正苦于以下问题:Java BeanUtils.populate方法的具体用法?Java BeanUtils.populate怎么用?Java BeanUtils.populate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.beanutils.BeanUtils
的用法示例。
在下文中一共展示了BeanUtils.populate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mapToBean
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
* map to bean
* 转换过程中,由于属性的类型不同,需要分别转换。
* java 反射机制,转换过程中属性的类型默认都是 String 类型,否则会抛出异常,而 BeanUtils 项目,做了大量转换工作,比 java 反射机制好用
* BeanUtils 的 populate 方法,对 Date 属性转换,支持不好,需要自己编写转换器
*
* @param map 待转换的 map
* @param bean 满足 bean 格式,且需要有无参的构造方法; bean 属性的名字应该和 map 的 key 相同
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void mapToBean(Map<String, Object> map, Object bean) throws IllegalAccessException, InvocationTargetException {
//注册几个转换器
ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);
ConvertUtils.register(new SqlTimestampConverter(null), java.sql.Timestamp.class);
//注册一个类型转换器 解决 common-beanutils 为 Date 类型赋值问题
ConvertUtils.register(new Converter() {
// @Override
public Object convert(Class type, Object value) { // type : 目前所遇到的数据类型。 value :目前参数的值。
// System.out.println(String.format("value = %s", value));
if (value == null || value.equals("") || value.equals("null"))
return null;
Date date = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = dateFormat.parse((String) value);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
}, Date.class);
BeanUtils.populate(bean, map);
}
示例2: doGet
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
User user = new User();
HashMap map = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
map.put(name, request.getParameterValues(name));
}
try{
BeanUtils.populate(user, map); //BAD
BeanUtilsBean beanUtl = BeanUtilsBean.getInstance();
beanUtl.populate(user, map); //BAD
}catch(Exception e){
e.printStackTrace();
}
}
示例3: _get
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/********************************** get *************************************/
@Override
public <T> T _get(Class<T> clazz, Long pv) throws Exception {
Map<String, Object> param = new HashMap<>();
String tableName = GeneralMapperReflectUtil.getTableName(clazz);
String primaryKey = GeneralMapperReflectUtil.getPrimaryKey(clazz);
List<String> queryColumn = GeneralMapperReflectUtil.getAllColumns(clazz, false);
param.put("_table_", tableName);
param.put("_pk_", primaryKey);
param.put("_pv_", pv);
param.put("_query_column_", queryColumn);
Map map = sqlSession.selectOne(Crudable.GET, param);
T t = clazz.newInstance();
BeanUtils.populate(t, map);
return t;
}
示例4: map2BeanList
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
* 类实现 Maps --> Bean
* @param objClass
* @param objMaps
*/
public static <T> List<T> map2BeanList(List<Map<String, Object>> objMaps, Class<T> objClass) {
if (objMaps == null || objClass == null) {
return Collections.emptyList();
}
List<T> objList = new ArrayList<T>();
try {
for(Map<String, Object> mapObj : objMaps) {
T obj = objClass.newInstance();
BeanUtils.populate(obj, mapObj);
objList.add(obj);
}
} catch (Exception e) {
logger.error(e.getMessage());
}
return objList;
}
示例5: testMap2Bean
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
@Test
public void testMap2Bean() throws Exception {
Map<String, Object> map = Maps.newHashMap();
map.put("Username", "jonnyliu");
map.put("Age", 39);
User user = new User();
Map map1 = Maps.newHashMap();
map.forEach((x, y) -> {
char[] charArray = x.toCharArray();
charArray[0] = Character.toLowerCase(charArray[0]);
map1.put(new String(charArray), y);
});
BeanUtils.populate(user, map1);
System.out.println(user);
}
示例6: mapToBean
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
public static <T> T mapToBean(Map<String, Object> map, Class<T> cls){
T bean = null;
try {
bean = cls.newInstance();
// 将map中的数据封装到javabean
BeanUtils.populate(bean, map);
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
示例7: applyFrom
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
static void applyFrom(final String value, final Object object) {
if (value == null) {
return;
}
final Map<String, String> kv = Splitter.on(",").trimResults().omitEmptyStrings()
.withKeyValueSeparator("=").split(value);
try {
BeanUtils.populate(object, kv);
} catch (final IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
开发者ID:HPI-Information-Systems,项目名称:AdvancedDataProfilingSeminar,代码行数:15,代码来源:ConfigurationMapper.java
示例8: newInstance
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
* 新建实例
*
* @param cls
* 实体类
* @param list
* 实体属性Map
* @return
*/
public static final <E> E newInstance(Class<E> cls, Map<String, ?> map) {
E object = null;
try {
object = cls.newInstance();
BeanUtils.populate(object, map);
} catch (Exception e) {
throw new InstanceException(e);
}
return object;
}
示例9: _gets
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
@Override
public <T> List<T> _gets(Class<T> clazz, CrudParam crudParam) throws Exception {
Map<String, Object> param = new HashMap<>();
String tableName = GeneralMapperReflectUtil.getTableName(clazz);
param.put("_table_", tableName);
param.put("_query_column_", null == crudParam.getQueryColumn() ? GeneralMapperReflectUtil.getAllColumns(clazz, false) : crudParam.getQueryColumn());
Object entity = crudParam.getEntity();
if (entity != null) {
param.put("_condition_param_", null == crudParam.getConditionParam() ? GeneralMapperReflectUtil.getFieldValueMappingExceptNull(entity, false) : crudParam.getConditionParam());
}
param.put("_where_exp_", crudParam.getConditionExp());
param.put("_order_exp_", crudParam.getOrderExp());
param.put("_pks_", crudParam.getPks());
//param.put("",);
if (crudParam.getPageSize() != null && crudParam.getPageNo() != null) {
Map<String, Integer> page = new HashMap<>();
page.put("pageSize", crudParam.getPageSize());
page.put("startRowNo", (crudParam.getPageNo() - 1) * crudParam.getPageSize());
param.put("page", page);
}
List<Map> maps = sqlSession.selectList(Crudable.GETS, param);
List<T> ts = new ArrayList<>();
for (Map map : maps) {
T t = clazz.newInstance();
BeanUtils.populate(t, map);
ts.add(t);
}
return ts;
}
示例10: newInstance
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
* 新建实例
*
* @param cls 实体类
* @param list 实体属性Map
* @return
*/
public static final <E> E newInstance(Class<E> cls, Map<String, ?> map) {
E object = null;
try {
object = cls.newInstance();
BeanUtils.populate(object, map);
} catch (Exception e) {
throw new InstanceException(e);
}
return object;
}
示例11: newInstance
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
* 新建实例
*
* @param cls
* 实体类
* @param list
* 实体属性Map
* @return
*/
public static final <E> E newInstance(Class<E> cls, Map<String, ?> map) {
E object = null;
try {
object = cls.newInstance();
BeanUtils.populate(object, map);
} catch (Exception e) {
throw new InstanceException(e);
}
return object;
}
示例12: assembleRequestParamForBean
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
protected <T> T assembleRequestParamForBean(HttpServletRequest request, Class<T> clazz, Page<?> page) {
Map<String, String> paraMap = assembleRequestParamForMap(request, page);
T t = null;
try {
t = clazz.newInstance();
BeanUtils.populate(t, paraMap);
} catch (Exception e) {
throw new RuntimeException("MAP转换BEAN时出错。");
}
return t;
}
示例13: modifyBean
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
* 将Bean的属性值修改为Map中对应的值
*
* @param property 一个Map对象,封装了相应的Bean类中的属性
* @param bean 需要修改的Bean类
* @return 返回一个属性已修改的Bean类实例
*/
public static <T extends Object> T modifyBean(Map<String, ? extends Object> property, T bean) {
try {
BeanUtils.populate(bean, property);
return bean;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例14: toBean
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
* 将一个Map对象转换成一个Bean对象
*
* @param property 一个Map对象,封装了相应的Bean中的属性
* @param clazz 需要转换成的Bean
* @return 返回一个封装好的Bean类实例,封装失败返回null
*/
public static <T extends Object> T toBean(Map<String, ? extends Object> property, Class<T> clazz) {
T bean = null;
try {
bean = clazz.newInstance();
BeanUtils.populate(bean, property);
} catch (Exception e) {
throw new RuntimeException(e);
}
return bean;
}
示例15: map2Bean
import org.apache.commons.beanutils.BeanUtils; //导入方法依赖的package包/类
/**
* 类实现 Map --> Bean
* @param map
* @param clazz
*/
public static <T> T map2Bean(Map<String, Object> map, Class<T> clazz) {
if (map == null || clazz == null) {
return null;
}
try {
T obj = clazz.newInstance();
BeanUtils.populate(obj, map);
return obj;
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}