您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Java怎么實現實體類轉Map、Map轉實體類的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
package com.jeff.entity; public class User { private String userName; private String password; private Integer age; public User() { super(); } public User(String userName, String password, Integer age) { super(); this.userName = userName; this.password = password; this.age = age; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [userName=" + userName + ", password=" + password + ", age=" + age + "]"; } }
package com.jeff.utils; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class EntityUtils { /** * * @description: 實體類轉Map * @author: Jeff * @date: 2019年10月29日 * @param object * @return */ public static Map<String, Object> entityToMap(Object object) { Map<String, Object> map = new HashMap<>(); for (Field field : object.getClass().getDeclaredFields()) { try { boolean flag = field.isAccessible(); field.setAccessible(true); Object o = field.get(object); map.put(field.getName(), o); field.setAccessible(flag); } catch (Exception e) { e.printStackTrace(); } } return map; } /** * * @description: Map轉實體類 * @author: Jeff * @date: 2019年10月29日 * @param <T> * @param map 需要初始化的數據,key字段必須與實體類的成員名字一樣,否則賦值為空 * @param entity 需要轉化成的實體類 * @return */ public static <T> T mapToEntity(Map<String, Object> map, Class<T> entity) { T t = null; try { t = entity.newInstance(); for (Field field : entity.getDeclaredFields()) { if (map.containsKey(field.getName())) { boolean flag = field.isAccessible(); field.setAccessible(true); Object object = map.get(field.getName()); if (object != null && field.getType().isAssignableFrom(object.getClass())) { field.set(t, object); } field.setAccessible(flag); } } return t; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return t; } }
package com.jeff; import java.util.HashMap; import java.util.Map; import com.jeff.entity.User; import com.jeff.utils.EntityUtils; public class TestCopyObj3 { public static void main(String[] args) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); map.put("userName", "Jeff"); map.put("password", "123456"); map.put("age", 18); System.out.println("Map對象:" + map); User user = EntityUtils.mapToEntity(map, User.class); System.out.println("User對象:" + user); } }
package com.jeff; import java.util.Map; import com.jeff.entity.User; import com.jeff.utils.EntityUtils; public class TestCopyObj4 { public static void main(String[] args) throws Exception { User user = new User("Jeff", "123456", 18); System.out.println("User對象:" + user); Map<String, Object> map = EntityUtils.entityToMap(user); System.out.println("Map對象:" + map); } }
因為做項目,需要取出復雜的map里的value轉為實體類對象和List,在博客里找了很久沒有找到合適的方法,于是只能默默探索。。。
map里包含了實體類,list數組,還有普通的key-value,取出我們想要的數據呢?直接取我是取不出來,后來想先轉json,在從json里去出對象和list,然后就這么干,廢話少說,擼代碼!??!
<!--alibaba json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency>
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.List; import java.util.Map; public class JsonUtil<T>{ public static JSONObject mapToJson(Map<String, Object> map) { String data = JSON.toJSONString(map); return JSON.parseObject(data); } /** * map中取key對應的value * @param map * @param key * @return */ public String mapToString(Map<String, Object> map, String key) { JSONObject jsonObject = mapToJson(map); return jsonObject.getString(key); } /** * map中取類對象 * @param map * @param clazz * @param key * @param <T> * @return */ public static <T> T mapToObject(Map<String, Object> map, Class<T> clazz, String key) { T t = null; JSONObject jsonObject = mapToJson(map); JSONObject object = jsonObject.getJSONObject(key); t = object.toJavaObject(clazz); return t; } /** * map中取list * @param map * @param clazz * @param key * @return */ public List<T> mapToList(Map<String, Object> map, Class<T> clazz, String key) { List<T> t = null; JSONObject jsonObject = mapToJson(map); JSONArray array = jsonObject.getJSONArray(key); t = array.toJavaList(clazz); return t; } }
@RequestMapping(value = "/subjects/subject", method = {RequestMethod.POST}) public Result addSubject(@RequestBody Map<String, Object> map) { //使用json JsonUtil jsonUtil = new JsonUtil(); //獲取Subject對象 Subject subject = jsonUtil.mapToObject(map, Subject.class, "subject"); //獲取List數組 List<Progress> progressList = jsonUtil.mapToList(map, Progress.class, "progress"); //獲取key對應的值 String specid = jsonUtil.mapToString(map, "specid"); //打印輸出 System.out.println(subject); for(Progress progress : progressList) { System.out.println(progress); } System.out.println(specid); return null; }
感謝各位的閱讀!關于“Java怎么實現實體類轉Map、Map轉實體類”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。