Map<Object, Object> datamap = new HashMap<>();for (int j = 0; j < typeList.size(); j++) { String typeName = typeList.get(j).getDsmType(); List<Object> dataList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { Map<Object, Object> dataMap = new HashMap<>(); dataMap.put("driverName", list.get(i).getDriverName()); dataMap.put("fatigue", list.get(i).getFatigue()); dataMap.put("callPhone", list.get(i).getCallPhone()); dataMap.put("smoke", list.get(i).getSmoke()); dataMap.put("overSpeed", list.get(i).getOverSpeed()); dataMap.put("overWeight", list.get(i).getOverWeight()); dataMap.put("overHeight", list.get(i).getOverHeight()); dataMap.put("noSeatBelt", list.get(i).getNoSeatBelt()); dataList.add(dataMap); } datamap.put(typeName, dataList);}
时间: 2024-04-04 12:34:36 浏览: 69
这段代码创建了一个HashMap,用于存储类型名称和每个类型对应的数据列表。数据列表中包含了每个数据对象的属性,如驾驶员名称、疲劳程度、是否接打电话、是否吸烟、是否超速、是否超载、是否超高、是否未系安全带等等。这个HashMap可以用于方便地管理和访问这些数据,并且可以根据类型名称进行分类、统计和分析。
相关问题
Map<Object, Object> datamap = new HashMap<>(); for (int j = 0; j < typeList.size(); j++) { typeName[j]=typeList.get(j).getDsmType(); name=typeList.get(j).getDsmType(); Map<Object, Object> map = new HashMap<>(); ArrayList<Object> data = new ArrayList<Object>(); for (int i = 0; i < list.size(); i++) { driverName[i]=list.get(i).getDriverName(); fatigue[i]=list.get(i).getFatigue(); callPhone[i]=list.get(i).getCallPhone(); smoke[i]=list.get(i).getSmoke(); overSpeed[i]=list.get(i).getOverSpeed(); overWeight[i]=list.get(i).getOverWeight(); overHeight[i]=list.get(i).getOverHeight(); noSeatBelt[i]=list.get(i).getNoSeatBelt(); data.add(fatigue); data.add(callPhone); data.add(smoke); data.add(overSpeed); data.add(overWeight); data.add(overHeight); data.add(noSeatBelt); } map.put("name",name); map.put("data",data); dataList.add(map); }
这段代码看起来像是将一些数据存储到一个 Map 中,其中 key 为一个 Object,value 也是一个 Object。具体来说,代码中首先创建了一个 Map 对象 datamap,然后使用一个 for 循环遍历 typeList,其中 typeName[j] 被赋值为 typeList 中第 j 个元素的 dsmType 属性,同时 name 被赋值为同样的属性值。接着,代码创建了一个新的 Map 对象 map,并创建了一个 ArrayList 对象 data。这里的 for 循环遍历了 list,并将 list 中每个元素的 driverName、fatigue、callPhone 等属性值添加到 data 中。最后,代码将 name 和 data 存储到 map 中,并将 map 存储到 dataList 中。整个过程重复了 typeList 的长度次数,最终得到了一个包含多个 Map 对象的 dataList。
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; DROP TABLE IF EXISTS department; CREATE TABLE department ( id int(0) NOT NULL AUTO_INCREMENT COMMENT '主键', name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '部门名称', PRIMARY KEY (id) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '部门表' ROW_FORMAT = Dynamic; INSERT INTO department VALUES (1, '软件部'); INSERT INTO department VALUES (2, '销售部'); INSERT INTO department VALUES (3, '市场部'); DROP TABLE IF EXISTS employee; CREATE TABLE employee ( id int(0) NOT NULL AUTO_INCREMENT COMMENT '主键', name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '人员名称', salary int(0) NULL DEFAULT NULL COMMENT '工资', departmentId int(0) NULL DEFAULT NULL COMMENT '部门id(外键)', PRIMARY KEY (id) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '人员表' ROW_FORMAT = Dynamic; INSERT INTO employee VALUES (1, '张三', 90000, 1); INSERT INTO employee VALUES (2, '李四', 80000, 2); INSERT INTO employee VALUES (3, '王五', 75000, 3); INSERT INTO employee VALUES (4, '老六', 90000, 1); SET FOREIGN_KEY_CHECKS = 1; 4.用Mybatis读取表数据,拼接指定JSON格式并打印在控制台。 “data”: { “张三-软件部”:[1, 1, 90000], “李四-销售部”:[2, 2, 80000], “王五-市场部”:[3, 3, 75000], “老六-软件部”:[4, 1, 90000] } 解释:“人员名 - 部门名”:[‘人员id’,‘部门id’,‘工资’]
可以使用Mybatis的SQL语句和结果映射来实现该功能,具体步骤如下:
1. 编写查询语句,使用SQL JOIN查询获取employee和department表中的数据,并拼接成指定格式的JSON字符串。
```xml
<!-- 编写查询语句 -->
<select id="getEmployeeData" resultType="map">
SELECT CONCAT(e.name, '-', d.name) AS name,
e.id AS employeeId,
d.id AS departmentId,
e.salary AS salary
FROM employee e JOIN department d ON e.departmentId = d.id
</select>
```
2. 将查询结果映射为一个Map对象,其中键为人员名 - 部门名,值为包含人员id、部门id和工资的列表。
```xml
<!-- 映射查询结果 -->
<resultMap id="employeeDataMap" type="java.util.HashMap">
<id property="name" column="name" />
<result property="value" resultMap="employeeValueMap" />
</resultMap>
<resultMap id="employeeValueMap" type="java.util.ArrayList">
<result property="employeeId" column="employeeId" />
<result property="departmentId" column="departmentId" />
<result property="salary" column="salary" />
</resultMap>
```
3. 编写Java代码调用Mybatis执行查询,并将结果拼接为指定格式的JSON字符串。
```java
// 获取SqlSession对象
SqlSession sqlSession = sessionFactory.openSession();
try {
// 执行查询语句
List<Map<String, Object>> employeeDataList = sqlSession.selectList("getEmployeeData");
// 创建Map对象,将查询结果映射为指定的JSON格式
Map<String, List<Integer>> dataMap = new HashMap<>();
for (Map<String, Object> employeeData : employeeDataList) {
String name = (String) employeeData.get("name");
int employeeId = (int) employeeData.get("employeeId");
int departmentId = (int) employeeData.get("departmentId");
int salary = (int) employeeData.get("salary");
List<Integer> valueList = new ArrayList<>();
valueList.add(employeeId);
valueList.add(departmentId);
valueList.add(salary);
dataMap.put(name, valueList);
}
// 输出结果
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("data", dataMap);
System.out.println(new JSONObject(resultMap).toString());
} finally {
sqlSession.close();
}
```
输出结果如下:
```json
{"data":{"张三-软件部":[1,1,90000],"李四-销售部":[2,2,80000],"王五-市场部":[3,3,75000],"老六-软件部":[4,1,90000]}}
```
阅读全文