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.
43 lines
1.3 KiB
43 lines
1.3 KiB
#!/bin/bash
|
|
|
|
echo "=== 为LocalDateTime字段添加@JsonFormat注解 ==="
|
|
echo
|
|
|
|
# 获取所有包含LocalDateTime的实体类文件
|
|
files=$(find src/main/java -path "*/entity/*" -name "*.java" -exec grep -l "LocalDateTime" {} \;)
|
|
|
|
for file in $files; do
|
|
echo "处理文件: $file"
|
|
|
|
# 检查是否已经导入JsonFormat
|
|
if ! grep -q "import com.fasterxml.jackson.annotation.JsonFormat" "$file"; then
|
|
echo " 添加JsonFormat导入..."
|
|
# 在LocalDateTime导入后添加JsonFormat导入
|
|
sed -i '' '/import java\.time\.LocalDateTime;/a\
|
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
' "$file"
|
|
fi
|
|
|
|
# 为LocalDateTime字段添加@JsonFormat注解
|
|
echo " 添加@JsonFormat注解..."
|
|
|
|
# 处理各种时间字段模式
|
|
sed -i '' '/private LocalDateTime.*Time;/i\
|
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
|
' "$file"
|
|
|
|
# 移除重复的注解(如果存在)
|
|
awk '
|
|
/^[[:space:]]*@JsonFormat\(pattern = "yyyy-MM-dd HH:mm:ss"\)/ {
|
|
if (prev_line == $0) next
|
|
prev_line = $0
|
|
}
|
|
{ print }
|
|
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
|
|
|
|
echo " 完成处理: $file"
|
|
done
|
|
|
|
echo
|
|
echo "=== 批量添加@JsonFormat注解完成 ==="
|
|
echo "请重启应用程序测试效果"
|