From 62efd8ff9a73d463ed1e87c4d0351254664dc504 Mon Sep 17 00:00:00 2001 From: "HUOJIN\\92525" <925259474@qq.com> Date: Sun, 7 Sep 2025 20:52:40 +0800 Subject: [PATCH] no message --- application.pid | 1 + .../basicdata/rest/ItemController.java | 52 ++++++------ .../service/impl/ItemServiceImpl.java | 10 +-- .../youchain/utils/BatchCreateOrUpdate.java | 78 ------------------ .../java/com/youchain/utils/BatchUtils.java | 81 +++++++++++++++++++ .../main/resources/config/application-dev.yml | 2 +- .../resources/config/application-prod.yml | 2 +- 7 files changed, 112 insertions(+), 114 deletions(-) create mode 100644 application.pid delete mode 100644 youchain-system/src/main/java/com/youchain/utils/BatchCreateOrUpdate.java create mode 100644 youchain-system/src/main/java/com/youchain/utils/BatchUtils.java diff --git a/application.pid b/application.pid new file mode 100644 index 0000000..982cceb --- /dev/null +++ b/application.pid @@ -0,0 +1 @@ +27324 \ No newline at end of file diff --git a/youchain-system/src/main/java/com/youchain/basicdata/rest/ItemController.java b/youchain-system/src/main/java/com/youchain/basicdata/rest/ItemController.java index 79c5138..fdce53a 100644 --- a/youchain-system/src/main/java/com/youchain/basicdata/rest/ItemController.java +++ b/youchain-system/src/main/java/com/youchain/basicdata/rest/ItemController.java @@ -1,21 +1,20 @@ /* -* Copyright 2019-2020 Zheng Jie -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.youchain.basicdata.rest; -import com.youchain.annotation.AnonymousAccess; import com.youchain.annotation.Log; import com.youchain.basicdata.domain.Item; import com.youchain.basicdata.service.ItemService; @@ -35,14 +34,13 @@ import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import javax.servlet.http.HttpServletResponse; -import javax.transaction.Transactional; /** -* @website https://eladmin.vip -* @author houjianlan -* @date 2023-08-16 -**/ + * @author houjianlan + * @website https://eladmin.vip + * @date 2023-08-16 + **/ @RestController @RequiredArgsConstructor @Api(tags = "item管理") @@ -62,13 +60,13 @@ public class ItemController { @GetMapping @ApiOperation("查询item") - public ResponseEntity queryItem(ItemQueryCriteria criteria, Pageable pageable){ - return new ResponseEntity<>(itemService.queryAll(criteria,pageable), HttpStatus.OK); + public ResponseEntity queryItem(ItemQueryCriteria criteria, Pageable pageable) { + return new ResponseEntity<>(itemService.queryAll(criteria, pageable), HttpStatus.OK); } @GetMapping(value = "/queryItemAll") @ApiOperation("查询item") - public ResponseEntity queryItemAll(ItemQueryCriteria criteria){ + public ResponseEntity queryItemAll(ItemQueryCriteria criteria) { return new ResponseEntity<>(itemService.queryAll(criteria), HttpStatus.OK); } @@ -76,9 +74,9 @@ public class ItemController { @Log("新增item") @ApiOperation("新增item") @PreAuthorize("@el.check('item:add')") - public ResponseEntity createItem(@Validated @RequestBody Item resources){ + public ResponseEntity createItem(@Validated @RequestBody Item resources) { resources.setDept(UserUtils.getDept()); - return new ResponseEntity<>(itemService.create(resources),HttpStatus.CREATED); + return new ResponseEntity<>(itemService.create(resources), HttpStatus.CREATED); } @Log("导入物料") @@ -88,7 +86,7 @@ public class ItemController { try { String result = itemService.importItem(multipartFile); return new ResponseEntity<>(ApiResult.success(result, null), HttpStatus.OK); - }catch (Exception e){ + } catch (Exception e) { return new ResponseEntity<>(ApiResult.fail(e.getMessage()), HttpStatus.BAD_REQUEST); } } @@ -97,7 +95,7 @@ public class ItemController { @Log("修改item") @ApiOperation("修改item") @PreAuthorize("@el.check('item:edit')") - public ResponseEntity updateItem(@Validated @RequestBody Item resources){ + public ResponseEntity updateItem(@Validated @RequestBody Item resources) { itemService.update(resources); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } diff --git a/youchain-system/src/main/java/com/youchain/basicdata/service/impl/ItemServiceImpl.java b/youchain-system/src/main/java/com/youchain/basicdata/service/impl/ItemServiceImpl.java index 7510385..92f8f83 100644 --- a/youchain-system/src/main/java/com/youchain/basicdata/service/impl/ItemServiceImpl.java +++ b/youchain-system/src/main/java/com/youchain/basicdata/service/impl/ItemServiceImpl.java @@ -36,7 +36,6 @@ import org.springframework.web.multipart.MultipartFile; import java.util.*; import java.io.IOException; -import javax.persistence.EntityManager; import javax.servlet.http.HttpServletResponse; import java.util.stream.Collectors; @@ -166,7 +165,6 @@ public class ItemServiceImpl implements ItemService { throw new BadRequestException("数据为空"); } - //获取文件中所有物料编码 List codes = dataList.stream() .map(ItemImportReq::getCode) @@ -182,11 +180,10 @@ public class ItemServiceImpl implements ItemService { //修改物料集合 List itemsToUpdate = new ArrayList<>(); - for (ItemImportReq itemImportReq : dataList) { String code = itemImportReq.getCode(); String specs = itemImportReq.getSpecs(); - //判断是否已存在容器 + //判断是否已存在物料 if (existingItemMap.containsKey(code)) { //修改物料 Item item = existingItemMap.get(code); @@ -199,12 +196,12 @@ public class ItemServiceImpl implements ItemService { } } - //批量新增容器 + //批量新增物料 if (!itemsToCreate.isEmpty()) { itemRepository.saveAll(itemsToCreate); } - //批量更新容器 + //批量更新物料 if (!itemsToUpdate.isEmpty()) { itemRepository.saveAll(itemsToUpdate); } @@ -228,5 +225,4 @@ public class ItemServiceImpl implements ItemService { return item; } - } diff --git a/youchain-system/src/main/java/com/youchain/utils/BatchCreateOrUpdate.java b/youchain-system/src/main/java/com/youchain/utils/BatchCreateOrUpdate.java deleted file mode 100644 index 31ea344..0000000 --- a/youchain-system/src/main/java/com/youchain/utils/BatchCreateOrUpdate.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.youchain.utils; - -import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import java.util.List; - -@Component -public class BatchCreateOrUpdate { - - @PersistenceContext - private EntityManager entityManager; - - /** - * 批量插入实体。 - * - * @param entities 要插入的实体列表 - * @param 实体类型 - */ - @Transactional - public void batchCreate(List entities) { - int batchSize = 100; - if (entities == null || entities.isEmpty()) { - return; - } - - int i = 0; - for (T entity : entities) { - entityManager.persist(entity); - i++; - if (i % batchSize == 0) { - // 执行flush将实体状态同步到数据库,并清理持久化上下文 - entityManager.flush(); - entityManager.clear(); - } - } - - // 确保最后未达到batchSize的数据也能被flush到数据库 - if (i % batchSize != 0) { - entityManager.flush(); - entityManager.clear(); - } - } - - - /** - * 批量更新实体。 - * - * @param entities 要更新的实体列表 - * @param 实体类型 - */ - @Transactional - public void batchUpdate(List entities) { - int batchSize = 100; - if (entities == null || entities.isEmpty()) { - return; - } - - int i = 0; - for (T entity : entities) { - entityManager.merge(entity); - i++; - if (i % batchSize == 0) { - // 执行flush和clear以同步状态到数据库并清理上下文,减少内存使用 - entityManager.flush(); - entityManager.clear(); - } - } - - if (i % batchSize != 0) { - entityManager.flush(); - entityManager.clear(); - } - } - -} diff --git a/youchain-system/src/main/java/com/youchain/utils/BatchUtils.java b/youchain-system/src/main/java/com/youchain/utils/BatchUtils.java new file mode 100644 index 0000000..0713b56 --- /dev/null +++ b/youchain-system/src/main/java/com/youchain/utils/BatchUtils.java @@ -0,0 +1,81 @@ +package com.youchain.utils; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityManager; +import java.util.List; + +@Component +public class BatchUtils { + + @Autowired + private EntityManager entityManager; + + /** + * 批量插入实体。 + * + * @param entities 要插入的实体列表 + * @param 实体类型 + */ + @Transactional + public void batchInsert(List entities, int batchSize) { + if (entities.isEmpty()) { + return; + } + + for (int i = 0; i < entities.size(); i++) { + entityManager.persist(entities.get(i)); + + if ((i + 1) % batchSize == 0) { + entityManager.flush(); + entityManager.clear(); + } + } + + // 处理剩余记录 + entityManager.flush(); + entityManager.clear(); + } + + // 重载方法,使用默认批量大小 + @Transactional + public void batchInsert(List entities) { + batchInsert(entities, 500); + } + + + /** + * 批量更新实体。 + * + * @param entities 要更新的实体列表 + * @param 实体类型 + */ + @Transactional + public void batchUpdate(List entities, int batchSize) { + if (entities.isEmpty()) { + return; + } + + for (int i = 0; i < entities.size(); i++) { + entityManager.merge(entities.get(i)); + + if ((i + 1) % batchSize == 0) { + entityManager.flush(); + entityManager.clear(); + } + } + + // 处理剩余记录 + entityManager.flush(); + entityManager.clear(); + } + + @Transactional + public void batchUpdate(List entities) { + batchUpdate(entities, 500); + } + + +} diff --git a/youchain-system/src/main/resources/config/application-dev.yml b/youchain-system/src/main/resources/config/application-dev.yml index 6ca0e74..c8cf86b 100644 --- a/youchain-system/src/main/resources/config/application-dev.yml +++ b/youchain-system/src/main/resources/config/application-dev.yml @@ -4,7 +4,7 @@ spring: druid: db-type: com.alibaba.druid.pool.DruidDataSource driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy - url: jdbc:log4jdbc:mysql://${DB_HOST:47.103.100.52}:${DB_PORT:53306}/${DB_NAME:nio_wms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false + url: jdbc:log4jdbc:mysql://${DB_HOST:47.103.100.52}:${DB_PORT:53306}/${DB_NAME:nio_wms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true username: ${DB_USER:root} password: ${DB_PWD:Youchain@56} # 初始连接数 diff --git a/youchain-system/src/main/resources/config/application-prod.yml b/youchain-system/src/main/resources/config/application-prod.yml index ab526d4..42e881c 100644 --- a/youchain-system/src/main/resources/config/application-prod.yml +++ b/youchain-system/src/main/resources/config/application-prod.yml @@ -4,7 +4,7 @@ spring: druid: db-type: com.alibaba.druid.pool.DruidDataSource driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy - url: jdbc:log4jdbc:mysql://${DB_HOST:10.172.64.114}:${DB_PORT:3306}/${DB_NAME:nio_wms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false + url: jdbc:log4jdbc:mysql://${DB_HOST:10.172.64.114}:${DB_PORT:3306}/${DB_NAME:nio_wms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true username: ${DB_USER:root} password: ${DB_PWD:Youchain@56} # 初始连接数