2025年4月工作经验记录
1 Git放弃所有更改
1 2
| git restore . git clean -fd
|
- 放弃所有暂存区和工作区的更改:已经add但是没有commit
1 2
| git reset --hard git clean -fd
|
1 2 3
| git fetch origin git reset --hard origin/你的分支名 git clean -fd
|
2 pm2
PM2(Process Manager 2
) 是一个非常流行的 Node.js 应用进程管理器,主要用于生产环境中对 Node.js 应用进行进程守护、负载均衡、日志管理等功能的管理工具。
可以为应用命名:
1
| pm2 start app.js --name my-app
|
也可以指定一些参数:
1 2
| pm2 start app.js --watch pm2 start app.js -i max
|
显示所有 PM2 管理的进程信息,包括名称、状态、内存占用、CPU 使用率等。
1 2 3
| pm2 stop my-app pm2 restart my-app pm2 delete my-app
|
配置文件:可以创建 ecosystem.config.js
来管理多个项目,示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| module.exports = { apps: [ { name: 'my-app', script: './app.js', instances: 2, exec_mode: 'cluster', watch: true, max_memory_restart: '300M', env: { NODE_ENV: 'development', PORT: 3000 }, env_production: { NODE_ENV: 'production', PORT: 8080 } } ] }
|
1 2
| pm2 start ecosystem.config.js pm2 start ecosystem.config.js --env production
|
3 ResultMap
在 MyBatis 中,**ResultMap
用于将数据库中的字段映射到 Java 实体类的属性上**。
默认情况下,MyBatis 会根据 字段名和属性名一致 来自动映射但如果数据库字段和 Java 类属性不完全对应,比如:
数据库字段名 |
Java属性名 |
threshold_distance |
thresholdDistance |
preset_status |
status |
在这种情况下,通常有几个方法来解决:
- 开启驼峰映射:适用于数据库字段是下划线,Java实体类字段是驼峰
1 2 3
| mybatis-plus: configuration: map-underscore-to-camel-case: true
|
- 使用
@TableField
注解:Java实体类字段采用一些缩写等形式
1 2
| @TableField("internet_address") private String ia;
|
作用:将 Java 中的 javaFieldName
字段,映射到数据库中的 db_column_name
字段。
1 2 3 4 5 6 7 8 9
| <resultMap id="AirportPresetPointMap" type="com.xxx.AirportPresetPoint"> <id column="id" property="id"/> <result column="preset_status" property="status"/> <result column="threshold_distance" property="thresholdDistance"/> </resultMap>
<select id="selectById" resultMap="AirportPresetPointMap"> SELECT * FROM airport_preset_point WHERE id = #{id} </select>
|
4 lambda和effectively final
遇到了这样的问题:
1 2 3 4 5 6 7 8
| String craftSite = "545";
craftSite = "432";
int index = IntStream.range(0, craftSites.length) .filter(i -> craftSite.equals(craftSites[i])) .findFirst() .orElse(-1);
|
上面的代码,在craftSite处报错:Variable used in lambda expression should be final or effectively final
。
effectively final
是 Java 8 引入的新概念,意思是:一个变量虽然没加 final
,但从声明之后就没再修改过,那它就是 effectively final,也能在 lambda 中使用。
原因:在 Java 中,lambda 表达式里引用的变量必须是 不可变的或实际上未改变的。
解决方式:提取为 final 局部变量再用
1 2 3 4 5 6 7 8 9
| String craftSite = "545";
craftSite = "432";
final String targetCraftSite = craftSite; int index = IntStream.range(0, craftSites.length) .filter(i -> targetCraftSite.equals(craftSites[i])) .findFirst() .orElse(-1);
|
5 前端响应类型和后端返回的数据结构不一致
遇到了这样的问题,前端的统一响应类型为:
1 2 3 4 5 6 7 8 9 10 11 12
|
export interface Result { code: string; message: string; }
export interface ResultData<T = any> extends Result { data?: T; }
|
但是和后端对接时,后端返回的数据结构为:
1 2 3 4 5 6
| { "code": "0", "msg": "success", "data": "success", "count": "0" }
|
这样在前端请求时:
1 2 3 4 5 6 7 8
| export const getRtspUrlApi = cameraCode => { return http.get<any>(`http://ip:port/device/rtsp?cameraCode=${cameraCode}`); };
get<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> { return this.service.get(url, { params, ..._object }); }
|
会报错,无任何报错信息,并且后续代码无法执行,如下图:

在无法修改后端返回的数据结构的情况下(可能因为后端的同事不想修改或不好修改,或者这套后端并不和现在的前端适配),则使用fetch
进行单独处理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| export const getRtspUrlApi = cameraCode => { return fetch(`http://ip:port/device/rtsp?cameraCode=${cameraCode}`, { method: 'GET', }).then(async (response) => { const res = await response.json();
if (res.code === '0') { const parsed = JSON.parse(res.data); return parsed.rtspUrl; } else { throw new Error(res.msg || '接口返回错误'); } }).catch((err) => { throw err; }); }
|
6
著作権表示: 此文章版权归Kisugi Takumi所有,如有转载,请注明来自原作者