Commit c239618a by jiangyun

修改

parent 0399f36b
...@@ -6,6 +6,7 @@ import java.util.stream.Collectors; ...@@ -6,6 +6,7 @@ import java.util.stream.Collectors;
import com.zjsgfa.common.utils.DateUtils; import com.zjsgfa.common.utils.DateUtils;
import com.zjsgfa.project.zjsgfa.domain.SjZjyFdxnb; import com.zjsgfa.project.zjsgfa.domain.SjZjyFdxnb;
import com.zjsgfa.project.zjsgfa.util.SymbolReplaceUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.zjsgfa.project.zjsgfa.mapper.SjSggyZjyFdxnbMapper; import com.zjsgfa.project.zjsgfa.mapper.SjSggyZjyFdxnbMapper;
...@@ -186,7 +187,7 @@ public class SjSggyZjyFdxnbServiceImpl implements ISjSggyZjyFdxnbService ...@@ -186,7 +187,7 @@ public class SjSggyZjyFdxnbServiceImpl implements ISjSggyZjyFdxnbService
if(list.size()>0){ if(list.size()>0){
sjSggyZjyFdxnbMapper.deleteSjSggyZjyFdxnbByJh(list.get(0).getJh()); sjSggyZjyFdxnbMapper.deleteSjSggyZjyFdxnbByJh(list.get(0).getJh());
} }
SymbolReplaceUtil.replaceSpecialSymbols(list);
return sjSggyZjyFdxnbMapper.insertSjSggyZjyFdxnbBatch(list); return sjSggyZjyFdxnbMapper.insertSjSggyZjyFdxnbBatch(list);
} }
} }
package com.zjsgfa.project.zjsgfa.util;
import com.zjsgfa.project.zjsgfa.domain.SjSggyZjyFdxnb;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.HtmlUtils;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
/**
* 实体字段特殊符号替换工具类
* 遍历List中实体的所有字段,检测并替换< >(含转义字符)
*/
public class SymbolReplaceUtil {
/**
* 核心方法:遍历List中的实体,替换所有字段中的< >符号(含转义字符)
* @param entityList 待处理的实体List(直接修改原集合中的字段值)
*/
public static void replaceSpecialSymbols(List<SjSggyZjyFdxnb> entityList) {
// 空值校验
if (entityList == null || entityList.isEmpty()) {
return;
}
// 遍历List中的每个实体
for (SjSggyZjyFdxnb entity : entityList) {
if (entity == null) {
continue;
}
// 通过反射获取实体类所有字段(含父类字段)
Field[] fields = getAllFields(entity.getClass());
// 遍历每个字段,检测并替换特殊符号
for (Field field : fields) {
try {
field.setAccessible(true); // 允许访问private字段
Object fieldValue = field.get(entity);
// 只处理字符串类型字段(只有字符串会包含< >符号)
if (fieldValue instanceof String) {
String strValue = (String) fieldValue;
// 先解码HTML转义字符(&lt;-><, &gt;->>),再统一替换
String decodedValue = HtmlUtils.htmlUnescape(strValue);
field.set(entity, decodedValue);
// 判断是否包含< 或 >,包含则替换(自定义替换规则)
// if (decodedValue.contains("<") || decodedValue.contains(">")) {
// // 替换规则1:直接删除< >符号(可根据需求修改)
//// String replacedValue = decodedValue.replace("<", "").replace(">", "");
//
// // 替换规则2:替换为中文符号(示例,按需切换)
// String replacedValue = decodedValue.replace("<", "<").replace(">", ">");
//
// // 将替换后的值写回实体字段(直接修改原集合中的对象)
// field.set(entity, replacedValue);
// }
}
} catch (IllegalAccessException e) {
// 单个字段处理失败不影响其他字段,打印日志即可
System.err.println("处理字段[" + field.getName() + "]时出错:" + e.getMessage());
}
}
}
}
/**
* 反射获取类的所有字段(含父类)
* @param clazz 目标实体类
* @return 所有字段数组
*/
private static Field[] getAllFields(Class<?> clazz) {
List<Field> fieldList = new java.util.ArrayList<>();
// 循环获取当前类及父类字段,直到Object类
while (clazz != null && !clazz.equals(Object.class)) {
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
// 跳过序列化字段(无需处理)
if (!"serialVersionUID".equals(field.getName())) {
fieldList.add(field);
}
}
clazz = clazz.getSuperclass();
}
return fieldList.toArray(new Field[0]);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment