提交 0be29099 authored 作者: 黄夏豪's avatar 黄夏豪

[数据模型] 更新了聚合对象生成的方法,现在支持多层嵌套的聚合对象了

上级 1c97e5f0
package com.tykj.workflowcore.model_layer.controller; package com.tykj.workflowcore.model_layer.controller;
import com.tykj.workflowcore.base.result.ResultObj;
import com.tykj.workflowcore.base.result.ResultUtil; import com.tykj.workflowcore.base.result.ResultUtil;
import com.tykj.workflowcore.model_layer.dao.AggregationDao;
import com.tykj.workflowcore.model_layer.entity.Aggregation;
import com.tykj.workflowcore.model_layer.entity.vo.AggregationVO; import com.tykj.workflowcore.model_layer.entity.vo.AggregationVO;
import com.tykj.workflowcore.model_layer.service.AggregationService; import com.tykj.workflowcore.model_layer.service.AggregationService;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
/** /**
* @ClassName AggregationController * @ClassName AggregationController
* @Description TODO * @Description TODO
...@@ -19,14 +28,55 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -19,14 +28,55 @@ import org.springframework.web.bind.annotation.RestController;
*/ */
@RestController @RestController
@RequestMapping("/aggregation")
public class AggregationController { public class AggregationController {
@Autowired @Autowired
AggregationService aggregationService; AggregationService aggregationService;
@PostMapping("aggregation/add") @Autowired
AggregationDao aggregationDao;
@PostMapping("/add")
public ResponseEntity add(@RequestBody AggregationVO aggregationVO) { public ResponseEntity add(@RequestBody AggregationVO aggregationVO) {
aggregationService.addAggregation(aggregationVO); aggregationService.addAggregation(aggregationVO);
return ResultUtil.success("","保存成功!"); return ResultUtil.success("","保存成功!");
} }
@PostMapping("/test")
public ResponseEntity test(@RequestBody AggregationVO AggregationVO) {
String result= "";
Document document = DocumentHelper.createDocument();
Element rootElement = DocumentHelper.createElement("rootElement");
String s = testDG(AggregationVO.getAggregations(), null, 1, rootElement).asXML();
document.add(rootElement);
return ResultUtil.success(aggregationDao.findById(3),"保存成功!");
}
//假设获得的是一份XML
public Element testDG(List<Aggregation> aggregations, Aggregation parentAggregation, Integer mainId, Element element){
//循环
Element elementCLass = DocumentHelper.createElement("class");
if (parentAggregation!=null){
elementCLass.addAttribute("name",parentAggregation.getSideTableId()+"_");
}else {
elementCLass.addAttribute("name",mainId+"_");
}
for (int i = 0; i < aggregations.size(); i++) {
Aggregation aggregationChild = aggregations.get(i);
if (aggregationChild.getAggregations()!=null&&aggregationChild.getAggregations().size()>0){
Element elementClassChild = testDG(aggregationChild.getAggregations(), aggregationChild, mainId, element);
Element elementSet = DocumentHelper.createElement("set");
elementSet.addAttribute("setName","c"+elementClassChild.attribute("name").getValue());
elementCLass.add(elementSet);
}else {
Element elementSet = DocumentHelper.createElement("set");
elementSet.addAttribute("setName","c"+aggregationChild.getSideTableId());
elementCLass.add(elementSet);
}
}
element.add(elementCLass);
return elementCLass;
}
} }
...@@ -8,7 +8,8 @@ import lombok.NoArgsConstructor; ...@@ -8,7 +8,8 @@ import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
import javax.persistence.Entity; import javax.persistence.*;
import java.util.List;
/** /**
* @ClassName AggregationRelationship * @ClassName AggregationRelationship
...@@ -42,4 +43,13 @@ public class Aggregation extends BaseEntity { ...@@ -42,4 +43,13 @@ public class Aggregation extends BaseEntity {
@ApiModelProperty("中间表名称") @ApiModelProperty("中间表名称")
private String connectionTableName; private String connectionTableName;
@ApiModelProperty("多层嵌套时会生成虚拟对象class 该class为了不影响其他真实对象所以用uuid命名")
private String uuidTableName;
private Integer parentId;
@OneToMany(cascade = CascadeType.ALL) //表示级练操作
@JoinColumn(name = "parentId")
List<Aggregation> aggregations;
} }
...@@ -24,6 +24,9 @@ public class AggregationVO { ...@@ -24,6 +24,9 @@ public class AggregationVO {
@ApiModelProperty("名称") @ApiModelProperty("名称")
private String name; private String name;
@ApiModelProperty("描述")
private String chineseName;
@ApiModelProperty("描述") @ApiModelProperty("描述")
private String description; private String description;
...@@ -31,7 +34,7 @@ public class AggregationVO { ...@@ -31,7 +34,7 @@ public class AggregationVO {
private Integer mainTableId; private Integer mainTableId;
@ApiModelProperty("关系集合") @ApiModelProperty("关系集合")
private List<Aggregation> lists; private List<Aggregation> aggregations;
public TableInfoEx getTableInfoEx(){ public TableInfoEx getTableInfoEx(){
TableInfoEx tableInfoEx = new TableInfoEx(); TableInfoEx tableInfoEx = new TableInfoEx();
......
...@@ -256,7 +256,7 @@ public class ModelImpl implements ModelService { ...@@ -256,7 +256,7 @@ public class ModelImpl implements ModelService {
map.put(propertyNames[i], value); map.put(propertyNames[i], value);
} }
} }
newSession.save(tableName, map); newSession.saveOrUpdate(tableName, map);
newSession.getTransaction().begin(); newSession.getTransaction().begin();
newSession.getTransaction().commit(); newSession.getTransaction().commit();
newSession.close(); newSession.close();
...@@ -307,8 +307,8 @@ public class ModelImpl implements ModelService { ...@@ -307,8 +307,8 @@ public class ModelImpl implements ModelService {
columnVO.setPrimaryKey(0); columnVO.setPrimaryKey(0);
} }
columnVO.setFieldType(getTypeName(genericType.toString())); columnVO.setFieldType(getTypeName(genericType.toString()));
columnVO.setFieldName(getClassName(declaredField.toString())); columnVO.setFieldName(getClassName(declaredField.toString()));
columnVO.setFieldLength(setLength(genericType.toString()));
//获得属性中文描述 //获得属性中文描述
if (declaredField.isAnnotationPresent(ApiModelProperty.class)) { if (declaredField.isAnnotationPresent(ApiModelProperty.class)) {
ApiModelProperty annotation = declaredField.getAnnotation(ApiModelProperty.class); ApiModelProperty annotation = declaredField.getAnnotation(ApiModelProperty.class);
...@@ -340,7 +340,7 @@ public class ModelImpl implements ModelService { ...@@ -340,7 +340,7 @@ public class ModelImpl implements ModelService {
ColumnInfo columnInfo = new ColumnInfo(); ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setFieldName(columnVO.getFieldName()); columnInfo.setFieldName(columnVO.getFieldName());
columnInfo.setFieldType(columnVO.getFieldType()); columnInfo.setFieldType(columnVO.getFieldType());
columnInfo.setFieldLength(setLength(genericType.toString())); columnInfo.setFieldLength(columnVO.getFieldLength());
columnInfo.setFieldTitle(columnVO.getFieldTitle()); columnInfo.setFieldTitle(columnVO.getFieldTitle());
columnInfo.setPrimaryKey(columnVO.getPrimaryKey()); columnInfo.setPrimaryKey(columnVO.getPrimaryKey());
......
...@@ -6,8 +6,7 @@ import org.dom4j.Document; ...@@ -6,8 +6,7 @@ import org.dom4j.Document;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
import org.dom4j.Element; import org.dom4j.Element;
import org.springframework.util.StringUtils;
/** /**
...@@ -63,14 +62,54 @@ public class AggregationUtil { ...@@ -63,14 +62,54 @@ public class AggregationUtil {
return document; return document;
} }
public static Element createOneToOneElement(String name,String connectionKey,String sideTableName ) {
Element element = DocumentHelper.createElement("one-to-one");
element.addAttribute("name", name);
element.addAttribute("property-ref", connectionKey);
element.addAttribute("class", sideTableName);
element.addAttribute("cascade", "save-update");
return element;
}
public static Element createOneToManyElement(String name,
String sideTableName,
String sideTableConnectionKey) {
//创建ELEMETN
Element setElement = createSetElement(
name,
sideTableName,
"",
"",
sideTableConnectionKey,
AggregationType.ONE_TO_MANY);
return setElement;
}
public static Element createManyToManyElement(String sideTableName,String uuidTableName,String connectionTableName,String mainTableConnectionKey, String sideTableConnectionKey) {
Element setElement = createSetElement(sideTableName,
StringUtils.isEmpty(uuidTableName)?sideTableName:uuidTableName,
connectionTableName,
mainTableConnectionKey,
sideTableConnectionKey,
AggregationType.MANY_TO_MANY);
return setElement;
}
public static Element getNode(Document document) { public static Element getNode(Document document) {
return document.getRootElement().element("class"); return document.getRootElement().element("class");
} }
public static Element createSetElement(String setName,String sideTableName,String connectionTableName,String mainTableConnectionKey, String sideTableConnectionKey,Integer type){ public static Element createSetElement(String name,
//创建ELEMTN String sideTableName,
String connectionTableName,
String mainTableConnectionKey,
String sideTableConnectionKey,
Integer type){
//创建element
Element elementSet = DocumentHelper.createElement("set"); Element elementSet = DocumentHelper.createElement("set");
elementSet.addAttribute("name", setName); elementSet.addAttribute("name", name);
elementSet.addAttribute("cascade", "save-update"); elementSet.addAttribute("cascade", "save-update");
Element elementKey = DocumentHelper.createElement("key"); Element elementKey = DocumentHelper.createElement("key");
if (type.equals(AggregationType.MANY_TO_MANY)){ if (type.equals(AggregationType.MANY_TO_MANY)){
...@@ -92,69 +131,6 @@ public class AggregationUtil { ...@@ -92,69 +131,6 @@ public class AggregationUtil {
return elementSet; return elementSet;
} }
// 字符串拼接
// private static String xmlStart = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
// "<!DOCTYPE hibernate-mapping PUBLIC\n" +
// " \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\"\n" +
// " \"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd\">\n" +
// "<hibernate-mapping>";
//
// private static String xmlEnd = "</hibernate-mapping>";
//
//
// public static String getOldProperty(String xml) {
// String property = "";
// Document document = null;
// try {
// document = DocumentHelper.parseText(xml);
// } catch (DocumentException e) {
// e.printStackTrace();
// }
// List<Element> elements = document.getRootElement().elements().get(0).elements();
// for (Element element : elements) {
// property = property + element.asXML();
// }
// return property;
// }
//
//
//
//
// public static String addOneToMany(String mainTable, String sideTable, String mainField, String xml) {
//
//
// String addStr =
// "<class entity-name= \"" +mainTable + "_" + sideTable + "\""+" table=\"" + mainTable +"\""+ ">\n" +
// getOldProperty(xml) +
// " <set name=\"" + sideTable + "_list" + "\""+" inverse=\"true\" lazy=\"true\">\n" +
// " <key column=\"" + mainField + "\""+"/>\n" +
// " <one-to-many class=\"" + sideTable +"\""+ "/>\n" +
// "</set>\n" +
// " </class>";
//
//
// return xmlStart+ addStr+xmlEnd;
// }
//
//
// public static String addManyToMany(String mainTable, String sideTable, String xml) {
//
// String entityName = mainTable + "_" + sideTable + "_ref";
//
// String addStr = "<class entity-name=\"" + entityName +"\""+ " table=\"" + mainTable +"\""+ ">\n" +
// " \n"
// +
// getOldProperty(xml)
// +
// " <set name=\"" + sideTable+"\"" + " table=\"" + entityName+"\"" + ">\n" +
// " \t<key column=\"" + mainTable + "_id" +"\""+ "/>\n" +
// " \t<many-to-many class=\"" + sideTable +"\""+ " column=\"" + sideTable +"\""+ "_id" + "/>\n" +
// " </set>" +
// "</class>";
// return xmlStart+ addStr+xmlEnd;
// }
//
......
...@@ -37,9 +37,7 @@ public class CreateTableUtil { ...@@ -37,9 +37,7 @@ public class CreateTableUtil {
} }
xmlMapping += " </class>\n" + xmlMapping += " </class>\n" +
"</hibernate-mapping>"; "</hibernate-mapping>";
return xmlMapping; return xmlMapping;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论