当前位置: 代码迷 >> 综合 >> 2022-05-19 对象转map
  详细解决方案

2022-05-19 对象转map

热度:80   发布时间:2023-09-11 01:55:49.0

开发中,经常会遇到对象转map 今天发现了一个工具类,来试试吧

1 pom文件中引入 坐标

<dependency><groupId>cn.jimmyshi</groupId><artifactId>bean-query</artifactId><version>1.1.5</version></dependency>

2 使用 demo

public static void main(String[] args) {BasicAdministrativeDivision basicAdministrativeDivision = new BasicAdministrativeDivision();basicAdministrativeDivision.setCode("123");basicAdministrativeDivision.setId("111");basicAdministrativeDivision.setLevel(2);BasicAdministrativeDivision basicAdministrativeDivision1 = new BasicAdministrativeDivision();basicAdministrativeDivision1.setCode("188823");basicAdministrativeDivision1.setId("111888");basicAdministrativeDivision1.setLevel(2888);ArrayList<BasicAdministrativeDivision> basicAdministrativeDivisions = CollectionUtil.newArrayList(basicAdministrativeDivision, basicAdministrativeDivision1);Map<String, Object> stringObjectMap = BeanQuery.select("code as code1", "id as id1", "level as LEVEL").executeFrom(basicAdministrativeDivision);List<Map<String, Object>> maps = BeanQuery.select("code as code1", "id as id1", "level as LEVEL").executeFrom(basicAdministrativeDivisions);System.out.println(JSONUtil.toJsonPrettyStr(stringObjectMap));System.out.println(JSONUtil.toJsonPrettyStr(maps));}

效果如图

2022-05-19 对象转map
image.png