Java8环境下如何将实体组定义文本解析映射到指定Java类对象
Java8解析EntityGroup关联关系实现方案
注:你提供的存储类
Realationship存在拼写错误,下文代码默认已将类名修正为Relationship,保持和RelationshipColumns命名规范一致。
实现步骤
- 读取整个文本文件内容,按分号
;拆分得到每个独立的EntityGroup定义块 - 对每个定义块使用正则匹配提取核心字段:EntityGroup名称、父表名、子表名、关联字段对集合
- 拆分关联字段对,逐个封装为
RelationshipColumns对象 - 所有信息组装为
Relationship对象,汇总后返回
补充实体类构造方法
为了简化赋值,先给RelationshipColumns补充全参构造方法:
package com.test.common; public class RelationshipColumns { String parentColumn; String childColumn; public RelationshipColumns(String parentColumn, String childColumn) { this.parentColumn = parentColumn; this.childColumn = childColumn; } // 原有getter、setter保持不变 }
解析工具类代码
package com.test.util; import com.test.common.Relationship; import com.test.common.RelationshipColumns; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class EntityGroupParser { // 匹配单个EntityGroup块的正则,兼容EntityParent/EntityChild顺序不固定的场景 private static final Pattern EG_PATTERN = Pattern.compile( "CREATE EntityGroup\\s+(\\w+)\\s+" + "(?:EntityChild\\s+(\\w+\\.\\w+)|EntityParent\\s+(\\w+\\.\\w+))\\s+" + "(?:EntityChild\\s+(\\w+\\.\\w+)|EntityParent\\s+(\\w+\\.\\w+))\\s+" + "SCHEMA\\s+\\w+\\s+\\(([^)]+)\\)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL ); // 匹配单组关联字段的正则 private static final Pattern COL_PATTERN = Pattern.compile("\\s*(\\w+)\\s*=\\s*(\\w+)\\s*"); public static List<Relationship> parseFile(String filePath) throws IOException { // 读取整个文件内容 String content = Files.lines(Paths.get(filePath)) .collect(Collectors.joining("\n")); List<Relationship> result = new ArrayList<>(); // 按分号拆分每个EntityGroup定义 String[] egBlocks = content.split(";"); for (String block : egBlocks) { block = block.trim(); if (block.isEmpty()) { continue; } Matcher egMatcher = EG_PATTERN.matcher(block); if (egMatcher.find()) { Relationship relationship = new Relationship(); // 赋值EntityGroup名称 relationship.setEntityGroup(egMatcher.group(1)); // 识别父表、子表 String childTable = null, parentTable = null; if (block.contains("EntityChild " + egMatcher.group(2))) { childTable = egMatcher.group(2); parentTable = egMatcher.group(5); } else { parentTable = egMatcher.group(3); childTable = egMatcher.group(4); } relationship.setChildTable(childTable); relationship.setParentTable(parentTable); // 解析关联字段 String colStr = egMatcher.group(6); ArrayList<RelationshipColumns> cols = new ArrayList<>(); Matcher colMatcher = COL_PATTERN.matcher(colStr); while (colMatcher.find()) { cols.add(new RelationshipColumns(colMatcher.group(1), colMatcher.group(2))); } relationship.setCols(cols); // 示例中无creator字段对应值,可根据实际业务补充赋值逻辑 relationship.setCreator(null); result.add(relationship); } } return result; } // 测试调用 public static void main(String[] args) throws IOException { List<Relationship> relationships = parseFile("你的文件路径.txt"); // 验证输出 for (Relationship rel : relationships) { System.out.println("EntityGroup:" + rel.getEntityGroup()); System.out.println("父表:" + rel.getParentTable()); System.out.println("子表:" + rel.getChildTable()); System.out.println("关联字段数:" + rel.getCols().size()); System.out.println("-------------------"); } } }
注意事项
- 正则自动忽略字段前后的空格、换行,支持单个和多个关联字段的解析
- 单个文件最多100个EntityGroup的场景完全可以覆盖,性能无问题
内容的提问来源于stack exchange,提问作者Subrahmanya Kudumaru
相关产品推荐
相关产品推荐

