From 37183ce26293c2fa713e12a2a7580c977bd85552 Mon Sep 17 00:00:00 2001 From: dataprince Date: Sat, 30 Sep 2023 11:00:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84IBaseService=EF=BC=8C?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=89=B9=E9=87=8F=E6=9B=B4=E6=96=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/orm/core/service/IBaseService.java | 34 +++++++++++++++++-- .../core/service/impl/BaseServiceImpl.java | 2 +- .../orm/listener/EntityInsertListener.java | 2 +- .../orm/listener/EntityUpdateListener.java | 2 +- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/core/service/IBaseService.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/core/service/IBaseService.java index aa962ab..0d012e8 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/core/service/IBaseService.java +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/core/service/IBaseService.java @@ -1,12 +1,42 @@ package com.ruoyi.common.orm.core.service; +import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.row.Db; import com.mybatisflex.core.service.IService; -import com.ruoyi.common.orm.core.domain.BaseEntity; +import com.mybatisflex.core.util.ClassUtil; +import com.mybatisflex.core.util.SqlUtil; + +import java.util.Collection; /** * 自定义的服务基类接口 * * @author dataprince数据小王子 */ -public interface IBaseService extends IService { +public interface IBaseService extends IService { + /** + *

带主键批量保存实体类对象数据,适用于中间表有联合主键场合,例如sys_role_menu。 + * + * @param entities 实体类对象 + * @return {@code true} 保存成功,{@code false} 保存失败。 + * @apiNote 默认调用的是 {@link BaseMapper#insertSelectiveWithPk(Object)} 方法,忽略实体类 + * {@code null} 属性的数据,使数据库配置的默认值生效。 + */ + default boolean saveBatchWithPk(Collection entities) { + return saveBatchWithPk(entities, DEFAULT_BATCH_SIZE); + } + + /** + *

带主键批量保存实体类对象数据,适用于中间表有联合主键场合,例如sys_role_menu。 + * + * @param entities 实体类对象 + * @param batchSize 每次保存切分的数量 + * @return {@code true} 保存成功,{@code false} 保存失败。 + * @apiNote 默认调用的是 {@link BaseMapper#insertSelectiveWithPk(Object)} 方法,忽略实体类 + * {@code null} 属性的数据,使数据库配置的默认值生效。 + */ + default boolean saveBatchWithPk(Collection entities, int batchSize) { + Class> usefulClass = (Class>) ClassUtil.getUsefulClass(getMapper().getClass()); + return SqlUtil.toBool(Db.executeBatch(entities, batchSize, usefulClass, BaseMapper::insertSelectiveWithPk)); + } } diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/core/service/impl/BaseServiceImpl.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/core/service/impl/BaseServiceImpl.java index c24b2c1..89013fe 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/core/service/impl/BaseServiceImpl.java +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/core/service/impl/BaseServiceImpl.java @@ -15,7 +15,7 @@ import com.ruoyi.common.orm.core.service.IBaseService; * * @author dataprince数据小王子 */ -public class BaseServiceImpl, T extends BaseEntity> extends ServiceImpl implements IBaseService { +public class BaseServiceImpl, T> extends ServiceImpl implements IBaseService { /** * 构造基本查询条件 diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java index 9ab2263..16cd82b 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityInsertListener.java @@ -19,7 +19,7 @@ public class EntityInsertListener implements InsertListener { @Override public void onInsert(Object entity) { try { - if (ObjectUtil.isNotNull(entity)) { + if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) { BaseEntity baseEntity = (BaseEntity) entity; Long loginUserId = LoginHelper.getUserId(); diff --git a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java index 4626ef7..3b42383 100644 --- a/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java +++ b/ruoyi-common/ruoyi-common-orm/src/main/java/com/ruoyi/common/orm/listener/EntityUpdateListener.java @@ -18,7 +18,7 @@ public class EntityUpdateListener implements UpdateListener { @Override public void onUpdate(Object entity) { try { - if (ObjectUtil.isNotNull(entity)) { + if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) { BaseEntity baseEntity = (BaseEntity) entity; baseEntity.setUpdateBy(LoginHelper.getUserId()); baseEntity.setUpdateTime(new Date());