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

model layer 扫描所有主系统Entity

上级 903a1460
...@@ -26,6 +26,9 @@ import java.util.Map; ...@@ -26,6 +26,9 @@ import java.util.Map;
public class ModelController { public class ModelController {
@Autowired @Autowired
private ModelService modelService; private ModelService modelService;
@Autowired
private ClassLoader classLoader;
/** /**
* @Author WWW * @Author WWW
* @Description 得到所有数据库信息 * @Description 得到所有数据库信息
...@@ -95,4 +98,10 @@ public class ModelController { ...@@ -95,4 +98,10 @@ public class ModelController {
return i; return i;
} }
@GetMapping("/getPackge")
public String testPack(){
String name = this.getClass().getPackage().getName();
return name;
}
} }
...@@ -11,7 +11,7 @@ import lombok.Data; ...@@ -11,7 +11,7 @@ import lombok.Data;
* @Date 2021/3/1 9:58 * @Date 2021/3/1 9:58
* @Version 1.0 * @Version 1.0
*/ */
@WorkFlowCoreNoScan
@Data @Data
public class ColumnVO { public class ColumnVO {
......
...@@ -13,7 +13,6 @@ import java.util.List; ...@@ -13,7 +13,6 @@ import java.util.List;
* @Date 2021/3/1 9:56 * @Date 2021/3/1 9:56
* @Version 1.0 * @Version 1.0
*/ */
@WorkFlowCoreNoScan
@Data @Data
public class TableVO { public class TableVO {
......
...@@ -12,42 +12,26 @@ import com.tykj.workflowcore.model_layer.model.TableVO; ...@@ -12,42 +12,26 @@ import com.tykj.workflowcore.model_layer.model.TableVO;
import com.tykj.workflowcore.model_layer.service.ModelService; import com.tykj.workflowcore.model_layer.service.ModelService;
import com.tykj.workflowcore.model_layer.utils.CreatTableUtil; import com.tykj.workflowcore.model_layer.utils.CreatTableUtil;
import org.hibernate.Session; 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.internal.SessionImpl; import org.hibernate.internal.SessionImpl;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.schema.TargetType;
import org.hibernate.type.IntegerType; import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType; import org.hibernate.type.StringType;
import org.hibernate.type.TimestampType; import org.hibernate.type.TimestampType;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.criteria.*; import javax.persistence.criteria.*;
import javax.xml.crypto.Data;
import java.io.ByteArrayInputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
...@@ -62,7 +46,6 @@ import java.util.stream.Collectors; ...@@ -62,7 +46,6 @@ import java.util.stream.Collectors;
public class ModelImpl implements ModelService { public class ModelImpl implements ModelService {
@Autowired @Autowired
private EntityManagerFactory entityManagerFactory; private EntityManagerFactory entityManagerFactory;
...@@ -72,14 +55,26 @@ public class ModelImpl implements ModelService { ...@@ -72,14 +55,26 @@ public class ModelImpl implements ModelService {
@Autowired @Autowired
private ColumnInfoDao columnInfoDao; private ColumnInfoDao columnInfoDao;
/**
* @param
* @return java.util.List<com.tykj.workflowcore.model_layer.model.TableInfo>
* @Author WWW
* @Description 得到所有表
* @Date 16:14 2021/3/5
**/
@Override @Override
public List<TableInfo> ListAllEntities() { public List<TableInfo> ListAllEntities() {
List<TableInfo> all = tableInfoDao.findAll(); List<TableInfo> all = tableInfoDao.findAll();
return all; return all;
} }
/**
* @param TableName
* @return java.util.List<com.tykj.workflowcore.model_layer.model.ColumnInfo>
* @Author WWW
* @Description 根据表名得到所有字段名
* @Date 16:15 2021/3/5
**/
@Override @Override
public List<ColumnInfo> showModelFields(String TableName) { public List<ColumnInfo> showModelFields(String TableName) {
...@@ -93,6 +88,13 @@ public class ModelImpl implements ModelService { ...@@ -93,6 +88,13 @@ public class ModelImpl implements ModelService {
return all; return all;
} }
/**
* @param jsonStr
* @return com.tykj.workflowcore.model_layer.model.TableVO
* @Author WWW
* @Description 将json字符串转换实体类
* @Date 16:15 2021/3/5
**/
@Override @Override
public TableVO addModel(String jsonStr) { public TableVO addModel(String jsonStr) {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
...@@ -105,12 +107,19 @@ public class ModelImpl implements ModelService { ...@@ -105,12 +107,19 @@ public class ModelImpl implements ModelService {
return tableVO; return tableVO;
} }
/**
* @param tableVO
* @return com.tykj.workflowcore.model_layer.model.TableVO
* @Author WWW
* @Description 新建一张表
* @Date 16:16 2021/3/5
**/
@Override @Override
public TableVO NewTable(TableVO tableVO) { public TableVO NewTable(TableVO tableVO) {
String XML_MAPPING = CreatTableUtil.CreatTable(tableVO); String XML_MAPPING = CreatTableUtil.CreatTable(tableVO);
CreatTableUtil creatTableUtil = new CreatTableUtil(); CreatTableUtil creatTableUtil = new CreatTableUtil();
Session session = creatTableUtil.getSession(entityManagerFactory,XML_MAPPING); Session session = creatTableUtil.getSession(entityManagerFactory, XML_MAPPING);
List<ColumnVO> dataList = tableVO.getDataList(); List<ColumnVO> dataList = tableVO.getDataList();
...@@ -139,10 +148,17 @@ public class ModelImpl implements ModelService { ...@@ -139,10 +148,17 @@ public class ModelImpl implements ModelService {
return tableVO; return tableVO;
} }
/**
* @param map
* @return int
* @Author WWW
* @Description 根据表名新增数据
* @Date 16:17 2021/3/5
**/
@Override @Override
public int putValueByEntityName(Map<String, Object> map) { public int putValueByEntityName(Map<String, Object> map) {
for (String tableName: for (String tableName :
map.keySet()) { map.keySet()) {
//查找对应的表 //查找对应的表
Specification spec = (Specification) (root, criteriaQuery, criteriaBuilder) -> { Specification spec = (Specification) (root, criteriaQuery, criteriaBuilder) -> {
Predicate equal = null; Predicate equal = null;
...@@ -152,28 +168,37 @@ public class ModelImpl implements ModelService { ...@@ -152,28 +168,37 @@ public class ModelImpl implements ModelService {
}; };
Optional one = tableInfoDao.findOne(spec); Optional one = tableInfoDao.findOne(spec);
TableInfo tableInfo = null; TableInfo tableInfo = null;
if (one.isPresent()){ if (one.isPresent()) {
tableInfo = (TableInfo) one.get(); tableInfo = (TableInfo) one.get();
} }
Object values = map.get(tableName); Object values = map.get(tableName);
if (values instanceof Map){ if (values instanceof Map) {
//插入数据 //插入数据
insertValue(tableInfo.getName(),tableInfo.getXML(), (Map) values); insertValue(tableInfo.getName(), tableInfo.getXML(), (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.getName(),tableInfo.getXML(), (Map) valuesList.get(i)); insertValue(tableInfo.getName(), tableInfo.getXML(), (Map) valuesList.get(i));
} }
} }
} }
return 0; return 0;
} }
public void insertValue(String tableName,String xml,Map map){ /**
CreatTableUtil creatTableUtil = new CreatTableUtil(); * @param tableName
Session newSession = creatTableUtil.getSession(entityManagerFactory,xml); * @param xml
* @param map
* @return void
* @Author WWW
* @Description 新增参数的方法
* @Date 16:17 2021/3/5
**/
public void insertValue(String tableName, String xml, Map map) {
CreatTableUtil creatTableUtil = new CreatTableUtil();
Session newSession = creatTableUtil.getSession(entityManagerFactory, xml);
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();
...@@ -183,25 +208,27 @@ public class ModelImpl implements ModelService { ...@@ -183,25 +208,27 @@ public class ModelImpl implements ModelService {
Object value = propertyValuesToInsert[i]; Object value = propertyValuesToInsert[i];
Type propertyType = propertyTypes[i]; Type propertyType = propertyTypes[i];
//先将Type转为java类 //先将Type转为java类
if (propertyType instanceof TimestampType){ if (propertyType instanceof TimestampType) {
try { try {
Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) value); Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) value);
map.put(propertyNames[i],parse); map.put(propertyNames[i], parse);
} catch (ParseException e) { } catch (ParseException e) {
e.printStackTrace(); } e.printStackTrace();
}
} }
if (propertyType instanceof IntegerType){ if (propertyType instanceof IntegerType) {
//然后调用强转方法 //然后调用强转方法
int i1 = Integer.parseInt(propertyNames[i]); int i1 = Integer.parseInt(propertyNames[i]);
map.put(propertyNames[i],i1); map.put(propertyNames[i], i1);
} }
if (propertyType instanceof StringType){ if (propertyType instanceof StringType) {
//然后调用强转方法 //然后调用强转方法
propertyNames[i]=propertyNames[i]+""; propertyNames[i] = propertyNames[i] + "";
map.put(propertyNames[i],propertyNames[i]); map.put(propertyNames[i], propertyNames[i]);
} }
} }
newSession.saveOrUpdate(tableName,map); newSession.saveOrUpdate(tableName, map);
newSession.close();
} }
} }
package com.tykj.workflowcore.model_layer.utils; package com.tykj.workflowcore.model_layer.utils;
import com.tykj.workflowcore.model_layer.annotatiion.WorkFlowCoreNoScan;
import com.tykj.workflowcore.model_layer.model.ColumnVO; import com.tykj.workflowcore.model_layer.model.ColumnVO;
import com.tykj.workflowcore.model_layer.model.TableInfo;
import com.tykj.workflowcore.model_layer.model.TableVO; import com.tykj.workflowcore.model_layer.model.TableVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
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;
...@@ -11,8 +15,12 @@ import org.hibernate.boot.registry.StandardServiceRegistry; ...@@ -11,8 +15,12 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaUpdate; import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.schema.TargetType; import org.hibernate.tool.schema.TargetType;
import javax.persistence.Entity;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
...@@ -23,13 +31,8 @@ import java.util.List; ...@@ -23,13 +31,8 @@ import java.util.List;
* @Date 2021/3/1 14:35 * @Date 2021/3/1 14:35
* @Version 1.0 * @Version 1.0
*/ */
public class CreatTableUtil { public class CreatTableUtil {
public static String CreatTable(TableVO tableVO){ public static String CreatTable(TableVO tableVO){
List<ColumnVO> dataList = tableVO.getDataList(); List<ColumnVO> dataList = tableVO.getDataList();
String XML_MAPPING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + String XML_MAPPING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE hibernate-mapping PUBLIC\n" + "<!DOCTYPE hibernate-mapping PUBLIC\n" +
...@@ -47,7 +50,6 @@ public class CreatTableUtil { ...@@ -47,7 +50,6 @@ public class CreatTableUtil {
"\" column=\"" + columnVO.getFieldName() + "\"/>\n"; "\" column=\"" + columnVO.getFieldName() + "\"/>\n";
} }
XML_MAPPING += " </class>" + XML_MAPPING += " </class>" +
"</hibernate-mapping>"; "</hibernate-mapping>";
...@@ -75,4 +77,80 @@ public class CreatTableUtil { ...@@ -75,4 +77,80 @@ public class CreatTableUtil {
return newSession; 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){
int i = aClass.lastIndexOf(".");
String substring = aClass.substring(i+1);
return substring;
}
} }
package com.tykj.workflowcore.model_layer.utils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.Entity;
import java.lang.reflect.Field;
/**
* @ClassName JugdeSwagger
* @Description TODO
* @Author WWW
* @Date 2021/2/26 9:53
* @Version 1.0
*/
public class SwaggerUtil {
public static void Swagger(Class<?> aClass) {
if (aClass.isAnnotationPresent(Entity.class)) {
if (aClass.isAnnotationPresent(ApiModel.class)) {
ApiModel annotation = aClass.getAnnotation(ApiModel.class);
StringBuilder APiModelDocument = new StringBuilder();
if (annotation.value() != null && !annotation.value().equals("")) {
APiModelDocument.append(aClass + "的文档:value=" + annotation.value().toString() + ".");
}
if (annotation.description() != null && !annotation.description().equals("")) {
APiModelDocument.append("description=" + annotation.description().toString() + ".");
}
System.out.println(APiModelDocument.toString());
}
Field[] declaredFields = aClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
if (declaredField.isAnnotationPresent(ApiModelProperty.class)) {
ApiModelProperty annotation = declaredField.getAnnotation(ApiModelProperty.class);
StringBuilder ApiModelPropertyDocument = new StringBuilder();
if (annotation.value() != null && !annotation.value().equals("")) {
// System.out.println(declaredField+"的文档是"+annotation.value());
ApiModelPropertyDocument.append(declaredField + "的文档:value=" + annotation.value() + ".");
}
if (annotation.example() != null && !annotation.example().equals("")) {
// System.out.println(declaredField+"的文档是"+annotation.value());
ApiModelPropertyDocument.append("example=" + annotation.example() + ".");
}
System.out.println(ApiModelPropertyDocument.toString());
}
}
}
}
public static boolean isSwagger(Class<?> aClass) {
if (aClass.isAnnotationPresent(Entity.class)) {
if (aClass.isAnnotationPresent(ApiModel.class)) {
return true;
}
}
return false;
}
}
...@@ -5,8 +5,6 @@ spring: ...@@ -5,8 +5,6 @@ spring:
username: root username: root
password: 123456 password: 123456
url: jdbc:mysql://localhost:3306/model_layer?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true url: jdbc:mysql://localhost:3306/model_layer?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true
type: com.alibaba.druid.pool.DruidDataSource
jpa: jpa:
hibernate: hibernate:
...@@ -15,15 +13,5 @@ spring: ...@@ -15,15 +13,5 @@ spring:
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
show-sql: true show-sql: true
server:
port: 8900
spring:
datasource:
username: root
password: Huang123+
url: jdbc:mysql://47.106.142.73:3306/www?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&nullCatalogMeansCurrent=true
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
show-sql: true
hibernate:
ddl-auto: update
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论