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

[数据模型] 修复扫描时多出id字段bug

上级 9b13f021
...@@ -2,6 +2,7 @@ package com.tykj.workflowcore.model_layer.service.impl; ...@@ -2,6 +2,7 @@ package com.tykj.workflowcore.model_layer.service.impl;
import com.github.wenhao.jpa.PredicateBuilder; import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications; import com.github.wenhao.jpa.Specifications;
import com.google.common.base.Strings;
import com.tykj.workflowcore.base.result.ApiException; import com.tykj.workflowcore.base.result.ApiException;
import com.tykj.workflowcore.model_layer.annotations.WorkFlowCoreNoScan; import com.tykj.workflowcore.model_layer.annotations.WorkFlowCoreNoScan;
import com.tykj.workflowcore.model_layer.dao.ColumnInfoDao; import com.tykj.workflowcore.model_layer.dao.ColumnInfoDao;
...@@ -104,13 +105,13 @@ public class ModelImpl implements ModelService { ...@@ -104,13 +105,13 @@ public class ModelImpl implements ModelService {
return tableInfoDao.findAll(and.build()); return tableInfoDao.findAll(and.build());
} }
private PredicateBuilder<TableInfo> createPredicateBySearchTableInfoVo(SearchTableInfoVo searchTableInfoVo){ private PredicateBuilder<TableInfo> createPredicateBySearchTableInfoVo(SearchTableInfoVo searchTableInfoVo) {
PredicateBuilder<TableInfo> and = Specifications.and(); PredicateBuilder<TableInfo> and = Specifications.and();
if (searchTableInfoVo!=null){ if (searchTableInfoVo != null) {
and.like(searchTableInfoVo.getModelName() != null && StringUtils.isNotEmpty(searchTableInfoVo.getModelName()), "modelName", "%" + searchTableInfoVo.getModelName() + "%"); and.like(searchTableInfoVo.getModelName() != null && StringUtils.isNotEmpty(searchTableInfoVo.getModelName()), "modelName", "%" + searchTableInfoVo.getModelName() + "%");
and.like(searchTableInfoVo.getModelTitle() != null && StringUtils.isNotEmpty(searchTableInfoVo.getModelTitle()), "modelTitle", "%" + searchTableInfoVo.getModelTitle() + "%"); and.like(searchTableInfoVo.getModelTitle() != null && StringUtils.isNotEmpty(searchTableInfoVo.getModelTitle()), "modelTitle", "%" + searchTableInfoVo.getModelTitle() + "%");
and.in(searchTableInfoVo.getModelType() != null&&searchTableInfoVo.getModelType().length>0, "modelType", searchTableInfoVo.getModelType()); and.in(searchTableInfoVo.getModelType() != null && searchTableInfoVo.getModelType().length > 0, "modelType", searchTableInfoVo.getModelType());
and.in(searchTableInfoVo.getIds() != null&&searchTableInfoVo.getIds().length>0, "id", searchTableInfoVo.getIds()); and.in(searchTableInfoVo.getIds() != null && searchTableInfoVo.getIds().length > 0, "id", searchTableInfoVo.getIds());
} }
return and; return and;
} }
...@@ -307,10 +308,10 @@ public class ModelImpl implements ModelService { ...@@ -307,10 +308,10 @@ public class ModelImpl implements ModelService {
if (aClass.isAnnotationPresent(ApiModel.class)) { if (aClass.isAnnotationPresent(ApiModel.class)) {
ApiModel annotation = aClass.getAnnotation(ApiModel.class); ApiModel annotation = aClass.getAnnotation(ApiModel.class);
StringBuilder apiModelDocument = new StringBuilder(); StringBuilder apiModelDocument = new StringBuilder();
if (annotation.value() != null && !"".equals(annotation.value())) { if (!Strings.isNullOrEmpty(annotation.value())) {
apiModelDocument.append(annotation.value() + "|"); apiModelDocument.append(annotation.value() + "|");
} }
if (annotation.description() != null && !"".equals(annotation.description())) { if (!Strings.isNullOrEmpty(annotation.description())) {
apiModelDocument.append(annotation.description() + "|"); apiModelDocument.append(annotation.description() + "|");
} }
tableVO.setModelTitle(apiModelDocument.toString()); tableVO.setModelTitle(apiModelDocument.toString());
...@@ -322,8 +323,11 @@ public class ModelImpl implements ModelService { ...@@ -322,8 +323,11 @@ public class ModelImpl implements ModelService {
java.lang.reflect.Type genericType; java.lang.reflect.Type genericType;
List<ColumnVO> list = new ArrayList<>(); List<ColumnVO> list = new ArrayList<>();
ColumnVO primaryColumn = new ColumnVO();
;
List<ColumnVO> otherColumns = new ArrayList<>();
for (Field declaredField : declaredFields) { for (Field declaredField : declaredFields) {
ColumnVO columnVO = new ColumnVO(); // ColumnVO columnVO = new ColumnVO();
if (declaredField.isAnnotationPresent(Transient.class)) { if (declaredField.isAnnotationPresent(Transient.class)) {
continue; continue;
} }
...@@ -331,31 +335,58 @@ public class ModelImpl implements ModelService { ...@@ -331,31 +335,58 @@ public class ModelImpl implements ModelService {
genericType = declaredField.getGenericType(); genericType = declaredField.getGenericType();
//是否主键 //是否主键
if (declaredField.isAnnotationPresent(javax.persistence.Id.class)) { if (declaredField.isAnnotationPresent(javax.persistence.Id.class)) {
columnVO.setPrimaryKey(0); primaryColumn.setPrimaryKey(0);
} primaryColumn.setFieldType(getTypeName(genericType.toString()));
primaryColumn.setFieldName(getClassName(declaredField.toString()));
columnVO.setFieldType(getTypeName(genericType.toString())); // primaryColumn.setFieldLength(setLength(genericType.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); StringBuilder apiModelPropertyDocument = new StringBuilder();
StringBuilder apiModelPropertyDocument = new StringBuilder(); if (!Strings.isNullOrEmpty(annotation.value())) {
if (annotation.value() != null && !"".equals(annotation.value())) { apiModelPropertyDocument.append(annotation.value() + "|");
apiModelPropertyDocument.append(annotation.value() + "|"); }
} if (!Strings.isNullOrEmpty(annotation.example())) {
if (annotation.example() != null && !"".equals(annotation.example())) { apiModelPropertyDocument.append(annotation.example() + "|");
}
apiModelPropertyDocument.append(annotation.example() + "|"); primaryColumn.setFieldTitle(apiModelPropertyDocument.toString());
} else {
primaryColumn.setFieldTitle("(无描述)");
} }
columnVO.setFieldTitle(apiModelPropertyDocument.toString());
} else { } else {
columnVO.setFieldTitle("(无描述)"); ColumnVO otherColumn = new ColumnVO();
otherColumn.setFieldType(getTypeName(genericType.toString()));
otherColumn.setFieldName(getClassName(declaredField.toString()));
// otherColumn.setFieldLength(setLength(genericType.toString()));
otherColumns.add(otherColumn);
//获得属性中文描述
if (declaredField.isAnnotationPresent(ApiModelProperty.class)) {
ApiModelProperty annotation = declaredField.getAnnotation(ApiModelProperty.class);
StringBuilder apiModelPropertyDocument = new StringBuilder();
if (!Strings.isNullOrEmpty(annotation.value())) {
apiModelPropertyDocument.append(annotation.value() + "|");
}
if (!Strings.isNullOrEmpty(annotation.example())) {
apiModelPropertyDocument.append(annotation.example() + "|");
}
otherColumn.setFieldTitle(apiModelPropertyDocument.toString());
} else {
otherColumn.setFieldTitle("(无描述)");
}
} }
list.add(columnVO);
// columnVO.setFieldType(getTypeName(genericType.toString()));
// columnVO.setFieldName(getClassName(declaredField.toString()));
// columnVO.setFieldLength(setLength(genericType.toString()));
} }
list.add(primaryColumn);
list.addAll(otherColumns);
tableVO.setDataList(list); tableVO.setDataList(list);
String xml = createTable(tableVO); //扫描的xml
String xml = createTable(tableVO.getModelName(), primaryColumn, otherColumns);
tableInfo.setModelName(tableVO.getModelName()); tableInfo.setModelName(tableVO.getModelName());
tableInfo.setModelTitle(tableVO.getModelTitle()); tableInfo.setModelTitle(tableVO.getModelTitle());
tableInfo.setXml(xml); tableInfo.setXml(xml);
...@@ -363,21 +394,20 @@ public class ModelImpl implements ModelService { ...@@ -363,21 +394,20 @@ public class ModelImpl implements ModelService {
//判断是否存在 //判断是否存在
if (checkRepeat(realName)) { if (checkRepeat(realName)) {
tableInfo = tableInfoDao.save(tableInfo); tableInfo = tableInfoDao.save(tableInfo);
List<ColumnVO> dataList = tableVO.getDataList();
for (ColumnVO columnVO : dataList) {
ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setFieldName(columnVO.getFieldName());
columnInfo.setFieldType(columnVO.getFieldType());
columnInfo.setFieldLength(columnVO.getFieldLength());
columnInfo.setFieldTitle(columnVO.getFieldTitle());
columnInfo.setPrimaryKey(columnVO.getPrimaryKey());
columnInfo.setDbName(realName);
columnInfo.setDbId(tableInfo.getId());
columnInfoDao.save(columnInfo);
}
} else { } else {
tableInfo = tableInfoDao.findByModelName(tableInfo.getModelName()); log.warn("{}已存在", realName);
log.info("{}已存在", realName);
}
List<ColumnVO> dataList = tableVO.getDataList();
for (ColumnVO columnVO : dataList) {
ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setFieldName(columnVO.getFieldName());
columnInfo.setFieldType(columnVO.getFieldType());
columnInfo.setFieldLength(columnVO.getFieldLength());
columnInfo.setFieldTitle(columnVO.getFieldTitle());
columnInfo.setPrimaryKey(columnVO.getPrimaryKey());
columnInfo.setDbName(realName);
columnInfo.setDbId(tableInfo.getId());
columnInfoDao.save(columnInfo);
} }
} }
} }
...@@ -469,14 +499,14 @@ public class ModelImpl implements ModelService { ...@@ -469,14 +499,14 @@ public class ModelImpl implements ModelService {
.collect(Collectors.toList()); .collect(Collectors.toList());
//根据ColumnInfo集合得出表实际改动的sql语句集合 //根据ColumnInfo集合得出表实际改动的sql语句集合
List<String> sqls = getTableSqls(tableInfo.getModelName(), originalColumnInfos, currentColumnInfos); List<String> sqls = getTableSqls(tableInfo.getModelName(), originalColumnInfos, currentColumnInfos);
//执行sql语句 //执行sql语句
for (String sql : sqls) { for (String sql : sqls) {
System.out.println(sql); System.out.println(sql);
jdbcTemplate.execute(sql); jdbcTemplate.execute(sql);
} }
//重新保存字段信息 //重新保存字段信息
List<ColumnInfo> columnsForAdd = getColumnsForAdd(originalColumnInfos, currentColumnInfos); List<ColumnInfo> columnsForAdd = getColumnsForAdd(originalColumnInfos, currentColumnInfos);
...@@ -493,7 +523,7 @@ public class ModelImpl implements ModelService { ...@@ -493,7 +523,7 @@ public class ModelImpl implements ModelService {
} }
} }
if(modelType.equals(ModelType.BASICS_EXTENSION)){ if (modelType.equals(ModelType.BASICS_EXTENSION)) {
columnInfoDao.deleteAllByDbId(updateTableInfoVO.getDbId()); columnInfoDao.deleteAllByDbId(updateTableInfoVO.getDbId());
List<ColumnInfo> dataList = updateTableInfoVO.getTableVO().getDataList().stream() List<ColumnInfo> dataList = updateTableInfoVO.getTableVO().getDataList().stream()
.map(columnVO -> columnInfo(tableInfo.getId(), tableInfo.getModelName(), columnVO)) .map(columnVO -> columnInfo(tableInfo.getId(), tableInfo.getModelName(), columnVO))
...@@ -599,7 +629,7 @@ public class ModelImpl implements ModelService { ...@@ -599,7 +629,7 @@ public class ModelImpl implements ModelService {
} else { } else {
TableInfo tableInfo = byId.get(); TableInfo tableInfo = byId.get();
Integer modelType = tableInfo.getModelType(); Integer modelType = tableInfo.getModelType();
if (modelType.equals(ModelType.BUSINESS)||modelType.equals(ModelType.BASICS_EXTENSION)) { if (modelType.equals(ModelType.BUSINESS) || modelType.equals(ModelType.BASICS_EXTENSION)) {
tableInfoDao.deleteById(delTableVO.getId()); tableInfoDao.deleteById(delTableVO.getId());
List<ColumnInfo> allByDbId = columnInfoDao.findAllByDbId(delTableVO.getId()); List<ColumnInfo> allByDbId = columnInfoDao.findAllByDbId(delTableVO.getId());
columnInfoDao.deleteInBatch(allByDbId); columnInfoDao.deleteInBatch(allByDbId);
......
...@@ -23,7 +23,7 @@ public class ClassTypeLength { ...@@ -23,7 +23,7 @@ public class ClassTypeLength {
} else if (Double.equals(genericType)) { } else if (Double.equals(genericType)) {
length = 10; length = 10;
} else { } else {
length = 0; length =1;
} }
return length; return length;
} }
......
...@@ -59,12 +59,13 @@ public class CreateTableUtil { ...@@ -59,12 +59,13 @@ public class CreateTableUtil {
" <class entity-name=\"" + tableVO.getModelName() + "\" table=\"" + tableVO.getModelName() + "\">\n"; " <class entity-name=\"" + tableVO.getModelName() + "\" table=\"" + tableVO.getModelName() + "\">\n";
xmlMapping += " <id name=\"id\" type=\"java.lang.Integer\" length=\"11\" unsaved-value=\"null\" >\n" + xmlMapping += " <id name=\"id\" type=\"java.lang.Integer\" length=\"11\" unsaved-value=\"null\" >\n" +
" <generator class=\"identity\" />\n" +
" </id>"; " </id>";
for (ColumnVO columnVO : dataList) { for (ColumnVO columnVO : dataList) {
xmlMapping += xmlMapping +=
" <property type=\"" + columnVO.getFieldType() + "\" name=\"" + columnVO.getFieldName() + "\" length=\"" + columnVO.getFieldLength() + "\n <property type=\"" + columnVO.getFieldType() + "\" name=\"" + columnVO.getFieldName() + "\" length=\"" + columnVO.getFieldLength() +
"\" column=\"" + columnVO.getFieldName() + "\"/>\n"; "\" column=\"" + columnVO.getFieldName() + "\"/>\n";
} }
...@@ -74,6 +75,37 @@ public class CreateTableUtil { ...@@ -74,6 +75,37 @@ public class CreateTableUtil {
} }
public static String createTable(String entityName,ColumnVO primaryColumn,List<ColumnVO> dataList) {
// 1sql-type="text" string 转为text文本,2长度超过会自动转换
// List<ColumnVO> dataList = tableVO.getDataList();
String xmlMapping = "<?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>\n" +
" <class entity-name=\"" + entityName + "\" table=\"" + entityName + "\">\n";
xmlMapping += " <id name=\""+primaryColumn.getFieldName()+"\" type=\""+primaryColumn.getFieldType()+"\" unsaved-value=\"null\" >\n" +
" </id>";
for (ColumnVO columnVO : dataList) {
xmlMapping +=
"\n <property type=\"" + columnVO.getFieldType() + "\" name=\"" + columnVO.getFieldName() +
"\" column=\"" + columnVO.getFieldName() + "\"/>\n";
// xmlMapping +=
// "\n <property type=\"" + columnVO.getFieldType() + "\" name=\"" + columnVO.getFieldName() + "\" length=\"" + columnVO.getFieldLength() +
// "\" column=\"" + columnVO.getFieldName() + "\"/>\n";
}
xmlMapping += " </class>\n" +
"</hibernate-mapping>";
return xmlMapping;
}
public static String getClassName(String aClass) { public static String getClassName(String aClass) {
......
...@@ -517,17 +517,13 @@ class WorkflowCoreApplicationTests { ...@@ -517,17 +517,13 @@ class WorkflowCoreApplicationTests {
@Test @Test
public void complex() { public void complex() {
ArrayList<QueryCondition> queryConditions = Lists.newArrayList( ArrayList<QueryCondition> queryConditions = Lists.newArrayList(
new QueryCondition("id", "=", "1") new QueryCondition("testId", "=", "1")
// new QueryCondition("type", "=", "'1'") // new QueryCondition("type", "=", "'1'")
); );
List<Map<String, Object>> list = modelService.complexQuery("nw_history", queryConditions); List<Map<String, Object>> list = modelService.complexQuery("TestScan", queryConditions);
for (Map<String, Object> map : list) { for (Map<String, Object> map : list) {
System.out.println(map); System.out.println(map);
} }
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论