1 package com.javaweb.utils;
2
3 import java.util.Map;
4 import java.util.UUID;
5
6 import org.apache.commons.beanutils.BeanUtils;
7 import org.apache.commons.beanutils.ConvertUtils;
8 /**
9 * 小工具
10 * @author Administrator
11 *
12 */
13 public class CommonsUtils {
14
15 /**
16 * 生成一个不重复的字符串
17 * @return
18 */
19 public static String uuid(){
20
21 return UUID.randomUUID().toString().toUpperCase().replace("-", "");
22 }
23
24 /**
25 * 把Map转成 指定对象
26 * 使用该方法依赖于 org.apache.commons.beanutils
27 * @param map
28 * @param clazz
29 * @return
30 */
31 @SuppressWarnings("rawtypes")
32 public static <T> T toBean(Map map, Class<T> clazz){
33 try {
34 // 1. 通过参数实例化clazz对象
35 T bean = clazz.newInstance();
36 // 类型转换器
37 ConvertUtils.register(new DateConvertUtils(), java.util.Date.class);
38 // 2. 通过BeanUtils.populate把map的数据封装到bean中
39 BeanUtils.populate(bean, map);
40 return bean;
41 } catch (Exception e) {
42 throw new RuntimeException(e);
43 }
44
45 }
46
47 }