java實(shí)體類轉(zhuǎn)成map的實(shí)現(xiàn)
更新時(shí)間:2022年06月21日 11:11:54 作者:南大白
這篇文章主要介紹了java實(shí)體類轉(zhuǎn)成map的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
java實(shí)體類轉(zhuǎn)成map
1.第一種
?<!-- 配置gson --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.google.code.gson</groupId> ? ? ? ? ? ? <artifactId>gson</artifactId> ? ? ? ? ? ? <version>2.8.6</version> ? ? ? ? </dependency> ? ? ? ?Map<String, Object> map = null; ? String jsonString = JSONUtil.toJsonStr(uploadBaseEntity); ? ? ? ?Gson gson = new Gson(); ? ? ? ? map = new HashMap<>(); ? ? ? ? map = gson.fromJson(jsonString, map.getClass());
2.第二種
/**
? ? ?* 對(duì)象轉(zhuǎn)化為Map
? ? ?*
? ? ?* @param obj
? ? ?* @return
? ? ?* @throws Exception
? ? ?*/
? ? public static Map<String, String> objectToMap01(Object obj) throws Exception {
? ? ? ? if (obj == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? Map<String, String> map = new HashMap<String, String>();
?
? ? ? ? Field[] declaredFields = obj.getClass().getDeclaredFields();
? ? ? ? for (Field field : declaredFields) {
? ? ? ? ? ? field.setAccessible(true);
? ? ? ? ? ? map.put(field.getName(), (String) field.get(obj));
? ? ? ? }
?
? ? ? ? return map;
? ? }java實(shí)體類與map集合互轉(zhuǎn)
廢話不說(shuō),直接上代碼!留著用吧...
package com.ddm.message.test;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
?* Java實(shí)體類與Map互轉(zhuǎn)
?* @author Administrator
?*
?*/
public class entityMapTransUtils {
? ? private static final Logger logger = LoggerFactory.getLogger(entityMapTransUtils.class);
? ? /**
? ? ?* Java實(shí)體類轉(zhuǎn)Map:方法一
? ? ?* @param obj
? ? ?* @return
? ? ?*/
? ? public static Map<String, Object> entityToMap1(Object obj){
? ? ? ? Map<String, Object> map = new HashMap<String, Object>();
? ? ? ? Class<?> clazz = obj.getClass();
? ? ? ? for(Field field : clazz.getDeclaredFields()){
? ? ? ? ? ? field.setAccessible(true);
? ? ? ? ? ? String fieldName = field.getName();
? ? ? ? ? ? Object object = null;
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? object = field.get(obj);
? ? ? ? ? ? } catch (IllegalArgumentException | IllegalAccessException e) {
? ? ? ? ? ? ? ? logger.info(e.getMessage());
? ? ? ? ? ? }
? ? ? ? ? ? map.put(fieldName, object);
? ? ? ? }
? ? ? ? return map;
? ? }
? ? /**
? ? ?* Java實(shí)體類轉(zhuǎn)Map:方法二
? ? ?* @param obj
? ? ?* @return
? ? ?*/
? ? public static Map<String, Object> entityToMap2(Object obj){
? ? ? ? Map<String, Object> map = new HashMap<String, Object>();
? ? ? ? try {
? ? ? ? ? ? BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
? ? ? ? ? ? PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
? ? ? ? ? ? for (PropertyDescriptor property : propertyDescriptors) {
? ? ? ? ? ? ? ? String key = property.getName();
? ? ? ? ? ? ? ? // 過(guò)濾class屬性
? ? ? ? ? ? ? ? if (!key.equals("class")) {
? ? ? ? ? ? ? ? ? ? // 得到property對(duì)應(yīng)的getter方法
? ? ? ? ? ? ? ? ? ? Method getter = property.getReadMethod();
? ? ? ? ? ? ? ? ? ? Object value = getter.invoke(obj);
? ? ? ? ? ? ? ? ? ? map.put(key, value);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? logger.info(e.getMessage());
? ? ? ? }
? ? ? ? return map;
? ? }
? ??
? ? /**
? ? ?* Map轉(zhuǎn)實(shí)體類:要轉(zhuǎn)換的Map的key跟實(shí)體類屬性名相同的數(shù)據(jù)會(huì)轉(zhuǎn)過(guò)去,不相同的字段會(huì)為null
? ? ?* @param clazz
? ? ?* @param map
? ? ?* @return
? ? ?*/
? ? public static <T> T mapToEntity1(Class<T> clazz,Map<String, Object> map){
? ? ? ? T obj = null;
? ? ? ? try {
? ? ? ? ? ? BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
? ? ? ? ? ? obj = clazz.newInstance(); // 創(chuàng)建 JavaBean 對(duì)象
? ? ? ? ? ? PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
? ? ? ? ? ? // 給 JavaBean 對(duì)象的屬性賦值
? ? ? ? ? ? for (int i = 0; i < propertyDescriptors.length; i++) {
? ? ? ? ? ? ? ? PropertyDescriptor descriptor = propertyDescriptors[i];
? ? ? ? ? ? ? ? String propertyName = descriptor.getName();
? ? ? ? ? ? ? ? if (map.containsKey(propertyName)) {
? ? ? ? ? ? ? ? ? ? Object value = map.get(propertyName);
? ? ? ? ? ? ? ? ? ? Object[] args = new Object[1];
? ? ? ? ? ? ? ? ? ? args[0] = value;
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? descriptor.getWriteMethod().invoke(obj, args);
? ? ? ? ? ? ? ? ? ? } catch (InvocationTargetException e) {
? ? ? ? ? ? ? ? ? ? ? ? logger.info(e.getMessage());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch (IllegalAccessException e) {
? ? ? ? ? ? logger.info(e.getMessage());
? ? ? ? } catch (IntrospectionException e) {
? ? ? ? ? ? logger.info(e.getMessage());
? ? ? ? } catch (IllegalArgumentException e) {
? ? ? ? ? ? logger.info(e.getMessage());
? ? ? ? } catch (InstantiationException e) {
? ? ? ? ? ? logger.info(e.getMessage());
? ? ? ? }
? ? ? ? return (T)obj;
? ? }
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
本地啟動(dòng)RocketMQ未映射主機(jī)名產(chǎn)生的超時(shí)問(wèn)題最新解決方案
這篇文章主要介紹了本地啟動(dòng)RocketMQ未映射主機(jī)名產(chǎn)生的超時(shí)問(wèn)題,本文給大家分享最新解決方案,感興趣的朋友跟隨小編一起看看吧2024-02-02
老生常談Java中List與ArrayList的區(qū)別
大家都知道List是接口,ArrayList是List接口的一個(gè)實(shí)現(xiàn)類,接下來(lái)通過(guò)本文給大家介紹Java中List與ArrayList的區(qū)別,需要的朋友可以參考下2022-08-08
Java將網(wǎng)絡(luò)圖片轉(zhuǎn)成輸入流以及將url轉(zhuǎn)成InputStream問(wèn)題
這篇文章主要介紹了Java將網(wǎng)絡(luò)圖片轉(zhuǎn)成輸入流以及將url轉(zhuǎn)成InputStream問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Nacos通過(guò)RefreshScope實(shí)現(xiàn)配置自動(dòng)更新的方式分享
這篇文章主要給大家介紹了Nacos如何通過(guò)RefreshScope實(shí)現(xiàn)配置自動(dòng)更新,文中給了兩種實(shí)現(xiàn)方式供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-09-09
SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例
這篇文章主要介紹了SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

