Files
java-10561/docs/fix_all_localdatetime_fields.sh
2025-09-06 11:58:18 +08:00

62 lines
1.7 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
echo "=== 为所有LocalDateTime字段添加@JsonFormat注解 ==="
echo
# 获取所有包含LocalDateTime的实体类文件
files=$(find src/main/java -path "*/entity/*" -name "*.java" -exec grep -l "private LocalDateTime" {} \;)
for file in $files; do
echo "处理文件: $file"
# 检查是否已经导入JsonFormat
if ! grep -q "import com.fasterxml.jackson.annotation.JsonFormat" "$file"; then
echo " 添加JsonFormat导入..."
# 在import部分添加JsonFormat导入
sed -i '' '/import.*LocalDateTime;/a\
import com.fasterxml.jackson.annotation.JsonFormat;
' "$file"
fi
# 为没有@JsonFormat注解的LocalDateTime字段添加注解
echo " 添加@JsonFormat注解..."
# 创建临时文件
temp_file=$(mktemp)
# 处理文件为LocalDateTime字段添加@JsonFormat注解
awk '
/^[[:space:]]*private LocalDateTime/ {
# 检查前一行是否已经有@JsonFormat注解
if (prev_line !~ /@JsonFormat/) {
# 获取当前行的缩进
match($0, /^[[:space:]]*/)
indent = substr($0, RSTART, RLENGTH)
# 添加@JsonFormat注解
print indent "@JsonFormat(pattern = \"yyyy-MM-dd HH:mm:ss\")"
}
print
next
}
{
prev_line = $0
print
}
' "$file" > "$temp_file"
# 替换原文件
mv "$temp_file" "$file"
echo " 完成处理: $file"
done
echo
echo "=== 批量添加@JsonFormat注解完成 ==="
# 统计处理结果
total_files=$(echo "$files" | wc -l)
echo "总共处理了 $total_files 个实体类文件"
echo
echo "请重启应用程序测试效果"