1
0

feat: 增加方便的批量方法

This commit is contained in:
2025-08-30 17:16:53 +08:00
parent 78af47b715
commit 01ccadd856
2 changed files with 37 additions and 0 deletions

View File

@@ -9,6 +9,8 @@ import org.springframework.data.domain.Page;
public interface SimpleService<ENTITY extends SimpleEntity> {
Long save(ENTITY entity) throws Exception;
void save(Iterable<ENTITY> entities) throws Exception;
Long count() throws Exception;
List<ENTITY> list() throws Exception;
@@ -22,4 +24,6 @@ public interface SimpleService<ENTITY extends SimpleEntity> {
ENTITY detailOrThrow(Long id) throws Exception;
void remove(Long id) throws Exception;
void remove(Iterable<Long> ids) throws Exception;
}

View File

@@ -125,6 +125,21 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
return entity.getId();
}
/**
* 批量保存实体对象集合
* <p>
* 使用saveOrUpdateAllByNotNullProperties方法只更新非空字段。
* 该方法具有事务性,遇到任何异常都会回滚。
* </p>
*
* @param entities 需要保存的实体对象集合
*/
@Transactional(rollbackOn = Throwable.class)
@Override
public void save(Iterable<ENTITY> entities) {
repository.saveOrUpdateAllByNotNullProperties(entities);
}
/**
* 统计符合条件的实体数量
* <p>
@@ -587,6 +602,24 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
}
}
/**
* 根据ID集合批量删除实体
* <p>
* 使用deleteAllById方法根据ID集合批量删除实体。
* 该方法具有事务性,遇到任何异常都会回滚。
* 如果ID集合为空则不执行任何操作。
* </p>
*
* @param ids 实体ID集合
*/
@Transactional(rollbackOn = Throwable.class)
@Override
public void remove(Iterable<Long> ids) {
if (ObjectHelper.isNotEmpty(ids)) {
repository.deleteAllById(ids);
}
}
/**
* ID未找到异常
* <p>