提交 722775bf authored 作者: ww1xhqc's avatar ww1xhqc

model layer 代码规约检查

上级 b237fd37
......@@ -5,6 +5,9 @@ import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* @author HASEE
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
......
......@@ -15,5 +15,4 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface WorkFlowCoreNoScan {
String desc ="工作流核心不扫描!";
}
......@@ -4,6 +4,7 @@ package com.tykj.workflowcore.model_layer.controller;
import com.tykj.workflowcore.base.result.ResultUtil;
import com.tykj.workflowcore.model_layer.model.QueryCondition;
import com.tykj.workflowcore.model_layer.model.SearchTableInfoVo;
import com.tykj.workflowcore.model_layer.model.TableInfo;
import com.tykj.workflowcore.model_layer.model.TableVO;
import com.tykj.workflowcore.model_layer.service.ModelService;
......@@ -11,6 +12,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
......@@ -43,11 +45,10 @@ public class ModelController {
**/
@ApiOperation("得到所有数据表信息")
@RequestMapping("/getAllEntity")
public ResponseEntity getAllEntity() {
List<TableInfo> tableInfos = null;
public ResponseEntity getAllEntity(SearchTableInfoVo searchTableInfoVo) {
Page<TableInfo> tableInfos = null;
try {
tableInfos = modelService.ListAllEntities();
tableInfos = modelService.listAllEntities(searchTableInfoVo);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
......@@ -80,16 +81,14 @@ public class ModelController {
**/
@ApiOperation("新增数据模型")
@PostMapping("/addModel")
//入参使用VO直接接收即可
//jsonStr -> TableVo
public ResponseEntity addModel(@RequestBody TableVO tableVO) throws Exception {
List<TableInfo> tableInfos = modelService.ListAllEntities();
List<TableInfo> tableInfos = modelService.listAllEntities();
for (TableInfo tableInfo : tableInfos) {
if (tableVO.getModelName().equals(tableInfo.getName())) {
return ResultUtil.failed("表已经存在!");
}
}
modelService.NewTable(tableVO);
modelService.newTable(tableVO);
return ResultUtil.success("","新建成功");
}
......
......@@ -24,7 +24,10 @@ public class ColumnInfo {
@Column(name = "id")
private long id;
//0是,1否
/**
* 0是,1否
*/
@Column(name = "primary_key")
private Integer primarykey;
@Column(name = "name")
......
......@@ -18,7 +18,7 @@ public class ColumnVO {
private String filedType;
private String fieldName;
private String filedDescription;
private int filedLength;
private Integer filedLength;
private Integer primarykey=1;
}
......@@ -4,6 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author HASEE
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
......
package com.tykj.workflowcore.model_layer.model;
import com.tykj.workflowcore.base.page.JpaCustomPage;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName SearchTableInfoVo
* @Description TODO
* @Author WWW
* @Date 2021/3/11 16:06
* @Version 1.0
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class SearchTableInfoVo extends JpaCustomPage {
private String tableName;
}
......@@ -42,7 +42,10 @@ public class TableInfo implements Serializable {
@Column(name = "description")
private String desc;
//0是扫描,1是自建
/**
*0是扫描,1是自建
*/
@Column(name = "type")
private Integer type;
......@@ -51,7 +54,7 @@ public class TableInfo implements Serializable {
@Lob
@Column(name = "xml")
private String XML;
private String xml;
@Column(name = "update_time")
private Date updateTime;
......
......@@ -3,10 +3,8 @@ package com.tykj.workflowcore.model_layer.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.tykj.workflowcore.model_layer.model.ColumnInfo;
import com.tykj.workflowcore.model_layer.model.QueryCondition;
import com.tykj.workflowcore.model_layer.model.TableInfo;
import com.tykj.workflowcore.model_layer.model.TableVO;
import com.tykj.workflowcore.model_layer.model.*;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -22,24 +20,67 @@ import java.util.Map;
* @Date 2021/2/26 13:36
* @Version 1.0
*/
@Transactional
@Service
public interface ModelService {
List<TableInfo> ListAllEntities() throws SQLException;
/**
* 分页查询
* @param searchTableInfoVo
* @return
* @throws SQLException
*/
Page<TableInfo> listAllEntities(SearchTableInfoVo searchTableInfoVo) throws SQLException;
/**
* 方法重载不分页查询
* @return
* @throws SQLException
*/
List<TableInfo> listAllEntities() throws SQLException;
List<ColumnInfo> showModelFields(String ModelName);
TableVO addModel(String tableVO) throws JsonProcessingException;
/**
* 根据表名查询所有字段信息
* @param modelName
* @return
*/
List<ColumnInfo> showModelFields(String modelName);
TableVO NewTable(TableVO tableVO);
/**
* 新建模型
* @param tableVO
* @return
*/
TableVO newTable(TableVO tableVO);
/**
* 根据表名插入数据
* @param map
* @return
*/
int putValueByEntityName(Map<String, Object> map);
/**
* 扫描
* @param classList
*/
void swaggerScan(List<Class<?>> classList);
/**
* 根据表名查询所有
* @param name
* @return
* @throws SQLException
*/
List findAllByName(String name) throws SQLException;
/**
* 复杂查询
* @param tableName
* @param queryConditions
* @return
*/
List complexQuery(String tableName, List<QueryCondition> queryConditions);
}
package com.tykj.workflowcore.model_layer.service.impl;
import cn.hutool.db.Db;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Specifications;
import com.tykj.workflowcore.model_layer.annotatiion.WorkFlowCoreNoScan;
import com.tykj.workflowcore.model_layer.dao.ColumnInfoDao;
import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
......@@ -23,6 +22,7 @@ import org.hibernate.type.StringType;
import org.hibernate.type.TimestampType;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -51,7 +51,7 @@ import static com.tykj.workflowcore.model_layer.utils.HqlUtil.createQuery;
* @Date 2021/2/26 13:39
* @Version 1.0
*/
@Transactional
@Service
@Slf4j
public class ModelImpl implements ModelService {
......@@ -74,48 +74,34 @@ public class ModelImpl implements ModelService {
* @Date 16:14 2021/3/5
**/
@Override
public List<TableInfo> ListAllEntities() {
List<TableInfo> all = tableInfoDao.findAll();
return all;
public Page<TableInfo> listAllEntities( SearchTableInfoVo searchTableInfoVo) {
PredicateBuilder<TableInfo> and = Specifications.and();
and.eq(searchTableInfoVo.getTableName()!=null,"name",searchTableInfoVo.getTableName());
return tableInfoDao.findAll(and.build(), searchTableInfoVo.getPageable());
}
/**
* @param TableName
* @param tableName
* @return java.util.List<com.tykj.workflowcore.model_layer.model.ColumnInfo>
* @Author WWW
* @Description 根据表名得到所有字段名
* @Date 16:15 2021/3/5
**/
@Override
public List<ColumnInfo> showModelFields(String TableName) {
public List<ColumnInfo> showModelFields(String tableName) {
Specification specification = (Specification) (root, criteriaQuery, criteriaBuilder) -> {
Path db_name = root.get("dbName");
Predicate equal = criteriaBuilder.equal(db_name, TableName);
Path dbName = root.get("dbName");
Predicate equal = criteriaBuilder.equal(dbName, tableName);
return equal;
};
List<ColumnInfo> all = columnInfoDao.findAll(specification);
return all;
}
/**
* @param jsonStr
* @return com.tykj.workflowcore.model_layer.model.TableVO
* @Author WWW
* @Description 将json字符串转换实体类
* @Date 16:15 2021/3/5
**/
@Override
public TableVO addModel(String jsonStr) {
ObjectMapper mapper = new ObjectMapper();
TableVO tableVO = null;
try {
tableVO = mapper.readValue(jsonStr, TableVO.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return tableVO;
}
/**
* @param tableVO
......@@ -125,17 +111,17 @@ public class ModelImpl implements ModelService {
* @Date 16:16 2021/3/5
**/
@Override
public TableVO NewTable(TableVO tableVO) {
public TableVO newTable(TableVO tableVO) {
String XML_MAPPING = CreatTable(tableVO);
String xmlMapping = creatTable(tableVO);
CreatTableUtil creatTableUtil = new CreatTableUtil();
Session session = creatTableUtil.getSession(entityManagerFactory, XML_MAPPING);
Session session = creatTableUtil.getSession(entityManagerFactory, xmlMapping);
List<ColumnVO> dataList = tableVO.getDataList();
TableInfo tableInfo = new TableInfo();
tableInfo.setName(tableVO.getModelName()+"_");
tableInfo.setCnName(tableVO.getModelTitle());
tableInfo.setXML(XML_MAPPING);
tableInfo.setXml(xmlMapping);
tableInfo.setType(2);
tableInfoDao.save(tableInfo);
......@@ -181,12 +167,12 @@ public class ModelImpl implements ModelService {
Object values = map.get(tableName);
if (values instanceof Map) {
//插入数据
insertValue(tableInfo.getName(), tableInfo.getXML(), (Map) values);
insertValue(tableInfo.getName(), tableInfo.getXml(), (Map) values);
} else {
//循环插入数据
List valuesList = (List) values;
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));
}
}
}
......@@ -230,7 +216,7 @@ public class ModelImpl implements ModelService {
}
if (propertyType instanceof StringType) {
//然后调用强转方法
propertyNames[i] = propertyNames[i] + "";
propertyNames[i] = propertyNames[i];
map.put(propertyNames[i], propertyNames[i]);
}
}
......@@ -253,16 +239,16 @@ public class ModelImpl implements ModelService {
//获得类中文描述
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() + "|");
StringBuilder apiModelDocument = new StringBuilder();
if (annotation.value() != null && !"".equals(annotation.value())) {
apiModelDocument.append(annotation.value() + "|");
}
if (annotation.description() != null && !annotation.description().equals("")) {
APiModelDocument.append(annotation.description() + "|");
if (annotation.description() != null && !"".equals(annotation.description())) {
apiModelDocument.append(annotation.description() + "|");
}
tableVO.setModelTitle(APiModelDocument.toString());
tableVO.setModelTitle(apiModelDocument.toString());
} else {
tableVO.setModelTitle("");
}
......@@ -284,25 +270,25 @@ public class ModelImpl implements ModelService {
//获得属性中文描述
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() + "|");
StringBuilder apiModelPropertyDocument = new StringBuilder();
if (annotation.value() != null && !"".equals(annotation.value())) {
apiModelPropertyDocument.append(annotation.value() + "|");
}
if (annotation.example() != null && !annotation.example().equals("")) {
if (annotation.example() != null && !"".equals(annotation.example())) {
ApiModelPropertyDocument.append(annotation.example() + "|");
apiModelPropertyDocument.append(annotation.example() + "|");
}
columnVO.setFiledDescription(ApiModelPropertyDocument.toString());
columnVO.setFiledDescription(apiModelPropertyDocument.toString());
} else {
columnVO.setFiledDescription("无描述");
}
list.add(columnVO);
}
tableVO.setDataList(list);
String xml = CreatTable(tableVO);
String xml = creatTable(tableVO);
tableInfo.setName(tableVO.getModelName());
tableInfo.setCnName(tableVO.getModelTitle());
tableInfo.setXML(xml);
tableInfo.setXml(xml);
tableInfo.setType(0);
//判断是否存在
if (checkRepeat(realName)){
......@@ -316,12 +302,12 @@ public class ModelImpl implements ModelService {
columnInfo.setCnName(columnVO.getFiledDescription());
columnInfo.setPrimarykey(columnVO.getPrimarykey());
//暂定 简单类型
if (genericType.toString().equals("class java.lang.String")) {
if ("class java.lang.String".equals(genericType.toString())){
columnInfo.setLength(255);
} else if (genericType.toString().equals("class java.lang.Integer")&&genericType.toString().equals("int")) {
} else if ("class java.lang.Integer".equals(genericType.toString())) {
columnInfo.setLength(11);
} else if (genericType.toString().equals("class java.lang.Double")) {
} else if ("class java.lang.Double".equals(genericType.toString())) {
columnInfo.setLength(10);
}
else {
......@@ -386,7 +372,7 @@ public class ModelImpl implements ModelService {
@Override
public List complexQuery(String tableName, List<QueryCondition> queryConditions) {
List<cn.hutool.db.Entity> list=null;
if (!tableName.equals("")){
if (!"".equals(tableName)){
String query = createQuery(tableName, queryConditions);
try {
list = Db.use().query(query);
......@@ -400,4 +386,8 @@ public class ModelImpl implements ModelService {
return list ;
}
@Override
public List<TableInfo> listAllEntities() {
return tableInfoDao.findAll();
}
}
package com.tykj.workflowcore.model_layer.utils;
import cn.hutool.json.XML;
import com.tykj.workflowcore.model_layer.annotatiion.WorkFlowCoreNoScan;
import com.tykj.workflowcore.model_layer.model.ColumnVO;
import com.tykj.workflowcore.model_layer.model.TableInfo;
......@@ -32,38 +33,38 @@ import java.util.List;
* @Version 1.0
*/
public class CreatTableUtil {
public static String CreatTable(TableVO tableVO){
public static String creatTable(TableVO tableVO){
List<ColumnVO> dataList = tableVO.getDataList();
String XML_MAPPING = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
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=\"" + tableVO.getModelName() + "\" table=\"" + tableVO.getModelName() + "\">\n";
XML_MAPPING += " <id name=\"id\" type=\"java.lang.Long\" length=\"64\" unsaved-value=\"null\">\n" +
xmlMapping += " <id name=\"id\" type=\"java.lang.Long\" length=\"64\" unsaved-value=\"null\">\n" +
" <generator class=\"identity\" />\n" +
" </id>";
for (ColumnVO columnVO : dataList) {
XML_MAPPING +=
xmlMapping +=
" <property type=\"" + columnVO.getFiledType() + "\" name=\"" + columnVO.getFieldName() +
"\" column=\"" + columnVO.getFieldName() + "\"/>\n";
}
XML_MAPPING += " </class>" +
xmlMapping += " </class>" +
"</hibernate-mapping>";
return XML_MAPPING;
return xmlMapping;
}
public Session getSession(EntityManagerFactory entityManagerFactory,String XML){
public Session getSession(EntityManagerFactory entityManagerFactory,String xml){
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
StandardServiceRegistry serviceRegistry = sessionFactory.getSessionFactoryOptions().getServiceRegistry();
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
sessionFactory.getSessionFactoryOptions();
metadataSources.addInputStream(new ByteArrayInputStream(XML.getBytes()));
metadataSources.addInputStream(new ByteArrayInputStream(xml.getBytes()));
Metadata metadata = metadataSources.buildMetadata();
//更新数据库Schema,如果不存在就创建表,存在就更新字段,不会影响已有数据
SchemaUpdate schemaUpdate = new SchemaUpdate();
......@@ -79,13 +80,13 @@ public class CreatTableUtil {
}
//处理.
public static String getClassName(String aClass){
int i = aClass.lastIndexOf(".");
String substring = aClass.substring(i+1);
return substring;
}
// 处理带空格的class
public static String getTypeName(String aClass){
return aClass.replace("class ", "");
......
......@@ -5,6 +5,9 @@ import com.tykj.workflowcore.model_layer.model.QueryCondition;
import java.util.List;
import java.util.Objects;
/**
* @author HASEE
*/
public class HqlUtil {
public static String createQuery(String tableName, List<QueryCondition> conditions) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论