Skip to content

Commit c0629d4

Browse files
author
litongjava
committed
update TableJsonController
1 parent 5262c7e commit c0629d4

File tree

1 file changed

+5
-77
lines changed
  • docs/zh/15_table-to-json

1 file changed

+5
-77
lines changed

docs/zh/15_table-to-json/08.md

+5-77
Original file line numberDiff line numberDiff line change
@@ -127,80 +127,9 @@ INSERT INTO `system_users` VALUES (118, 'goudan', '$2a$10$Lrb71muL.s5/AFjQ2IHkzO
127127

128128
## 添加工具类
129129

130-
### EasyExcelResponseUtils
130+
### TableJsonController
131131

132132
```
133-
package com.litongjava.tio.boot.admin.utils;
134-
135-
import java.io.ByteArrayOutputStream;
136-
import java.io.IOException;
137-
import java.io.UnsupportedEncodingException;
138-
import java.net.URLEncoder;
139-
import java.util.LinkedHashMap;
140-
import java.util.List;
141-
import java.util.stream.Collectors;
142-
143-
import com.litongjava.data.utils.EasyExcelUtils;
144-
import com.litongjava.jfinal.plugin.activerecord.Record;
145-
import com.litongjava.tio.http.common.HttpRequest;
146-
import com.litongjava.tio.http.common.HttpResponse;
147-
import com.litongjava.tio.http.server.util.Resps;
148-
149-
import lombok.Cleanup;
150-
151-
public class EasyExcelResponseUtils {
152-
public static HttpResponse exportRecords(HttpRequest request, String filename, String sheetName, List<Record> records)
153-
throws IOException {
154-
155-
@Cleanup
156-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
157-
EasyExcelUtils.write(outputStream, sheetName, records);
158-
159-
// 将输出流转换为字节数组
160-
byte[] bytes = outputStream.toByteArray();
161-
162-
// 使用 Resps 工具类创建一个包含二维码图片的响应
163-
HttpResponse response = Resps.bytesWithContentType(request, bytes, "application/vnd.ms-excel;charset=UTF-8");
164-
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
165-
return response;
166-
}
167-
168-
public static HttpResponse exportAllTableRecords(HttpRequest request, String filename,
169-
LinkedHashMap<String, List<Record>> allTableData) throws IOException {
170-
@Cleanup
171-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
172-
EasyExcelUtils.write(outputStream, allTableData);
173-
byte[] bytes = outputStream.toByteArray();
174-
HttpResponse response = Resps.bytesWithContentType(request, bytes, "application/vnd.ms-excel;charset=UTF-8");
175-
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
176-
return response;
177-
}
178-
179-
/**
180-
* 自定义导出
181-
*/
182-
public static <T> HttpResponse export(HttpRequest request, String filename, String sheetName, List<Record> records,
183-
Class<T> clazz) throws UnsupportedEncodingException, IOException {
184-
List<T> exportDatas = records.stream().map(e -> e.toBean(clazz)).collect(Collectors.toList());
185-
186-
@Cleanup
187-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
188-
EasyExcelUtils.write(outputStream, filename, sheetName, clazz, exportDatas);
189-
byte[] bytes = outputStream.toByteArray();
190-
HttpResponse response = Resps.bytesWithContentType(request, bytes, "application/vnd.ms-excel;charset=UTF-8");
191-
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
192-
return response;
193-
}
194-
195-
}
196-
197-
```
198-
199-
## 编写 Controller
200-
201-
```java
202-
package com.litongjava.tio.boot.admin.controller;
203-
204133
import java.io.IOException;
205134
import java.sql.SQLException;
206135
import java.util.LinkedHashMap;
@@ -259,7 +188,7 @@ public class TableJsonController {
259188
260189
log.info("tableName:{},kv:{}", f, kv);
261190
DbJsonBean<List<Record>> list = dbJsonService.list(f, kv);
262-
DbJsonBean<List<Kv>> dbJsonBean = DbJsonBeanUtils.recordsToKv(list);
191+
DbJsonBean<List<Kv>> dbJsonBean = DbJsonBeanUtils.recordsToKv(list, false);
263192
264193
return RespVo.ok(dbJsonBean.getData()).code(dbJsonBean.getCode()).msg(dbJsonBean.getMsg());
265194
}
@@ -268,7 +197,7 @@ public class TableJsonController {
268197
public RespVo listAll(String f) {
269198
log.info("tableName:{}", f);
270199
DbJsonBean<List<Record>> listAll = dbJsonService.listAll(f);
271-
DbJsonBean<List<Kv>> dbJsonBean = DbJsonBeanUtils.recordsToKv(listAll);
200+
DbJsonBean<List<Kv>> dbJsonBean = DbJsonBeanUtils.recordsToKv(listAll, false);
272201
273202
return RespVo.ok(dbJsonBean.getData()).code(dbJsonBean.getCode()).msg(dbJsonBean.getMsg());
274203
}
@@ -284,7 +213,7 @@ public class TableJsonController {
284213
log.info("tableName:{},kv:{}", f, kv);
285214
DbJsonBean<Page<Record>> page = dbJsonService.page(f, kv);
286215
287-
DbJsonBean<DbPage<Kv>> dbJsonBean = DbJsonBeanUtils.pageToDbPage(page);
216+
DbJsonBean<DbPage<Kv>> dbJsonBean = DbJsonBeanUtils.pageToDbPage(page,false);
288217
return RespVo.ok(dbJsonBean.getData()).code(dbJsonBean.getCode()).msg(dbJsonBean.getMsg());
289218
}
290219
@@ -382,7 +311,7 @@ public class TableJsonController {
382311
kv.set("deleted", 1);
383312
384313
log.info("tableName:{},kv:{}", f, kv);
385-
DbJsonBean<DbPage<Kv>> dbJsonBean = DbJsonBeanUtils.pageToDbPage(dbJsonService.page(f, kv));
314+
DbJsonBean<DbPage<Kv>> dbJsonBean = DbJsonBeanUtils.pageToDbPage(dbJsonService.page(f, kv),false);
386315
387316
return RespVo.ok(dbJsonBean.getData()).code(dbJsonBean.getCode()).msg(dbJsonBean.getMsg());
388317
}
@@ -409,7 +338,6 @@ public class TableJsonController {
409338
}
410339
411340
}
412-
413341
```
414342

415343
## 测试 Controller

0 commit comments

Comments
 (0)