java反射机制完成List map 与List javabean ,List 对象 之间的互相转换

时间:2025-02-16 10:39:20
  • public class ListUtil{
  • /**
  • * 将JavaBean转换成Map
  • *
  • * @param obj
  • * @return
  • * @throws SecurityException
  • * @throws NoSuchMethodException
  • * @throws InvocationTargetException
  • * @throws IllegalArgumentException
  • * @throws IllegalAccessException
  • */
  • public static Map beanToMap(Object obj) throws NoSuchMethodException, SecurityException, IllegalAccessException,
  • IllegalArgumentException, InvocationTargetException {
  • // 创建map集合
  • Map map = new HashMap();
  • // 获取JavaBean中所有属性
  • Field[] fields = ().getDeclaredFields();
  • for (Field fie : fields) {
  • // 将属性第一个字母转换成大写
  • String frist = ().substring(0, 1).toUpperCase();
  • // 获取属性的类型
  • Class<?> type = ();
  • // 封装属性的get
  • String getter = "";
  • if ("boolean".equals(())) {
  • getter = "is" + frist + ().substring(1);
  • } else {
  • getter = "get" + frist + ().substring(1);
  • }
  • // 获取JavaBean的方法
  • Method method = ().getMethod(getter, new Class[] {});
  • // 调用方法,并接收返回值
  • Object objec = (obj, new Object[] {});
  • // 判断返回值不为空
  • if (objec != null) {
  • ((), objec);
  • } else {
  • ((), "");
  • }
  • }
  • return map;
  • }
  • /**
  • * 将Map转换为JavaBean
  • *
  • * @param map
  • * @param obj
  • * @return
  • * @throws SecurityException
  • * @throws NoSuchMethodException
  • * @throws InvocationTargetException
  • * @throws IllegalArgumentException
  • * @throws IllegalAccessException
  • */
  • public static Object mapToBean(Map<String, Object> map, Object obj) throws NoSuchMethodException, SecurityException,
  • IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  • // 获取JavaBean中的所有属性
  • Field[] field = ().getDeclaredFields();
  • for (Field fi : field) {
  • // 判断key值是否存在
  • if ((())) {
  • // 获取key的value值
  • String value = (()).toString();
  • // 将属性的第一个字母转换为大写
  • String frist = ().substring(0, 1).toUpperCase();
  • // 属性封装set方法
  • String setter = "set" + frist + ().substring(1);
  • // 获取当前属性类型
  • Class<?> type = ();
  • // 获取JavaBean的方法,并设置类型
  • Method method = ().getMethod(setter, type);
  • // 判断属性为double类型
  • if ("".equals(())) {
  • // 调用当前Javabean中set方法,并传入指定类型参数
  • (obj, value);
  • } else if ("int".equals(())) {
  • (obj, (value));
  • }else if ("double".equals(())) {
  • (obj, (value));
  • } else if ("char".equals(())) {
  • (obj, value);
  • }
  • }
  • }
  • return obj;
  • }
  • /**
  • * 将List<Map<String,Object>>转换成List<javaBean>
  • *
  • * @param listm
  • * @param obj
  • * @return
  • * @throws InvocationTargetException
  • * @throws IllegalArgumentException
  • * @throws IllegalAccessException
  • * @throws SecurityException
  • * @throws NoSuchMethodException
  • */
  • public static Object ListMapToListBean(List<Map<String, Object>> listm, Object obj)
  • throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
  • InvocationTargetException {
  • List<Object> list = new ArrayList<Object>();
  • // 循环遍历出map对象
  • for (Map<String, Object> m : listm) {
  • // 调用将map转换为JavaBean的方法
  • Object objs = mapToBean(m, obj);
  • // 添加进list集合
  • (objs);
  • }
  • return list;
  • }
  • /**
  • * 将list<javabean>转换为List<Map>
  • *
  • * @param list
  • * @return
  • * @throws NoSuchMethodException
  • * @throws SecurityException
  • * @throws IllegalAccessException
  • * @throws IllegalArgumentException
  • * @throws InvocationTargetException
  • */
  • public static List<Map<String, Object>> ListBeanToListMap(List<Object> list) throws NoSuchMethodException,
  • SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  • List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
  • for (Object ob : list) {
  • (beanToMap(ob));
  • }
  • return listmap;
  • }
  • }