小程序开发-服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

61 lines
1.7 KiB

#!/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 "请重启应用程序测试效果"