public static <T> T converter(Map<String, Object> map, Class<T> clz) {
T obj = null;
try {
obj = clz.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(clz);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
setter.invoke(obj, value);
}
}
} catch (Exception e) {
logger.error("map转{}异常", clz.getName(), e);
}
return obj;
}

  

public static <T> List<T> converter(List<Map<String, Object>> list, Class<T> clz) {
List<T> rst = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
rst.add(converter(list.get(i), clz));
}
return rst;
}

  

Map转Bean小工具的更多相关文章

  1. 简单的map转换成Bean的工具

    简单的map转换成Bean的工具 package com.sd.microMsg.util; import java.lang.reflect.Field; import java.lang.refl ...

  2. java的bean和map互转的工具类

    import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;i ...

  3. PHP API接口测试小工具

    前端时间给手机客户端做接口,当时弱爆了,写完API接口后,也不怎么测试,最后是等客户端调用的时候检验API的正确性. 后面利用PHP的curl实现Post请求,检验API接口的正确性:配合前面做的一个 ...

  4. 三个 DAL 相关的Java代码小工具

    最近在做 DAL (Data Access Layer 数据访问层) 的服务化,发现有不少地方是人工编写比较繁琐的,因此写了几个小工具来完成. 1.  从 DAO 类自动生成 CoreService ...

  5. 数据库表转换成javaBean对象小工具

    package test.utils; import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter; ...

  6. linux相关小工具的使用(一)————代码相关工具

    在linux环境下,对于程序员来说,知道使用一些好用的小工具,对源代码的阅读.编译和调试都有着事半功倍的效果,这里我也是边学边写的原则,把自己知道的一丁点小知识分享给大家. 一.  源代码的阅读 首先 ...

  7. 2014年Windows平台软件推荐:神器小工具(骨灰级

    原文  http://www.wtoutiao.com/a/120621.html 底层工具 “If you know how to use Process Monitor competently, ...

  8. Windows平台软件推荐:神器小工具(骨灰级)

    底层工具 "If you know how to use Process Monitor competently, people of both sexes will immediately ...

  9. python3 小工具

    扫描IP的端口是否开放:Porttest.py # -*- coding: utf-8 -*- import sys import os import socket #扫描 def scanport( ...

随机推荐

  1. css系列教程--color direction line-height letter-spacing

    css标签:colorcolor:用法color:指定文本的颜色color:red/#fff/unicode; direction:用法 direction:定义文本的方向.dirction:ltr/ ...

  2. 01js高级_1

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. mac 桌面美化

    官网:Übersicht 先来大图:  当然,,,我自己的这个还不成型,去官网看看吧,有大神们做好的各种主题可选哦~ 像这样1: 这样2: 甚至这样3:(酷毙了有木有..)        Downlo ...

  4. 远程控制利器TeamViewer使用教程(图)

    TeamViewer是什么? 他是一款免费的可以穿透内网的远程控制软件,可以实现桌面共享,文件传送等功能,简单一点说就是和QQ远程协助一样,但是比QQ的远程协助功能更为强大. TeamViewer与木 ...

  5. python image show()方法的预览问题

      在windows下面使用PIL中Image的show()函数时,执行下列代码: from PIL import Image img = Image.open("1.png") ...

  6. Multipart/form-data POST文件上传详解(转)

    Multipart/form-data POST文件上传详解 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form ...

  7. mysql入库中文乱码问题

    mysql> show variables like '%char%';+--------------------------+----------------------------+| Va ...

  8. SWOT自我分析

    个人信息: 大三学生 二本大学 软件工程专业 一:SWOT自我分析 Strenghs(优势): 1.有着良好的作息习惯,坚持锻炼 2.专注力强,能沉下心来学习 3.有着强烈的危机意思,明白不仅则退的道 ...

  9. poj3650---将一个字符串中的特定字符转换

    #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; int i; w ...

  10. Hibernate 、多表关联映射 - 一对一关系映射(one- to-one)

    hibernate.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...