重构IBaseService,加入批量更新功能

This commit is contained in:
dataprince 2023-09-30 11:00:17 +08:00
parent 891ef9bb09
commit 37183ce262
4 changed files with 35 additions and 5 deletions

View File

@ -1,12 +1,42 @@
package com.ruoyi.common.orm.core.service; 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.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数据小王子 * @author dataprince数据小王子
*/ */
public interface IBaseService<T extends BaseEntity> extends IService<T> { public interface IBaseService<T> extends IService<T> {
/**
* <p>带主键批量保存实体类对象数据适用于中间表有联合主键场合例如sys_role_menu
*
* @param entities 实体类对象
* @return {@code true} 保存成功{@code false} 保存失败
* @apiNote 默认调用的是 {@link BaseMapper#insertSelectiveWithPk(Object)} 方法忽略实体类
* {@code null} 属性的数据使数据库配置的默认值生效
*/
default boolean saveBatchWithPk(Collection<T> entities) {
return saveBatchWithPk(entities, DEFAULT_BATCH_SIZE);
}
/**
* <p>带主键批量保存实体类对象数据适用于中间表有联合主键场合例如sys_role_menu
*
* @param entities 实体类对象
* @param batchSize 每次保存切分的数量
* @return {@code true} 保存成功{@code false} 保存失败
* @apiNote 默认调用的是 {@link BaseMapper#insertSelectiveWithPk(Object)} 方法忽略实体类
* {@code null} 属性的数据使数据库配置的默认值生效
*/
default boolean saveBatchWithPk(Collection<T> entities, int batchSize) {
Class<BaseMapper<T>> usefulClass = (Class<BaseMapper<T>>) ClassUtil.getUsefulClass(getMapper().getClass());
return SqlUtil.toBool(Db.executeBatch(entities, batchSize, usefulClass, BaseMapper::insertSelectiveWithPk));
}
} }

View File

@ -15,7 +15,7 @@ import com.ruoyi.common.orm.core.service.IBaseService;
* *
* @author dataprince数据小王子 * @author dataprince数据小王子
*/ */
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M , T> implements IBaseService<T> { public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M , T> implements IBaseService<T> {
/** /**
* 构造基本查询条件 * 构造基本查询条件

View File

@ -19,7 +19,7 @@ public class EntityInsertListener implements InsertListener {
@Override @Override
public void onInsert(Object entity) { public void onInsert(Object entity) {
try { try {
if (ObjectUtil.isNotNull(entity)) { if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) {
BaseEntity baseEntity = (BaseEntity) entity; BaseEntity baseEntity = (BaseEntity) entity;
Long loginUserId = LoginHelper.getUserId(); Long loginUserId = LoginHelper.getUserId();

View File

@ -18,7 +18,7 @@ public class EntityUpdateListener implements UpdateListener {
@Override @Override
public void onUpdate(Object entity) { public void onUpdate(Object entity) {
try { try {
if (ObjectUtil.isNotNull(entity)) { if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) {
BaseEntity baseEntity = (BaseEntity) entity; BaseEntity baseEntity = (BaseEntity) entity;
baseEntity.setUpdateBy(LoginHelper.getUserId()); baseEntity.setUpdateBy(LoginHelper.getUserId());
baseEntity.setUpdateTime(new Date()); baseEntity.setUpdateTime(new Date());