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

[数据模型] 优化返回信息,增加了逻辑类型判断,增加了缓存。

上级 e0888e6e
package com.tykj.workflowcore.model_layer.config;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Collections;
/**
* @Author WWW
* @Description
* @Date 10:47 2021/3/26
* @return
**/
@Configuration
@EnableCaching
public class MyCacheConfig {
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Collections.singletonList(new ConcurrentMapCache("tableInfos")));
return cacheManager;
}
}
...@@ -11,6 +11,7 @@ import io.swagger.annotations.ApiOperation; ...@@ -11,6 +11,7 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -44,13 +45,13 @@ public class ModelController { ...@@ -44,13 +45,13 @@ public class ModelController {
@ApiOperation("得到所有数据表信息") @ApiOperation("得到所有数据表信息")
@PostMapping("/getAllEntity") @PostMapping("/getAllEntity")
public ResponseEntity getAllEntity(@RequestBody SearchTableInfoVo searchTableInfoVo) { public ResponseEntity getAllEntity(@RequestBody SearchTableInfoVo searchTableInfoVo) {
Page<TableInfo> tableInfos = null; Page<TableInfo> tableInfos;
try { try {
tableInfos = modelService.listAllEntities(searchTableInfoVo); tableInfos = modelService.listAllEntities(searchTableInfoVo);
} catch (SQLException throwables) { } catch (SQLException throwables) {
throwables.printStackTrace(); return ResultUtil.failed("因为某些神秘原因,数据查询失败!");
}
}
return ResultUtil.success(tableInfos, "数据查询成功!"); return ResultUtil.success(tableInfos, "数据查询成功!");
} }
...@@ -99,8 +100,11 @@ public class ModelController { ...@@ -99,8 +100,11 @@ public class ModelController {
@ApiOperation(value = "根据表名表插入数据") @ApiOperation(value = "根据表名表插入数据")
@PostMapping("/insertValues") @PostMapping("/insertValues")
public ResponseEntity insertValues(@RequestBody Map<String, Object> map) { public ResponseEntity insertValues(@RequestBody Map<String, Object> map) {
modelService.putValueByEntityName(map); int i = modelService.putValueByEntityName(map);
return ResultUtil.success("", "数据插入成功"); if (i==0){
return ResultUtil.success("", "数据插入成功!");
}
return ResultUtil.success("","模型类型不支持插入数据!");
} }
...@@ -134,22 +138,27 @@ public class ModelController { ...@@ -134,22 +138,27 @@ public class ModelController {
public ResponseEntity complexQuery(String tableName, List<QueryCondition> queryConditions) { public ResponseEntity complexQuery(String tableName, List<QueryCondition> queryConditions) {
List list = modelService.complexQuery(tableName, queryConditions); List list = modelService.complexQuery(tableName, queryConditions);
if (list != null) { if (list != null) {
return ResultUtil.success(list, "查询成功"); return ResultUtil.success(list, "查询成功!");
} }
return ResultUtil.success(null, "没有数据"); return ResultUtil.success(null, "没有数据!");
} }
@ApiOperation("编辑操作") @ApiOperation("编辑操作")
@PutMapping("/update") @PutMapping("/update")
public ResponseEntity updateTable(@RequestBody UpdateTableInfoVO updateTableInfoVO){ public ResponseEntity updateTable(@RequestBody UpdateTableInfoVO updateTableInfoVO){
modelService.updateTable(updateTableInfoVO); int i = modelService.updateTable(updateTableInfoVO);
if (i==1){
return ResultUtil.success("","修改成功!"); return ResultUtil.success("","修改成功!");
} }
return ResultUtil.success("未找到该表信息或者业务类型不支持修改!");
}
@ApiOperation("删除操作") @ApiOperation("删除操作")
@DeleteMapping("/delete") @DeleteMapping("/delete")
public ResponseEntity delTable(DelTableVO delTableVO) throws SQLException { public ResponseEntity delTable( DelTableVO delTableVO) {
modelService.delTable(delTableVO);
return ResultUtil.success(modelService.findAllByName(delTableVO.getDbName()),"删除成功!");
}
int i = modelService.delTable(delTableVO);
if (i==1){
return ResultUtil.success("", "删除成功!");
}
return ResultUtil.success("未找到该id的表信息或者业务类型不支持删除!");
}
} }
...@@ -4,10 +4,11 @@ package com.tykj.workflowcore.model_layer.service; ...@@ -4,10 +4,11 @@ package com.tykj.workflowcore.model_layer.service;
import com.tykj.workflowcore.model_layer.entity.ColumnInfo; import com.tykj.workflowcore.model_layer.entity.ColumnInfo;
import com.tykj.workflowcore.model_layer.entity.TableInfo; import com.tykj.workflowcore.model_layer.entity.TableInfo;
import com.tykj.workflowcore.model_layer.entity.vo.*; import com.tykj.workflowcore.model_layer.entity.vo.*;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -28,8 +29,10 @@ public interface ModelService { ...@@ -28,8 +29,10 @@ public interface ModelService {
* @return * @return
* @throws SQLException * @throws SQLException
*/ */
@Cacheable(cacheNames = "tableInfos",key ="#searchTableInfoVo" )
Page<TableInfo> listAllEntities(SearchTableInfoVo searchTableInfoVo) throws SQLException; Page<TableInfo> listAllEntities(SearchTableInfoVo searchTableInfoVo) throws SQLException;
/** /**
* 方法重载不分页查询 * 方法重载不分页查询
* @return * @return
...@@ -38,14 +41,17 @@ public interface ModelService { ...@@ -38,14 +41,17 @@ public interface ModelService {
List<TableInfo> listAllEntities() throws SQLException; List<TableInfo> listAllEntities() throws SQLException;
/** /**
* 根据表名查询所有字段信息 * 根据表名查询所有字段信息
* @param searchColumnInfoVo * @param searchColumnInfoVo
* @return * @return
*/ */
@Cacheable(cacheNames = "tableInfos",key ="#searchColumnInfoVo" )
List<ColumnInfo> showModelFields(SearchColumnInfoVo searchColumnInfoVo); List<ColumnInfo> showModelFields(SearchColumnInfoVo searchColumnInfoVo);
/** /**
* 新建模型 * 新建模型
* @param tableVO * @param tableVO
...@@ -88,14 +94,17 @@ public interface ModelService { ...@@ -88,14 +94,17 @@ public interface ModelService {
* 更新表 * 更新表
* @param updateTableInfoVO * @param updateTableInfoVO
*/ */
void updateTable(UpdateTableInfoVO updateTableInfoVO); @CachePut(cacheNames = "tableInfos")
int updateTable(UpdateTableInfoVO updateTableInfoVO);
/** /**
* 删除表 * 删除表
* @param delTableVO * @param delTableVO
* @return
*/ */
void delTable(DelTableVO delTableVO); @CacheEvict(cacheNames = "tableInfos")
int delTable(DelTableVO delTableVO);
} }
...@@ -2,6 +2,8 @@ package com.tykj.workflowcore.model_layer.service.impl; ...@@ -2,6 +2,8 @@ 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.tykj.workflowcore.base.result.ResultObj;
import com.tykj.workflowcore.base.result.ResultUtil;
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;
import com.tykj.workflowcore.model_layer.dao.TableInfoDao; import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
...@@ -24,11 +26,13 @@ import org.hibernate.type.StringType; ...@@ -24,11 +26,13 @@ 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.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
...@@ -37,6 +41,7 @@ import javax.persistence.EntityManagerFactory; ...@@ -37,6 +41,7 @@ import javax.persistence.EntityManagerFactory;
import javax.persistence.criteria.*; import javax.persistence.criteria.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.sql.SQLException;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
...@@ -182,7 +187,8 @@ public class ModelImpl implements ModelService { ...@@ -182,7 +187,8 @@ public class ModelImpl implements ModelService {
if (one.isPresent()) { if (one.isPresent()) {
tableInfo = (TableInfo) one.get(); tableInfo = (TableInfo) one.get();
} }
Integer modelType = tableInfo.getModelType();
if (modelType==1){
Object values = map.get(tableName); Object values = map.get(tableName);
if (values instanceof Map) { if (values instanceof Map) {
//插入数据 //插入数据
...@@ -195,6 +201,11 @@ public class ModelImpl implements ModelService { ...@@ -195,6 +201,11 @@ public class ModelImpl implements ModelService {
} }
} }
} }
else {
return 1;
}
}
return 0; return 0;
} }
...@@ -238,7 +249,7 @@ public class ModelImpl implements ModelService { ...@@ -238,7 +249,7 @@ public class ModelImpl implements ModelService {
map.put(propertyNames[i], value); map.put(propertyNames[i], value);
} }
} }
newSession.save(tableName,map); newSession.save(tableName, map);
newSession.getTransaction().begin(); newSession.getTransaction().begin();
newSession.getTransaction().commit(); newSession.getTransaction().commit();
newSession.close(); newSession.close();
...@@ -246,6 +257,7 @@ public class ModelImpl implements ModelService { ...@@ -246,6 +257,7 @@ public class ModelImpl implements ModelService {
/** /**
* 扫描表 * 扫描表
*
* @param classList * @param classList
*/ */
@Override @Override
...@@ -346,7 +358,7 @@ public class ModelImpl implements ModelService { ...@@ -346,7 +358,7 @@ public class ModelImpl implements ModelService {
* @Date 10:50 2021/3/11 * @Date 10:50 2021/3/11
**/ **/
public boolean checkRepeat(String tableName) { public boolean checkRepeat(String tableName) {
Specification spec = (Specification) (root, criteriaQuery, criteriaBuilder) -> { Specification spec = (root, criteriaQuery, criteriaBuilder) -> {
Path name = root.get("modelName"); Path name = root.get("modelName");
Predicate equal = criteriaBuilder.equal(name, tableName); Predicate equal = criteriaBuilder.equal(name, tableName);
return equal; return equal;
...@@ -390,17 +402,19 @@ public class ModelImpl implements ModelService { ...@@ -390,17 +402,19 @@ public class ModelImpl implements ModelService {
@Override @Override
public List<TableInfo> listAllEntities() { public List<TableInfo> listAllEntities() {
System.out.println("从数据库中取出的!");
return tableInfoDao.findAll(); return tableInfoDao.findAll();
} }
@Override @Override
public void updateTable(UpdateTableInfoVO updateTableInfoVO) { public int updateTable(UpdateTableInfoVO updateTableInfoVO) {
//tableInfo和columnInfo变化 //tableInfo和columnInfo变化
//查询到TableInfo和ColumnInfo //查询到TableInfo和ColumnInfo
TableVO tableVO = updateTableInfoVO.getTableVO(); TableVO tableVO = updateTableInfoVO.getTableVO();
Integer dbId = updateTableInfoVO.getDbId(); Integer dbId = updateTableInfoVO.getDbId();
TableInfo tableInfo = tableInfoDao.findById(dbId).orElseThrow(() -> new RuntimeException("未找到该id的表信息")); TableInfo tableInfo = tableInfoDao.findById(dbId).orElseThrow(() -> new RuntimeException("未找到该id的表信息"));
Integer modelType = tableInfo.getModelType();
if (modelType==1){
tableInfo.setUpdatedTime(new Date()); tableInfo.setUpdatedTime(new Date());
tableInfo.setDescription(tableVO.getDescription()); tableInfo.setDescription(tableVO.getDescription());
...@@ -443,6 +457,9 @@ public class ModelImpl implements ModelService { ...@@ -443,6 +457,9 @@ public class ModelImpl implements ModelService {
for (ColumnInfo originColumnInfo : columnsFordDelete) { for (ColumnInfo originColumnInfo : columnsFordDelete) {
columnInfoDao.delete(originColumnInfo); columnInfoDao.delete(originColumnInfo);
} }
}
return 1;
} }
/** /**
...@@ -534,20 +551,25 @@ public class ModelImpl implements ModelService { ...@@ -534,20 +551,25 @@ public class ModelImpl implements ModelService {
} }
@Override @Override
public void delTable(DelTableVO delTableVO) { public int delTable(DelTableVO delTableVO) {
if (StringUtils.isNotEmpty(delTableVO.getDbName())) { if (StringUtils.isNotEmpty(delTableVO.getDbName())) {
Optional<TableInfo> byId = tableInfoDao.findById(delTableVO.getId()); Optional<TableInfo> byId = tableInfoDao.findById(delTableVO.getId());
if (byId.isPresent()) { if (byId.isPresent()) {
tableInfoDao.delete(byId.get()); TableInfo tableInfo = byId.get();
Integer modelType = tableInfo.getModelType();
if (modelType == 1) {
tableInfoDao.delete(tableInfo);
List<ColumnInfo> allByDbId = columnInfoDao.findAllByDbId(delTableVO.getId()); List<ColumnInfo> allByDbId = columnInfoDao.findAllByDbId(delTableVO.getId());
columnInfoDao.deleteInBatch(allByDbId); columnInfoDao.deleteInBatch(allByDbId);
jdbcTemplate.execute("drop table " + delTableVO.getDbName());
jdbcTemplate.execute("truncate table " + delTableVO.getDbName()); return 1;
} else {
new RuntimeException("未找到该id的表信息");
} }
} }
} }
return 0;
}
} }
...@@ -3,7 +3,7 @@ package com.tykj.workflowcore; ...@@ -3,7 +3,7 @@ package com.tykj.workflowcore;
import com.tykj.workflowcore.model_layer.controller.ModelController; import com.tykj.workflowcore.model_layer.controller.ModelController;
import com.tykj.workflowcore.model_layer.dao.TableInfoDao; import com.tykj.workflowcore.model_layer.dao.TableInfoDao;
import com.tykj.workflowcore.model_layer.entity.TableInfo; import com.tykj.workflowcore.model_layer.service.ModelService;
import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -17,6 +17,7 @@ import org.springframework.test.web.servlet.RequestBuilder; ...@@ -17,6 +17,7 @@ import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
...@@ -42,6 +43,9 @@ class WorkflowCoreApplicationTests { ...@@ -42,6 +43,9 @@ class WorkflowCoreApplicationTests {
@Autowired @Autowired
MockMvc mockMvc; MockMvc mockMvc;
@Autowired
ModelService modelService;
@Before("") @Before("")
public void setUp() { public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(modelController).build(); mockMvc = MockMvcBuilders.standaloneSetup(modelController).build();
...@@ -56,11 +60,15 @@ class WorkflowCoreApplicationTests { ...@@ -56,11 +60,15 @@ class WorkflowCoreApplicationTests {
RequestBuilder request; RequestBuilder request;
/**
* 测试表的条件查询
* @throws Exception
*/
@Test @Test
public void SearchTableVOTest() throws Exception { public void SearchTableVOTest() throws Exception {
String SearchTableinfo_Json = "{\n" + String SearchTableinfo_Json = "{\n" +
" \"modelName\":\"peop\",\n" + " \"modelName\":\"\",\n" +
" \"modelTitle\":\"\",\n" + " \"modelTitle\":\"\",\n" +
" \"modelType\":\"\"\n" + " \"modelType\":\"\"\n" +
"}"; "}";
...@@ -76,6 +84,10 @@ class WorkflowCoreApplicationTests { ...@@ -76,6 +84,10 @@ class WorkflowCoreApplicationTests {
} }
/**
* 测试列条件查询
* @throws Exception
*/
@Test @Test
public void testSearchColumnVO() throws Exception { public void testSearchColumnVO() throws Exception {
...@@ -84,7 +96,7 @@ class WorkflowCoreApplicationTests { ...@@ -84,7 +96,7 @@ class WorkflowCoreApplicationTests {
"}"; "}";
String searchColumnV0_json2 = "{\n" + String searchColumnV0_json2 = "{\n" +
"\"dbName\":\"techer\"\n" + "\"dbName\":\"sy_order\"\n" +
"}"; "}";
request = post("/model/getAllField/") request = post("/model/getAllField/")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
...@@ -98,6 +110,10 @@ class WorkflowCoreApplicationTests { ...@@ -98,6 +110,10 @@ class WorkflowCoreApplicationTests {
.andDo(print());//打印输出结果 .andDo(print());//打印输出结果
} }
/**
* 测试新增对象
* @throws Exception
*/
@Test @Test
public void addModelTest() throws Exception { public void addModelTest() throws Exception {
...@@ -125,8 +141,12 @@ class WorkflowCoreApplicationTests { ...@@ -125,8 +141,12 @@ class WorkflowCoreApplicationTests {
.andDo(print());//打印输出结果 .andDo(print());//打印输出结果
} }
/**
* 测试插入数据
* @throws Exception
*/
@Test @Test
public void inserValuesByTableName() throws Exception { public void insertValuesByTableName() throws Exception {
String values_json="{\"techer\":{\"name\": \"zhangsan\",\"age\" :\"20\"}}"; String values_json="{\"techer\":{\"name\": \"zhangsan\",\"age\" :\"20\"}}";
request = post("/model/insertValues/") request = post("/model/insertValues/")
...@@ -142,8 +162,12 @@ class WorkflowCoreApplicationTests { ...@@ -142,8 +162,12 @@ class WorkflowCoreApplicationTests {
.andDo(print());//打印输出结果 .andDo(print());//打印输出结果
} }
/**
* 测试查询全部
* @throws Exception
*/
@Test @Test
public void findAllbyTableName() throws Exception { public void findAllByTableName() throws Exception {
request=get("/model/getAll/") request=get("/model/getAll/")
.param("tableName","techer") .param("tableName","techer")
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE); .accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
...@@ -156,15 +180,21 @@ class WorkflowCoreApplicationTests { ...@@ -156,15 +180,21 @@ class WorkflowCoreApplicationTests {
} }
/**
* 测试删除表
*/
@Test @Test
public void testDelTable(){ public void testDelTable() throws Exception {
// jdbcTemplate.execute("drop table if exists test"); ok String delete_JSON="{\n" +
Optional<TableInfo> byId = tableInfoDao.findById(12); "\"dbName\":\"testentity\"\n" +
if (byId.isPresent()){ "}";
TableInfo tableInfo = byId.get(); request=delete("/model/delete/")
System.out.println(tableInfo); .contentType(MediaType.APPLICATION_JSON)
tableInfoDao.delete(tableInfo); .content(delete_JSON)
} .accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
mockMvc.perform(request)
.andExpect(status().isOk())
.andExpect(content().string(not("")))
.andDo(print());
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论