~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
开发工具与关键技术:
作者:陈剑波
撰写时间:2020年04月28日
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CommonUtils类就两个方法:
/** * 随机生成32位长的字符串,通常用来做实体类的ID */ @Test public void testUuid() { String s = CommonUtils.uuid();//生成随机32位长的字符串 System.out.println(s); }
/** * 把Map类型映射成Bean类型。 * 要求map中键的名称与Person类的属性名称相同。 * 即map的key分别为:pid、name、age、birthday,person的属性名称也是pid、name、age、birthday */ @Test public void testToBean() { Map<String,String> map = new HashMap<String,String>(); /* * map的key:pid、age、birthday、myname * person的属性:pid、age、birthday、name * map中没有名为name的键值,而多出一个名为myname的键值,所以映射后的person对象的name属性值为null。 * map中的age和birthday都是字符串类型,而person的age是int类型、birthday是Date类型,但toBean()方法会自动对Map中值进行类型转换。 */ map.put("pid", CommonUtils.uuid()); map.put("age", "23"); map.put("birthday", "2020-09-09"); map.put("myname", "布波");
Person p = CommonUtils.toBean(map, Person.class); System.out.println(p); } |