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’,‘工资’]
时间: 2023-11-19 11:05:56 浏览: 196
可以使用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]}}
```
阅读全文