遇到了一个需求 将excel文件内容 导入到mysql数据库中 要求可乱序 如下图
考虑许久后决定使用树状结构过度 既能保证关系又无关顺序
首先创建结构对象
映射对象
public class Cate {
private Integer cateId;
private Integer parentId;
private String cateName;
private Integer level;
private Byte status = 1;
----------------get/set略----------------
}
树对象
public class CateNode {
private Integer cateId;
private String cateName;
private Integer level;
private Map<String,CateNode> childNodes;
----------------get/set略----------------
}
平面结构转树形结构
----------------文件读取过程略----------------
// 获取最后行号
int lastRowNum = sheet.getLastRowNum();
//记录主键
int cateId = 0;
//创建根节点
CateNode rootNode = new CateNode();
rootNode.setCateId(cateId);
rootNode.setChildNodes(new HashMap<>());
//记录最后节点(临时对象)
CateNode parentNode;
// 循环读取
for (int i = 0; i <= lastRowNum; i++) {
Row row = sheet.getRow(i);
//重置临对象
parentNode = rootNode;
//减少查询(若父节点不存在表明后面都是新节点)
Boolean isNew = Boolean.FALSE;
if (row != null) {
// 获取前四列每一单元格的值
for (int j = 0; j < 4; j++) {
row.getCell(j).setCellType(CellType.STRING);
String cateName = row.getCell(j).getStringCellValue();
int level = j+1;
Map<String,CateNode> brotherNodes = parentNode.getChildNodes();
//父节点不为新节点则查询
if(!isNew){
//存在记录父节点并continue
if(brotherNodes.containsKey(cateName)){
parentNode = brotherNodes.get(cateName);
continue;
}else{
isNew = Boolean.TRUE;
}
}
level = j+1;
CateNode cateNode = new CateNode();
cateNode.setCateId(++cateId);
cateNode.setCateName(cateName);
cateNode.setLevel(level);
//最末级节点不创建children(递归时停止条件:见下节)
if (level!=4) {
cateNode.setChildNodes(new HashMap<>());
}
brotherNodes.put(cateName,cateNode);
//更新临时对象
parentNode = cateNode;
}
}
}
树形结构转关系结构
调用
//树结构转二维结构(递归方法)
List<Cate> cateList = cateTreeToList(rootNode);
----------------对象list入库略----------------
递归
public List<Cate> cateTreeToList(CateNode parentNode){
List<Cate> cateList = new ArrayList<>();
if(null == parentNode.getChildNodes()){
return cateList;
}
for(Map.Entry<String, CateNode> entry : parentNode.getChildNodes().entrySet()){
CateNode cateNode = entry.getValue();
Cate cate = new Cate();
cate.setCateId(cateNode.getCateId());
cate.setParentId(parentNode.getCateId());
cate.setCateName(cateNode.getCateName());
cate.setLevel(cateNode.getLevel());
cateList.add(cate);
//递归调用点
cateList.addAll(cateTreeToList(cateNode));
}
return cateList;
}
注意:由于最深层节点childNodes为null无法进去for循环所以可以停止递归