Map與JavaBean相互轉(zhuǎn)換的工具類?
更新時間:2022年02月18日 16:52:28 作者:梁云亮
這篇文章主要介紹了Map與JavaBean相互轉(zhuǎn)換的工具類,在做導入的時候,遇到了需要將map對象轉(zhuǎn)化?成javabean的問題,也就是說,不清楚javabean的內(nèi)部字段排列,只知道m(xù)ap的?key代表javabean的字段名,value代表值,需要的朋友可以參考下
下面就來分享工具類的內(nèi)容:
使用范圍:JavaBean
類對象的屬性不能是數(shù)組、List
、Set
、Map
public class MapBeanUtil { ? ? /** ? ? ?* JavaBean轉(zhuǎn)Map ? ? ?* @param obj ? ? ?* @return ? ? ?*/ ? ? public static Map<String, Object> bean2Map(Object obj) { ? ? ? ? Map<String, Object> map = new LinkedHashMap<>(); ? ? ? ? Class<?> clazz = obj.getClass(); ? ? ? ? for (Field field : clazz.getDeclaredFields()) { ? ? ? ? ? ? field.setAccessible(true); ? ? ? ? ? ? String fieldName = field.getName(); ? ? ? ? ? ? Object value = null; ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? value = field.get(obj); ? ? ? ? ? ? } catch (IllegalAccessException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? ? ? if (value == null){ ? ? ? ? ? ? ? ? value = ""; ? ? ? ? ? ? } ? ? ? ? ? ? map.put(fieldName, value); ? ? ? ? } ? ? ? ? return map; ? ? } ? ? /** ? ? ?* Map轉(zhuǎn)JavaBean ? ? ?* @param clazz ? ? ?* @param map ? ? ?* @param <T> ? ? ?* @return ? ? ?*/ ? ? public static <T> T map2Bean(final Class<T> clazz, final Map<String, ? extends Object> map) { ? ? ? ? if (map == null) { ? ? ? ? ? ? return null; ? ? ? ? } ? ? ? ? T res = null; ? ? ? ? try { ? ? ? ? ? ? res = clazz.getDeclaredConstructor().newInstance(); ? ? ? ? ? ? //獲取到所有屬性,不包括繼承的屬性 ? ? ? ? ? ? Field[] fields = clazz.getDeclaredFields(); ? ? ? ? ? ? for (Field field : fields) { ? ? ? ? ? ? ? ? //獲取字段的修飾符 ? ? ? ? ? ? ? ? int mod = field.getModifiers(); ? ? ? ? ? ? ? ? if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) { ? ? ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //設(shè)置對象的訪問權(quán)限 ? ? ? ? ? ? ? ? field.setAccessible(true); ? ? ? ? ? ? ? ? //根據(jù)屬性名稱去map獲取value ? ? ? ? ? ? ? ? if(map.containsKey(field.getName())) { ? ? ? ? ? ? ? ? ? ? //給對象賦值 ? ? ? ? ? ? ? ? ? ? field.set(res, map.get(field.getName())); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return res; ? ? } ? ? public static void main(String[] args) throws Exception { ? ? ? ? HashMap<String, Object> map = new HashMap<>(); ? ? ? ? map.put("id", 1001); ? ? ? ? map.put("username", "zhangsan"); ? ? ? ? map.put("password", "123456"); ? ? ? ? map.put("nickname", "張三"); ? ? ? ? map.put("email", "369950806@qq.com"); ? ? ? ? map.put("gender", true); ? ? ? ? map.put("birth", LocalDate.now()); ? ? ? ? map.put("avatar", "/aa/bb/ab.jpg"); ? ? ? ? map.put("role", "VIP"); ? ? ? ? map.put("status", (byte) 1); ? ? ? ? map.put("salt", "ldfkasjghweoiq324"); ? ? ? ? map.put("createTime", LocalDateTime.now()); ? ? ? ? map.put("updateTime", LocalDateTime.now()); ? ? ? ? User user = map2Bean(User.class, map); ? ? ? ? System.out.println(user); ? ? ? ? Map<String, Object> res = bean2Map(user); ? ? ? ? System.out.println(map); ? ? } }
User類的代碼:
public class User { ? ? private Integer id; ? ? private String username; ? ? private String password; ? ? private String nickname; ? ? private String email; ? ? private Boolean gender; ? ? private LocalDate birth; ? ? private String avatar; ? ? private String role; ? ? private Byte status; ? ? private String salt; ? ? private LocalDateTime createTime; ? ? private LocalDateTime updateTime; }
到此這篇關(guān)于Map與JavaBean相互轉(zhuǎn)換的工具類 的文章就介紹到這了,更多相關(guān)Map與JavaBean相互轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java.lang.Instrument 代理Agent使用詳細介紹
這篇文章主要介紹了java.lang.Instrument 代理Agent使用詳細介紹的相關(guān)資料,附有實例代碼,幫助大家學習參考,需要的朋友可以參考下2016-11-11Java中SimpleDateFormat日期格式轉(zhuǎn)換詳解及代碼示例
這篇文章主要介紹了Java中SimpleDateFormat日期格式轉(zhuǎn)換詳解及代碼示例,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12Java long 轉(zhuǎn)成 String的實現(xiàn)
這篇文章主要介紹了Java long 轉(zhuǎn)成 String的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09