本文整理匯總了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;
}