提交 195dab58 authored 作者: ww1xhqc's avatar ww1xhqc

[数据模型] 优化细节,新增删除表操作,新增测试

上级 abaa9837
......@@ -2,9 +2,8 @@ package com.tykj.workflowcore.base.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.*;
/**
* @author zsp
......@@ -17,25 +16,25 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
.maxAge(3600);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/xml/**")
.addResourceLocations("file:"+System.getProperty("user.dir")+"\\xml\\");
}
};
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
.maxAge(3600);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/xml/**")
.addResourceLocations("file:" + System.getProperty("user.dir") + "\\xml\\");
}
};
}
}
......@@ -3,11 +3,8 @@ package com.tykj.workflowcore.model_layer.controller;
import com.tykj.workflowcore.base.result.ResultUtil;
import com.tykj.workflowcore.model_layer.entity.vo.QueryCondition;
import com.tykj.workflowcore.model_layer.entity.vo.SearchColumnInfoVo;
import com.tykj.workflowcore.model_layer.entity.vo.SearchTableInfoVo;
import com.tykj.workflowcore.model_layer.entity.vo.*;
import com.tykj.workflowcore.model_layer.entity.TableInfo;
import com.tykj.workflowcore.model_layer.entity.vo.TableVO;
import com.tykj.workflowcore.model_layer.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
......@@ -54,7 +51,7 @@ public class ModelController {
throwables.printStackTrace();
}
return ResultUtil.success(tableInfos, "");
return ResultUtil.success(tableInfos, "数据查询成功!");
}
/**
......@@ -67,7 +64,7 @@ public class ModelController {
@ApiOperation("根据表名或者id查询表中所有字段")
@PostMapping("/getAllField")
public ResponseEntity getFields(@RequestBody SearchColumnInfoVo searchColumnInfoVo) {
return ResultUtil.success(modelService.showModelFields(searchColumnInfoVo), "");
return ResultUtil.success(modelService.showModelFields(searchColumnInfoVo), "数据查询成功!");
}
/**
......@@ -78,7 +75,7 @@ public class ModelController {
* @Date 16:21 2021/3/4
**/
@ApiOperation("新增数据模型")
@PostMapping("/addModel")
@PostMapping(value = "/addModel")
public ResponseEntity addModel(@RequestBody TableVO tableVO) throws Exception {
List<TableInfo> tableInfos = modelService.listAllEntities();
for (TableInfo tableInfo : tableInfos) {
......@@ -103,7 +100,7 @@ public class ModelController {
@PostMapping("/insertValues")
public ResponseEntity insertValues(@RequestBody Map<String, Object> map) {
modelService.putValueByEntityName(map);
return ResultUtil.success("数据插入成功", "");
return ResultUtil.success("", "数据插入成功");
}
......@@ -118,7 +115,7 @@ public class ModelController {
@GetMapping("/getAll")
public ResponseEntity getAll(String tableName) {
try {
return ResultUtil.success(modelService.findAllByName(tableName), "");
return ResultUtil.success(modelService.findAllByName(tableName), "查询成功!");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
......@@ -135,12 +132,24 @@ public class ModelController {
@ApiOperation("复杂查询")
@GetMapping("/complexQuery")
public ResponseEntity complexQuery(String tableName, List<QueryCondition> queryConditions) {
List list = modelService.complexQuery(tableName, queryConditions);
if (list != null) {
return ResultUtil.success(list, "查询成功");
}
return ResultUtil.success(null, "没有数据");
}
@ApiOperation("编辑操作")
@PutMapping("/update")
public ResponseEntity updateTable(@RequestBody UpdateTableInfoVO updateTableInfoVO){
modelService.updateTable(updateTableInfoVO);
return ResultUtil.success("","修改成功!");
}
@ApiOperation("删除操作")
@DeleteMapping("/delete")
public ResponseEntity delTable(DelTableVO delTableVO) throws SQLException {
modelService.delTable(delTableVO);
return ResultUtil.success(modelService.findAllByName(delTableVO.getDbName()),"删除成功!");
}
}
......@@ -12,6 +12,5 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
* @Date 2021/2/24 11:20
* @Version 1.0
*/
public interface TableInfoDao extends JpaRepository<TableInfo,Integer>, JpaSpecificationExecutor<TableInfo> {
public interface TableInfoDao extends JpaRepository<TableInfo, Integer>, JpaSpecificationExecutor<TableInfo> {
}
......@@ -4,7 +4,10 @@ import com.tykj.workflowcore.base.entity.BaseEntity;
import com.tykj.workflowcore.model_layer.annotations.WorkFlowCoreNoScan;
import io.swagger.annotations.ApiModelProperty;
import liquibase.pro.packaged.A;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
......@@ -19,8 +22,11 @@ import java.io.Serializable;
* @Version 1.0
*/
@WorkFlowCoreNoScan
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@SQLDelete(sql = "update column_info set deleted = 1 where id = ?")
@Where(clause = "deleted = 0")
public class ColumnInfo extends BaseEntity {
......@@ -47,6 +53,17 @@ public class ColumnInfo extends BaseEntity {
private String dbName;
@ApiModelProperty("所属表id")
private Integer dbId;
@ApiModelProperty("详细描述")
private String description;
public ColumnInfo(Integer primaryKey, String fieldName, String fieldTitle, String fieldType, Integer fieldLength, String dbName, Integer dbId, String description) {
this.primaryKey = primaryKey;
this.fieldName = fieldName;
this.fieldTitle = fieldTitle;
this.fieldType = fieldType;
this.fieldLength = fieldLength;
this.dbName = dbName;
this.dbId = dbId;
this.description = description;
}
}
......@@ -27,5 +27,7 @@ public class ColumnVO {
private String fieldTitle;
@ApiModelProperty("字段长度,有默认值")
private Integer fieldLength;
@ApiModelProperty("详细描述")
private String description;
}
package com.tykj.workflowcore.model_layer.entity.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName DelTableVO
* @Description TODO
* @Author WWW
* @Date 2021/3/23 18:51
* @Version 1.0
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class DelTableVO {
private Integer id;
private String dbName;
}
......@@ -17,7 +17,7 @@ import lombok.NoArgsConstructor;
public class UpdateTableInfoVO {
private Integer dbId;
private String dbName;
private TableVO tableVO;
......
......@@ -84,8 +84,18 @@ public interface ModelService {
List<Map<String, Object>> complexQuery(String tableName, List<QueryCondition> queryConditions);
/**
* 更新表
* @param updateTableInfoVO
*/
void updateTable(UpdateTableInfoVO updateTableInfoVO);
/**
* 删除表
* @param delTableVO
*/
void delTable(DelTableVO delTableVO);
}
......@@ -28,6 +28,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.Entity;
import javax.persistence.EntityManagerFactory;
......@@ -136,15 +137,19 @@ public class ModelImpl implements ModelService {
tableInfo.setModelTitle(tableVO.getModelTitle());
tableInfo.setXml(xmlMapping);
tableInfo.setModelType(tableVO.getModelType());
tableInfo.setDescription(tableVO.getDescription());
tableInfo.setParentTable(parentTable);
tableInfoDao.save(tableInfo);
//默认存储ID字段
ColumnInfo cId = new ColumnInfo(0, "id", "主键", "java.lang.Integer", 11, tableInfo.getModelName(), tableInfo.getId(), "主键");
columnInfoDao.save(cId);
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.setDescription(columnVO.getDescription());
columnInfo.setDbName(tableInfo.getModelName());
columnInfo.setDbId(tableInfo.getId());
columnInfoDao.save(columnInfo);
......@@ -224,17 +229,19 @@ public class ModelImpl implements ModelService {
}
if (propertyType instanceof IntegerType) {
//然后调用强转方法
int i1 = Integer.parseInt(propertyNames[i]);
int i1 = Integer.valueOf(value + "");
map.put(propertyNames[i], i1);
}
if (propertyType instanceof StringType) {
//然后调用强转方法
propertyNames[i] = propertyNames[i];
map.put(propertyNames[i], propertyNames[i]);
map.put(propertyNames[i], value);
}
}
newSession.saveOrUpdate(tableName, map);
// newSession.save(tableName,map);
newSession.merge(tableName, map);
// newSession.flush();
newSession.close();
}
......@@ -523,4 +530,20 @@ public class ModelImpl implements ModelService {
return result;
}
@Override
public void delTable(DelTableVO delTableVO) {
if (StringUtils.isNotEmpty(delTableVO.getDbName())) {
Optional<TableInfo> byId = tableInfoDao.findById(delTableVO.getId());
if (byId.isPresent()) {
tableInfoDao.delete(byId.get());
List<ColumnInfo> allByDbId = columnInfoDao.findAllByDbId(delTableVO.getId());
columnInfoDao.deleteInBatch(allByDbId);
jdbcTemplate.execute("truncate table " + delTableVO.getDbName());
} else {
new RuntimeException("未找到该id的表信息");
}
}
}
}
......@@ -34,7 +34,7 @@ public class CreateTableUtil {
"<hibernate-mapping>\n" +
" <class entity-name=\"" + tableVO.getModelName() + "\" table=\"" + tableVO.getModelName() + "\">\n";
xmlMapping += " <id name=\"id\" type=\"java.lang.Integer\" length=\"64\" unsaved-value=\"null\">\n" +
xmlMapping += " <id name=\"id\" type=\"java.lang.Integer\" length=\"11\" unsaved-value=\"null\">\n" +
" <generator class=\"identity\" />\n" +
" </id>";
for (ColumnVO columnVO : dataList) {
......
package com.tykj.workflowcore;
import com.tykj.workflowcore.model_layer.controller.ModelController;
import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
import com.tykj.workflowcore.model_layer.entity.TableInfo;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
......@@ -8,12 +10,14 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import javax.print.attribute.standard.Media;
import java.util.Optional;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
......@@ -31,7 +35,13 @@ class WorkflowCoreApplicationTests {
ModelController modelController;
@Autowired
MockMvc mockMvc;
JdbcTemplate jdbcTemplate;
@Autowired
TableInfoDao tableInfoDao;
@Autowired
MockMvc mockMvc;
@Before("")
public void setUp() {
......@@ -50,14 +60,14 @@ class WorkflowCoreApplicationTests {
@Test
public void SearchTableVOTest() throws Exception {
String SearchTableinfo_Json="{\n" +
" \"modelName\":\"peop\",\n" +
" \"modelTitle\":\"\",\n" +
" \"modelType\":\"\"\n" +
"}";
String SearchTableinfo_Json = "{\n" +
" \"modelName\":\"peop\",\n" +
" \"modelTitle\":\"\",\n" +
" \"modelType\":\"\"\n" +
"}";
request = post("/model/getAllEntity/")
.contentType(MediaType.APPLICATION_JSON)
.content(SearchTableinfo_Json)
.content(SearchTableinfo_Json)
// 设置返回值类型为utf-8,否则默认为ISO-8859-1
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request).andExpect(status().isOk())
......@@ -70,11 +80,11 @@ String SearchTableinfo_Json="{\n" +
@Test
public void testSearchColumnVO() throws Exception {
String searchColumnV0_json="{\n" +
String searchColumnV0_json = "{\n" +
"\"dbId\":2\n" +
"}";
String searchColumnV0_json2= "{\n" +
String searchColumnV0_json2 = "{\n" +
"\"dbName\":\"techer\"\n" +
"}";
request = post("/model/getAllField/")
......@@ -82,12 +92,80 @@ String SearchTableinfo_Json="{\n" +
.content(searchColumnV0_json2)
// 设置返回值类型为utf-8,否则默认为ISO-8859-1
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request).andExpect(status().isOk())
.andExpect(content().string(not("")))
.andExpect(content().string(not("[]")))
.andDo(print());//打印输出结果
}
@Test
public void addModelTest() throws Exception {
String TableVo_json = "{\n" +
"\"modelName\":\"people2\",\n" +
"\"modelTitle\":\"人2\",\n" +
"\"modelType\":1,\n" +
"\"parentTable\":\"\",\n" +
"\"description\":\"详细信息\",\n" +
"\"dataList\":[\n" +
" {\"fieldName\": \"c1\", \"fieldTitle\": \"列1\", \"fieldType\": \"java.lang.String\", \"fieldLength\": \"255\"},\n" +
" {\"fieldName\": \"c2\", \"fieldTitle\": \"列2\", \"fieldType\": \"java.lang.String\", \"fieldLength\": \"255\"}\n" +
"]\n" +
"}";
request = post("/model/addModel/")
.contentType(MediaType.APPLICATION_JSON)
.content(TableVo_json)
// 设置返回值类型为utf-8,否则默认为ISO-8859-1
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request).andExpect(status().isOk())
.andExpect(content().string(not("")))
.andExpect(content().string(not("[]")))
.andExpect(content().string(equalTo("{\"message\":\"新建成功\",\"data\":\"\"}")))
.andDo(print());//打印输出结果
}
@Test
public void inserValuesByTableName() throws Exception {
String values_json="{\"techer\":{\"name\": \"zhangsan\",\"age\" :\"20\"}}";
request = post("/model/insertValues/")
.contentType(MediaType.APPLICATION_JSON)
.content(values_json)
// 设置返回值类型为utf-8,否则默认为ISO-8859-1
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request).andExpect(status().isOk())
.andExpect(content().string(not("")))
.andExpect(content().string(not("[]")))
.andExpect(content().string(equalTo("{\"message\":\"数据插入成功\",\"data\":\"\"}")))
.andDo(print());//打印输出结果
}
@Test
public void findAllbyTableName() throws Exception {
request=get("/model/getAll/")
.param("tableName","techer")
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string(not("")))
.andDo(print());
}
@Test
public void testDelTable(){
// jdbcTemplate.execute("drop table if exists test"); ok
Optional<TableInfo> byId = tableInfoDao.findById(12);
if (byId.isPresent()){
TableInfo tableInfo = byId.get();
System.out.println(tableInfo);
tableInfoDao.delete(tableInfo);
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论