diff --git a/src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleService.java b/src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleService.java index 4e16360..3d990e0 100644 --- a/src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleService.java +++ b/src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleService.java @@ -9,6 +9,8 @@ import org.springframework.data.domain.Page; public interface SimpleService { Long save(ENTITY entity) throws Exception; + void save(Iterable entities) throws Exception; + Long count() throws Exception; List list() throws Exception; @@ -22,4 +24,6 @@ public interface SimpleService { ENTITY detailOrThrow(Long id) throws Exception; void remove(Long id) throws Exception; + + void remove(Iterable ids) throws Exception; } diff --git a/src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleServiceSupport.java b/src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleServiceSupport.java index e90329d..ff7a53e 100644 --- a/src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleServiceSupport.java +++ b/src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleServiceSupport.java @@ -125,6 +125,21 @@ public abstract class SimpleServiceSupport implemen return entity.getId(); } + /** + * 批量保存实体对象集合 + *

+ * 使用saveOrUpdateAllByNotNullProperties方法,只更新非空字段。 + * 该方法具有事务性,遇到任何异常都会回滚。 + *

+ * + * @param entities 需要保存的实体对象集合 + */ + @Transactional(rollbackOn = Throwable.class) + @Override + public void save(Iterable entities) { + repository.saveOrUpdateAllByNotNullProperties(entities); + } + /** * 统计符合条件的实体数量 *

@@ -587,6 +602,24 @@ public abstract class SimpleServiceSupport implemen } } + /** + * 根据ID集合批量删除实体 + *

+ * 使用deleteAllById方法根据ID集合批量删除实体。 + * 该方法具有事务性,遇到任何异常都会回滚。 + * 如果ID集合为空则不执行任何操作。 + *

+ * + * @param ids 实体ID集合 + */ + @Transactional(rollbackOn = Throwable.class) + @Override + public void remove(Iterable ids) { + if (ObjectHelper.isNotEmpty(ids)) { + repository.deleteAllById(ids); + } + } + /** * ID未找到异常 *