这篇文章给大家分享的是有关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转实体类”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。