feat: 增加方便的批量方法
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user