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