1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
01ccadd856 feat: 增加方便的批量方法 2025-08-30 17:16:53 +08:00
78af47b715 fix: 修复排序参数为空导致空匹配 2025-08-30 17:16:07 +08:00
2 changed files with 47 additions and 2 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>
@@ -363,10 +378,18 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
});
}
if (ObjectHelper.isNotEmpty(queryable.getInside())) {
queryable.getInside().forEach((column, value) -> predicates.add(builder.in(column(root, column)).value(value)));
queryable.getInside()
.entrySet()
.stream()
.filter(entry -> ObjectHelper.isNotEmpty(entry.getValue()))
.forEach(entry -> predicates.add(builder.in(column(root, entry.getKey())).value(entry.getValue())));
}
if (ObjectHelper.isNotEmpty(queryable.getNotInside())) {
queryable.getNotInside().forEach((column, value) -> predicates.add(builder.in(column(root, column)).value(value).not()));
queryable.getNotInside()
.entrySet()
.stream()
.filter(entry -> ObjectHelper.isNotEmpty(entry.getValue()))
.forEach(entry -> predicates.add(builder.in(column(root, entry.getKey())).value(entry.getValue()).not()));
}
if (ObjectHelper.isNotEmpty(queryable.getBetween())) {
queryable.getBetween().forEach((column, value) -> {
@@ -579,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>