no message

main
HUOJIN\92525 2025-09-07 20:52:40 +08:00
parent 58af5b1482
commit 62efd8ff9a
7 changed files with 112 additions and 114 deletions

1
application.pid 100644
View File

@ -0,0 +1 @@
27324

View File

@ -15,7 +15,6 @@
*/
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,12 +34,11 @@ 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
* @website https://eladmin.vip
* @date 2023-08-16
**/
@RestController

View File

@ -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<String> codes = dataList.stream()
.map(ItemImportReq::getCode)
@ -182,11 +180,10 @@ public class ItemServiceImpl implements ItemService {
//修改物料集合
List<Item> 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;
}
}

View File

@ -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 <T>
*/
@Transactional
public <T> void batchCreate(List<T> 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 <T>
*/
@Transactional
public <T> void batchUpdate(List<T> 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();
}
}
}

View File

@ -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 <T>
*/
@Transactional
public <T> void batchInsert(List<T> 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 <T> void batchInsert(List<T> entities) {
batchInsert(entities, 500);
}
/**
*
*
* @param entities
* @param <T>
*/
@Transactional
public <T> void batchUpdate(List<T> 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 <T> void batchUpdate(List<T> entities) {
batchUpdate(entities, 500);
}
}

View File

@ -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}
# 初始连接数

View File

@ -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}
# 初始连接数