提交 ba353842 authored 作者: HASEE's avatar HASEE

model layer 优化(1)扫描所有主系统Entity

上级 d02b152d
......@@ -3,6 +3,7 @@ package com.tykj.workflowcore.model_layer.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tykj.workflowcore.model_layer.annotatiion.WorkFlowCoreNoScan;
import com.tykj.workflowcore.model_layer.dao.ColumnInfoDao;
import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
import com.tykj.workflowcore.model_layer.model.ColumnInfo;
......@@ -11,6 +12,8 @@ import com.tykj.workflowcore.model_layer.model.TableInfo;
import com.tykj.workflowcore.model_layer.model.TableVO;
import com.tykj.workflowcore.model_layer.service.ModelService;
import com.tykj.workflowcore.model_layer.utils.CreatTableUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.Session;
import org.hibernate.internal.SessionImpl;
......@@ -24,15 +27,20 @@ import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.Entity;
import javax.persistence.EntityManagerFactory;
import javax.persistence.criteria.*;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.tykj.workflowcore.model_layer.utils.CreatTableUtil.CreatTable;
import static com.tykj.workflowcore.model_layer.utils.CreatTableUtil.getClassName;
/**
* @ClassName MoedelImpl
......@@ -117,7 +125,7 @@ public class ModelImpl implements ModelService {
@Override
public TableVO NewTable(TableVO tableVO) {
String XML_MAPPING = CreatTableUtil.CreatTable(tableVO);
String XML_MAPPING = CreatTable(tableVO);
CreatTableUtil creatTableUtil = new CreatTableUtil();
Session session = creatTableUtil.getSession(entityManagerFactory, XML_MAPPING);
......@@ -231,4 +239,89 @@ public class ModelImpl implements ModelService {
newSession.saveOrUpdate(tableName, map);
newSession.close();
}
public void SwaggerScan(Class<?> aClass) {
if (!aClass.isAnnotationPresent(WorkFlowCoreNoScan.class)) {
if (aClass.isAnnotationPresent(Entity.class)) {
TableInfo tableInfo = new TableInfo();
// tableInfo.setId(id);
TableVO tableVO = new TableVO();
String className = getClassName(aClass.toString());
tableVO.setModelName(className.toLowerCase() + "_model_test");
if (aClass.isAnnotationPresent(ApiModel.class)) {
ApiModel annotation = aClass.getAnnotation(ApiModel.class);
StringBuilder APiModelDocument = new StringBuilder();
if (annotation.value() != null && !annotation.value().equals("")) {
APiModelDocument.append(annotation.value() + "|");
}
if (annotation.description() != null && !annotation.description().equals("")) {
APiModelDocument.append(annotation.description() + "|");
}
tableVO.setModelTitle(APiModelDocument.toString());
} else {
tableVO.setModelTitle("");
}
Field[] declaredFields = aClass.getDeclaredFields();
java.lang.reflect.Type genericType = null;
List<ColumnVO> list = new ArrayList<>();
for (Field declaredField : declaredFields) {
ColumnVO columnVO = new ColumnVO();
genericType = declaredField.getGenericType();
columnVO.setFiledType(genericType.toString());
columnVO.setFieldName(getClassName(declaredField.toString()));
if (declaredField.isAnnotationPresent(ApiModelProperty.class)) {
ApiModelProperty annotation = declaredField.getAnnotation(ApiModelProperty.class);
StringBuilder ApiModelPropertyDocument = new StringBuilder();
if (annotation.value() != null && !annotation.value().equals("")) {
ApiModelPropertyDocument.append(annotation.value() + "|");
}
if (annotation.example() != null && !annotation.example().equals("")) {
ApiModelPropertyDocument.append(annotation.example() + "|");
}
columnVO.setFiledDescription(ApiModelPropertyDocument.toString());
} else {
columnVO.setFiledDescription("无描述");
}
list.add(columnVO);
}
tableVO.setDataList(list);
String xml = CreatTable(tableVO);
tableInfo.setName(tableVO.getModelName());
tableInfo.setCnName(tableVO.getModelTitle());
tableInfo.setXML(xml);
tableInfo= tableInfoDao.save(tableInfo);
List<ColumnVO> dataList = tableVO.getDataList();
for (ColumnVO columnVO : dataList) {
ColumnInfo columnInfo = new ColumnInfo();
columnInfo.setName(columnVO.getFieldName());
columnInfo.setType(columnVO.getFiledType());
columnInfo.setLength(columnVO.getFiledLength());
columnInfo.setCnName(columnVO.getFiledDescription());
if (genericType.toString().equals("class java.lang.String")){
columnInfo.setLength(65535);
}
else if (genericType.toString().equals("class java.lang.Integer")){
columnInfo.setLength(11);
}else {
columnInfo.setLength(0);
}
columnInfo.setDbName(className);
columnInfo.setDbId(tableInfo.getId());
columnInfoDao.save(columnInfo);
}
}
}
}
}
package com.tykj.workflowcore.model_layer.utils;
import sun.net.www.protocol.jar.JarURLConnection;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* @ClassName ClassScan
* @Description TODO
* @Author WWW
* @Date 2021/2/22 13:44
* @Version 1.0
*/
public class ClassScanUtil {
private void loadClassByPath(String root, String path, List<Class<?>> list, ClassLoader load) {
File f = new File(path);
if (root == null) root = f.getPath();
//判断是否是class文件
if (f.isFile() && f.getName().matches("^.*\\.class$")) {
try {
String classPath = f.getPath();
//截取出className 将路径分割符替换为.(windows是\ linux、mac是/)
String className = classPath.substring(root.length() + 1, classPath.length() - 6).replace('/', '.').replace('\\', '.');
list.add(load.loadClass(className));
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
File[] fs = f.listFiles();
if (fs == null) return;
for (File file : fs) {
loadClassByPath(root, file.getPath(), list, load);
}
}
}
public List<Class<?>> loadClassByLoader(ClassLoader load) {
List<Class<?>> classes = new ArrayList<>();
try {
Enumeration<URL> urls = load.getResources("");
//放所有类型
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
//文件类型(其实是文件夹)
if (url.getProtocol().equals("file")) {
loadClassByPath(null, url.getPath(), classes, load);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return classes;
}
}
......@@ -78,74 +78,7 @@ public class CreatTableUtil {
return newSession;
}
public TableInfo SwaggerScan(Class<?> aClass) {
TableInfo tableInfo = null;
if (!aClass.isAnnotationPresent(WorkFlowCoreNoScan.class)){
if (aClass.isAnnotationPresent(Entity.class)) {
tableInfo =new TableInfo();
TableVO tableVO=new TableVO();
String className = getClassName(aClass.toString());
tableVO.setModelName(className.toLowerCase()+"_ModelTest");
if (aClass.isAnnotationPresent(ApiModel.class)) {
ApiModel annotation = aClass.getAnnotation(ApiModel.class);
StringBuilder APiModelDocument = new StringBuilder();
if (annotation.value() != null && !annotation.value().equals("")) {
APiModelDocument.append( annotation.value()+ "|");
}
if (annotation.description() != null && !annotation.description().equals("")) {
APiModelDocument.append( annotation.description() + "|");
}
tableVO.setModelTitle(APiModelDocument.toString());
}
else {
tableVO.setModelTitle("");
}
Field[] declaredFields = aClass.getDeclaredFields();
List<ColumnVO> list=new ArrayList<>();
for (Field declaredField : declaredFields) {
ColumnVO columnVO=new ColumnVO();
Type genericType = declaredField.getGenericType();
columnVO.setFiledType(genericType.toString());
columnVO.setFieldName(getClassName(declaredField.toString()));
if (declaredField.isAnnotationPresent(ApiModelProperty.class)) {
ApiModelProperty annotation = declaredField.getAnnotation(ApiModelProperty.class);
StringBuilder ApiModelPropertyDocument = new StringBuilder();
if (annotation.value() != null && !annotation.value().equals("")) {
ApiModelPropertyDocument.append( annotation.value() + "|");
}
if (annotation.example() != null && !annotation.example().equals("")) {
ApiModelPropertyDocument.append( annotation.example() + "|");
}
columnVO.setFiledDescription(ApiModelPropertyDocument.toString());
}
else {
columnVO.setFiledDescription("无描述");
}
list.add(columnVO);
}
tableVO.setDataList(list);
String xml = CreatTable(tableVO);
tableInfo.setName(tableVO.getModelName());
tableInfo.setCnName(tableVO.getModelTitle());
tableInfo.setXML(xml);
}
}
return tableInfo;
}
public static String getClassName(String aClass){
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论