Skip to content

Commit

Permalink
code cheat sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
horaoen committed Oct 12, 2024
1 parent 063bed4 commit 016db7d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
1 change: 0 additions & 1 deletion content/posts/_index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
+++
path = "posts"
template = "posts.html"
transparent = true
sort_by = "date"
Expand Down
80 changes: 80 additions & 0 deletions content/posts/code-cheat-sheet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
+++
title = "code cheat sheet"
date = "2024-10-12"
+++

## Java
### 去除空格
```java
public String trim(String str) {
// "\u0020" 半角空格, office空格, 全角空格
return str.trim().replace("\u00a0", "").replace("\u3000", "");
}
```

## Spring
### 获取Servlet相关参数
```java
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = servletRequestAttributes.getResponse();
HttpServletRequest request = servletRequestAttributes.getRequest();
```
## Library
### Hutool
#### ExcelUtil
```java
ExcelReader reader = ExcelUtil.getReader(FileUtil.file("公务用车车辆管理员信息.xlsx"), 0);
List<Map<String, Object>> carManagerMaps = reader.readAll();
// 所有用车管理员userCode
List<String> carManagersUserCode = carManagerMaps.stream()
.map(map -> map.get("账号").toString())
.collect(Collectors.toList());

final List<LinkedHashMap<String, String>> rows = carManagersUserCode.stream().map(userCode -> {
LinkedHashMap<String, String> row = new LinkedHashMap<>();
row.put("账号", userCode);
return row;
}).collect(Collectors.toList());

ExcelWriter excelWriter = ExcelUtil.getWriter();

excelWriter.renameSheet("所有车辆管理员账号");
excelWriter.write(rows, true);

response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=test_1.xls");
ServletOutputStream out = response.getOutputStream();
excelWriter.flush(out, true);

excelWriter.close();
IoUtil.close(out);
```

```java
ExcelReader reader = ExcelUtil.getReader(FileUtil.file("path"), 0);
```
```
ExcelWriter excelWriter = ExcelUtil.getWriter();
```
## DB
### 时间段重叠问题
```sql
DELIMITER $$
CREATE TRIGGER check_time_overlap
BEFORE INSERT
ON t_meeting_pre_form
FOR EACH ROW
BEGIN
IF EXISTS (SELECT 1
FROM t_meeting_pre_form
WHERE (not (NEW.meeting_time_top >= meeting_time_bottom
or NEW.meeting_time_bottom <= meeting_time_top))
AND NEW.meeting_room_id = meeting_room_id
AND NEW.meeting_date = meeting_date
AND delete_mark = 0) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = '会议室预定时间重叠';
END IF;
END$$
DELIMITER ;
```

0 comments on commit 016db7d

Please sign in to comment.