@@ -127,80 +127,9 @@ INSERT INTO `system_users` VALUES (118, 'goudan', '$2a$10$Lrb71muL.s5/AFjQ2IHkzO
127
127
128
128
## 添加工具类
129
129
130
- ### EasyExcelResponseUtils
130
+ ### TableJsonController
131
131
132
132
```
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
-
204
133
import java.io.IOException;
205
134
import java.sql.SQLException;
206
135
import java.util.LinkedHashMap;
@@ -259,7 +188,7 @@ public class TableJsonController {
259
188
260
189
log.info("tableName:{},kv:{}", f, kv);
261
190
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 );
263
192
264
193
return RespVo.ok(dbJsonBean.getData()).code(dbJsonBean.getCode()).msg(dbJsonBean.getMsg());
265
194
}
@@ -268,7 +197,7 @@ public class TableJsonController {
268
197
public RespVo listAll(String f) {
269
198
log.info("tableName:{}", f);
270
199
DbJsonBean<List<Record>> listAll = dbJsonService.listAll(f);
271
- DbJsonBean<List<Kv > > dbJsonBean = DbJsonBeanUtils . recordsToKv(listAll);
200
+ DbJsonBean<List<Kv>> dbJsonBean = DbJsonBeanUtils.recordsToKv(listAll, false );
272
201
273
202
return RespVo.ok(dbJsonBean.getData()).code(dbJsonBean.getCode()).msg(dbJsonBean.getMsg());
274
203
}
@@ -284,7 +213,7 @@ public class TableJsonController {
284
213
log.info("tableName:{},kv:{}", f, kv);
285
214
DbJsonBean<Page<Record>> page = dbJsonService.page(f, kv);
286
215
287
- DbJsonBean<DbPage<Kv > > dbJsonBean = DbJsonBeanUtils . pageToDbPage(page);
216
+ DbJsonBean<DbPage<Kv>> dbJsonBean = DbJsonBeanUtils.pageToDbPage(page,false );
288
217
return RespVo.ok(dbJsonBean.getData()).code(dbJsonBean.getCode()).msg(dbJsonBean.getMsg());
289
218
}
290
219
@@ -382,7 +311,7 @@ public class TableJsonController {
382
311
kv.set("deleted", 1);
383
312
384
313
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 );
386
315
387
316
return RespVo.ok(dbJsonBean.getData()).code(dbJsonBean.getCode()).msg(dbJsonBean.getMsg());
388
317
}
@@ -409,7 +338,6 @@ public class TableJsonController {
409
338
}
410
339
411
340
}
412
-
413
341
```
414
342
415
343
## 测试 Controller
0 commit comments