From b08775dbb9dad2caf04526920cfeb93e7bbc7ead Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Tue, 1 Dec 2020 21:07:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/ApiDefinitionService.java | 42 ++-- .../db/migration/V46__api_definition.sql | 2 +- .../api/definition/components/ApiModule.vue | 2 +- .../components/import/ApiImport.vue | 200 ++++++------------ 4 files changed, 83 insertions(+), 163 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 2e581d296a..05f3665256 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -19,11 +19,15 @@ import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper; import io.metersphere.commons.constants.APITestStatus; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.exception.MSException; +import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.ServiceUtils; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.i18n.Translator; import io.metersphere.service.FileService; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; import org.apache.jorphan.collections.HashTree; import org.aspectj.util.FileUtil; import org.springframework.stereotype.Service; @@ -55,6 +59,8 @@ public class ApiDefinitionService { private ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper; @Resource private JMeterService jMeterService; + @Resource + private SqlSessionFactory sqlSessionFactory; private static Cache cache = Cache.newHardMemoryCache(0, 3600 * 24); @@ -218,39 +224,27 @@ public class ApiDefinitionService { return test; } - private ApiDefinition createTest(ApiDefinitionResult request) { + private ApiDefinition createTest(ApiDefinitionResult request, ApiDefinitionMapper batchMapper) { SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest(); + BeanUtils.copyBean(saveReq, request); saveReq.setId(UUID.randomUUID().toString()); - saveReq.setName(request.getName()); - saveReq.setProtocol(request.getProtocol()); - saveReq.setProjectId(request.getProjectId()); - saveReq.setPath(request.getPath()); checkNameExist(saveReq); final ApiDefinitionWithBLOBs test = new ApiDefinitionWithBLOBs(); - test.setId(request.getId()); - test.setName(request.getName()); - test.setProtocol(request.getProtocol()); - test.setMethod(request.getMethod()); - test.setPath(request.getPath()); - test.setModuleId(request.getModuleId()); - test.setProjectId(request.getProjectId()); - test.setRequest(request.getRequest()); + BeanUtils.copyBean(test, request); test.setCreateTime(System.currentTimeMillis()); test.setUpdateTime(System.currentTimeMillis()); test.setStatus(APITestStatus.Underway.name()); - test.setModulePath(request.getModulePath()); - test.setResponse(request.getResponse()); - test.setEnvironmentId(request.getEnvironmentId()); if (request.getUserId() == null) { test.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); } else { test.setUserId(request.getUserId()); } test.setDescription(request.getDescription()); - apiDefinitionMapper.insert(test); + batchMapper.insert(test); return test; } + private void deleteFileByTestId(String apiId) { ApiTestFileExample apiTestFileExample = new ApiTestFileExample(); apiTestFileExample.createCriteria().andTestIdEqualTo(apiId); @@ -337,15 +331,23 @@ public class ApiDefinitionService { } private void importApiTest(ApiTestImportRequest importRequest, ApiDefinitionImport apiImport) { - apiImport.getData().forEach(item -> { + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class); + List data = apiImport.getData(); + for (int i = 0; i < data.size(); i++) { + ApiDefinitionResult item = data.get(i); item.setProjectId(importRequest.getProjectId()); item.setModuleId(importRequest.getModuleId()); item.setModulePath(importRequest.getModulePath()); item.setEnvironmentId(importRequest.getEnvironmentId()); item.setId(UUID.randomUUID().toString()); item.setUserId(null); - createTest(item); - }); + createTest(item, batchMapper); + if (i % 300 == 0) { + sqlSession.flushStatements(); + } + } + sqlSession.flushStatements(); } } diff --git a/backend/src/main/resources/db/migration/V46__api_definition.sql b/backend/src/main/resources/db/migration/V46__api_definition.sql index cf07a28858..91049386b7 100644 --- a/backend/src/main/resources/db/migration/V46__api_definition.sql +++ b/backend/src/main/resources/db/migration/V46__api_definition.sql @@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS `api_module` ( CREATE TABLE IF NOT EXISTS `api_definition` ( `id` varchar(50) NOT NULL COMMENT 'Test ID', `project_id` varchar(50) NOT NULL COMMENT 'Project ID this test belongs to', - `name` varchar(64) NOT NULL COMMENT 'Test name', + `name` varchar(255) NOT NULL COMMENT 'Test name', `method` varchar(64) NOT NULL COMMENT 'method', `protocol` varchar(255) NOT NULL COMMENT 'request protocol', `path` varchar(255) DEFAULT NULL COMMENT 'request path', diff --git a/frontend/src/business/components/api/definition/components/ApiModule.vue b/frontend/src/business/components/api/definition/components/ApiModule.vue index 735b018bf9..c8994cbc27 100644 --- a/frontend/src/business/components/api/definition/components/ApiModule.vue +++ b/frontend/src/business/components/api/definition/components/ApiModule.vue @@ -95,7 +95,7 @@ - + diff --git a/frontend/src/business/components/api/definition/components/import/ApiImport.vue b/frontend/src/business/components/api/definition/components/import/ApiImport.vue index 1097907aa0..e2cfbe95f9 100644 --- a/frontend/src/business/components/api/definition/components/import/ApiImport.vue +++ b/frontend/src/business/components/api/definition/components/import/ApiImport.vue @@ -1,5 +1,6 @@