提交 ea256d2b authored 作者: ww1xhqc's avatar ww1xhqc

[数据模型] 新增聚合对象生成xml方法。

上级 cc953f6b
...@@ -13,4 +13,10 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor; ...@@ -13,4 +13,10 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* @Version 1.0 * @Version 1.0
*/ */
public interface TableInfoDao extends JpaRepository<TableInfo, Integer>, JpaSpecificationExecutor<TableInfo> { public interface TableInfoDao extends JpaRepository<TableInfo, Integer>, JpaSpecificationExecutor<TableInfo> {
/**
* 根据表名查询tableInfo
* @param name
* @return
*/
TableInfo findByModelName(String name);
} }
package com.tykj.workflowcore.model_layer.dao;
import com.tykj.workflowcore.model_layer.entity.TableInfoEX;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @ClassName TableInfoExDao
* @Description TODO
* @Author WWW
* @Date 2021/4/1 17:00
* @Version 1.0
*/
public interface TableInfoExDao extends JpaRepository<TableInfoEX, Integer>, JpaSpecificationExecutor<TableInfoEX> {
}
...@@ -25,7 +25,6 @@ import java.io.Serializable; ...@@ -25,7 +25,6 @@ import java.io.Serializable;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@Entity @Entity
@SQLDelete(sql = "update column_info set deleted = 1 where id = ?") @SQLDelete(sql = "update column_info set deleted = 1 where id = ?")
@Where(clause = "deleted = 0") @Where(clause = "deleted = 0")
public class ColumnInfo extends BaseEntity { public class ColumnInfo extends BaseEntity {
......
package com.tykj.workflowcore.model_layer.entity;
import com.tykj.workflowcore.base.entity.BaseEntity;
import com.tykj.workflowcore.model_layer.annotations.WorkFlowCoreNoScan;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import javax.persistence.Entity;
import javax.persistence.Lob;
/**
* @ClassName TableInfoEX
* @Description TODO 聚合对象
* @Author WWW
* @Date 2021/4/1 16:45
* @Version 1.0
*/
@WorkFlowCoreNoScan
@Data
@NoArgsConstructor
@Entity
@SQLDelete(sql = "update column_info set deleted = 1 where id = ?")
@Where(clause = "deleted = 0")
public class TableInfoEX extends BaseEntity {
@ApiModelProperty("名称")
private String name;
@ApiModelProperty("主表id")
private Integer mainTableId;
@ApiModelProperty("附表id")
private Integer sideTableId;
@Lob
@ApiModelProperty("聚合对象xml")
private String xml;
@ApiModelProperty("聚合对象关系")
private Integer relationship;
@ApiModelProperty("连接属性")
private String connectionProperty;
@ApiModelProperty("连接key")
private String connectionKey;
}
package com.tykj.workflowcore.model_layer.myEnum;
/**
* @ClassName AggregationType
* @Description TODO
* @Author WWW
* @Date 2021/4/1 16:56
* @Version 1.0
*/
public class AggregationType {
public static final Integer ONE_TO_ONE=0;
public static final Integer ONE_TO_MANY=1;
public static final Integer MANY_TO_MANY=0;
}
package com.tykj.workflowcore.model_layer.service;
import com.tykj.workflowcore.model_layer.entity.TableInfoEX;
import org.springframework.stereotype.Service;
/**
* @ClassName Aggregation
* @Description TODO
* @Author WWW
* @Date 2021/4/1 17:08
* @Version 1.0
*/
@Service
public interface AggregationService {
void addAggregationOneToOne(TableInfoEX tableInfoEX);
}
package com.tykj.workflowcore.model_layer.service.impl;
import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
import com.tykj.workflowcore.model_layer.dao.TableInfoExDao;
import com.tykj.workflowcore.model_layer.entity.TableInfo;
import com.tykj.workflowcore.model_layer.entity.TableInfoEX;
import com.tykj.workflowcore.model_layer.service.AggregationService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Optional;
/**
* @ClassName AggregationImpl
* @Description TODO
* @Author WWW
* @Date 2021/4/1 17:24
* @Version 1.0
*/
public class AggregationImpl implements AggregationService {
@Autowired
TableInfoDao tableInfoDao;
@Autowired
TableInfoExDao tableInfoExDao;
@Override
public void addAggregationOneToOne(TableInfoEX tableInfoEX) {
}
}
...@@ -11,7 +11,7 @@ import com.tykj.workflowcore.model_layer.entity.vo.*; ...@@ -11,7 +11,7 @@ import com.tykj.workflowcore.model_layer.entity.vo.*;
import com.tykj.workflowcore.model_layer.entity.*; import com.tykj.workflowcore.model_layer.entity.*;
import com.tykj.workflowcore.model_layer.myEnum.ModelType; import com.tykj.workflowcore.model_layer.myEnum.ModelType;
import com.tykj.workflowcore.model_layer.service.ModelService; import com.tykj.workflowcore.model_layer.service.ModelService;
import com.tykj.workflowcore.model_layer.utils.CreateTableUtil; import com.tykj.workflowcore.model_layer.utils.SessionUtil;
import com.tykj.workflowcore.model_layer.utils.SqlUtil; import com.tykj.workflowcore.model_layer.utils.SqlUtil;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
...@@ -68,9 +68,6 @@ import static java.util.Objects.nonNull; ...@@ -68,9 +68,6 @@ import static java.util.Objects.nonNull;
public class ModelImpl implements ModelService { public class ModelImpl implements ModelService {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Autowired @Autowired
private TableInfoDao tableInfoDao; private TableInfoDao tableInfoDao;
...@@ -80,6 +77,9 @@ public class ModelImpl implements ModelService { ...@@ -80,6 +77,9 @@ public class ModelImpl implements ModelService {
@Autowired @Autowired
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@Autowired
private SessionUtil sessionUtil;
/** /**
* @param * @param
* @return java.util.List<com.tykj.workflowcore.model_layer.model.TableInfo> * @return java.util.List<com.tykj.workflowcore.model_layer.model.TableInfo>
...@@ -133,8 +133,11 @@ public class ModelImpl implements ModelService { ...@@ -133,8 +133,11 @@ public class ModelImpl implements ModelService {
tableVO.setModelType(ModelType.BUSINESS); tableVO.setModelType(ModelType.BUSINESS);
} }
CreateTableUtil creatTableUtil = new CreateTableUtil();
Session session = creatTableUtil.getSession(entityManagerFactory, xmlMapping); sessionUtil.addXml(xmlMapping);
Session session = sessionUtil.getSession();
// CreateTableUtil creatTableUtil = new CreateTableUtil();
// Session session = creatTableUtil.getSession(entityManagerFactory, xmlMapping);
List<ColumnVO> dataList = tableVO.getDataList(); List<ColumnVO> dataList = tableVO.getDataList();
TableInfo tableInfo = new TableInfo(); TableInfo tableInfo = new TableInfo();
...@@ -193,12 +196,12 @@ public class ModelImpl implements ModelService { ...@@ -193,12 +196,12 @@ public class ModelImpl implements ModelService {
Object values = map.get(tableName); Object values = map.get(tableName);
if (values instanceof Map) { if (values instanceof Map) {
//插入数据 //插入数据
insertValue(tableInfo.getModelName(), tableInfo.getXml(), (Map) values); insertValue(tableInfo.getModelName(), (Map) values);
} else { } else {
//循环插入数据 //循环插入数据
List valuesList = (List) values; List valuesList = (List) values;
for (int i = 0; i < valuesList.size(); i++) { for (int i = 0; i < valuesList.size(); i++) {
insertValue(tableInfo.getModelName(), tableInfo.getXml(), (Map) valuesList.get(i)); insertValue(tableInfo.getModelName(), (Map) valuesList.get(i));
} }
} }
} }
...@@ -212,16 +215,18 @@ public class ModelImpl implements ModelService { ...@@ -212,16 +215,18 @@ public class ModelImpl implements ModelService {
/** /**
* @param tableName * @param tableName
* @param xml
* @param map * @param map
* @return void * @return void
* @Author WWW * @Author WWW
* @Description 新增参数的方法 * @Description 新增参数的方法
* @Date 16:17 2021/3/5 * @Date 16:17 2021/3/5
**/ **/
public void insertValue(String tableName, String xml, Map map) { public void insertValue(String tableName, Map map) {
CreateTableUtil createTableUtil = new CreateTableUtil(); // CreateTableUtil createTableUtil = new CreateTableUtil();
Session newSession = createTableUtil.getSession(entityManagerFactory, xml); // Session newSession = createTableUtil.getSession(entityManagerFactory, xml);
Session newSession = sessionUtil.getSession();
SessionImpl session = (SessionImpl) newSession; SessionImpl session = (SessionImpl) newSession;
EntityPersister entityPersister = session.getEntityPersister(tableName, map); EntityPersister entityPersister = session.getEntityPersister(tableName, map);
Type[] propertyTypes = entityPersister.getPropertyTypes(); Type[] propertyTypes = entityPersister.getPropertyTypes();
......
package com.tykj.workflowcore.model_layer.utils;
import liquibase.pro.packaged.E;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.util.List;
/**
* @ClassName AggregationUtil
* @Description TODO
* @Author WWW
* @Date 2021/4/1 18:59
* @Version 1.0
*/
public class AggregationUtil {
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 addOneToOne(String field, String sideTableName, String xml) {
Element element = DocumentHelper.createElement("one-to-one");
element.addAttribute("name", field);
element.addAttribute("class", sideTableName);
element.addAttribute("cascade", "save-update");
Document document = null;
try {
document = DocumentHelper.parseText(xml);
} catch (DocumentException e) {
e.printStackTrace();
}
Element classElement = getNode(xml);
classElement.add(element);
return document.asXML();
}
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;
}
public static Element getNode(String xml) {
Document document = null;
try {
document = DocumentHelper.parseText(xml);
} catch (DocumentException e) {
e.printStackTrace();
}
return document.getRootElement().elements().get(0);
}
}
...@@ -4,6 +4,7 @@ package com.tykj.workflowcore.model_layer.utils; ...@@ -4,6 +4,7 @@ package com.tykj.workflowcore.model_layer.utils;
import com.tykj.workflowcore.base.result.ApiException; import com.tykj.workflowcore.base.result.ApiException;
import com.tykj.workflowcore.model_layer.entity.vo.ColumnVO; import com.tykj.workflowcore.model_layer.entity.vo.ColumnVO;
import com.tykj.workflowcore.model_layer.entity.vo.TableVO; import com.tykj.workflowcore.model_layer.entity.vo.TableVO;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata; import org.hibernate.boot.Metadata;
...@@ -42,7 +43,7 @@ public class CreateTableUtil { ...@@ -42,7 +43,7 @@ public class CreateTableUtil {
for (ColumnVO columnVO : dataList) { for (ColumnVO columnVO : dataList) {
xmlMapping += xmlMapping +=
" <property type=\"" + columnVO.getFieldType() + "\" name=\"" + columnVO.getFieldName() + "\" length=\"" + columnVO.getFieldLength()+ " <property type=\"" + columnVO.getFieldType() + "\" name=\"" + columnVO.getFieldName() + "\" length=\"" + columnVO.getFieldLength() +
"\" column=\"" + columnVO.getFieldName() + "\"/>\n"; "\" column=\"" + columnVO.getFieldName() + "\"/>\n";
} }
......
package com.tykj.workflowcore.model_layer.utils;
import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
import com.tykj.workflowcore.model_layer.entity.TableInfo;
import org.checkerframework.checker.units.qual.A;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.schema.TargetType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.persistence.EntityManagerFactory;
import java.io.ByteArrayInputStream;
import java.util.EnumSet;
import java.util.List;
/**
* @ClassName SessionUtil
* @Description TODO
* @Author WWW
* @Date 2021/4/1 10:08
* @Version 1.0
*/
@Component
public class SessionUtil {
SessionFactory sessionFactory;
StandardServiceRegistry serviceRegistry;
@Autowired
TableInfoDao tableInfoDao;
MetadataSources metadataSources;
public SessionUtil(EntityManagerFactory entityManagerFactory,TableInfoDao tableInfoDao){
this.tableInfoDao = tableInfoDao;
sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
serviceRegistry = sessionFactory.getSessionFactoryOptions().getServiceRegistry();
metadataSources = new MetadataSources(serviceRegistry);
sessionFactory.getSessionFactoryOptions();
init();
}
private void init(){
//去数据库读取所有的XML 加载到 metasource里
List<TableInfo> all = tableInfoDao.findAll();
for (TableInfo tableInfo : all) {
String xml = tableInfo.getXml();
addXml(xml);
}
}
public void addXml(String xmlStr){
metadataSources.addInputStream(new ByteArrayInputStream(xmlStr.getBytes()));
Metadata metadata = metadataSources.buildMetadata();
//更新数据库Schema,如果不存在就创建表,存在就更新字段,不会影响已有数据
SchemaUpdate schemaUpdate = new SchemaUpdate();
schemaUpdate.execute(EnumSet.of(TargetType.DATABASE), metadata, serviceRegistry);
}
public Session getSession(){
Metadata metadata = metadataSources.buildMetadata();
//创建会话工厂
SessionFactory newSessionFactory = metadata.buildSessionFactory();
//保存对象
return newSessionFactory.openSession();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论