chore(extension/crud): 新增 IService 通用业务接口

This commit is contained in:
2024-01-16 22:40:43 +08:00
parent 62ff9b2d1b
commit 926c92cc32
4 changed files with 74 additions and 26 deletions

View File

@@ -41,10 +41,10 @@ import java.util.List;
* 控制器基类
*
* @param <S> 业务接口
* @param <L> 列表信息
* @param <D> 详情信息
* @param <L> 列表类型
* @param <D> 详情类型
* @param <Q> 查询条件
* @param <C> 创建或修改信息
* @param <C> 创建或修改类型
* @author Charles7c
* @since 1.0.0
*/

View File

@@ -27,10 +27,10 @@ import java.util.List;
/**
* 业务接口基类
*
* @param <L> 列表信息
* @param <D> 详情信息
* @param <L> 列表类型
* @param <D> 详情类型
* @param <Q> 查询条件
* @param <C> 创建或修改信息
* @param <C> 创建或修改类型
* @author Charles7c
* @since 1.0.0
*/

View File

@@ -20,7 +20,6 @@ import cn.crane4j.core.support.OperateTemplate;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
@@ -46,6 +45,7 @@ import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
import top.charles7c.continew.starter.extension.crud.util.TreeUtils;
import top.charles7c.continew.starter.file.excel.util.ExcelUtils;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
@@ -54,15 +54,15 @@ import java.util.List;
* 业务实现基类
*
* @param <M> Mapper 接口
* @param <T> 实体类
* @param <L> 列表信息
* @param <D> 详情信息
* @param <T> 实体类
* @param <L> 列表类型
* @param <D> 详情类型
* @param <Q> 查询条件
* @param <C> 创建或修改信息
* @param <C> 创建或修改类型
* @author Charles7c
* @since 1.0.0
*/
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO, L, D, Q, C extends BaseReq> implements BaseService<L, D, Q, C> {
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO, L, D, Q, C extends BaseReq> implements BaseService<L, D, Q, C>, IService<T> {
@Autowired
protected M baseMapper;
@@ -139,8 +139,8 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
@Override
public D get(Long id) {
T entity = this.getById(id);
D detail = BeanUtil.copyProperties(entity, detailClass);
T entity = this.getById(id, false);
D detail = BeanUtil.toBean(entity, detailClass);
this.fill(detail);
return detail;
}
@@ -180,28 +180,24 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
ExcelUtils.export(list, "导出数据", detailClass, response);
}
@Override
public T getById(Serializable id) {
return this.getById(id, true);
}
/**
* 填充数据
*
* @param obj 待填充信息
*/
protected void fill(Object obj) {
if (null == obj) {
return;
}
OperateTemplate operateTemplate = SpringUtil.getBean(OperateTemplate.class);
operateTemplate.execute(obj);
}
/**
* 根据 ID 查询
*
* @param id ID
* @return 实体信息
*/
protected T getById(Object id) {
T entity = baseMapper.selectById(Convert.toStr(id));
CheckUtils.throwIfNotExists(entity, ClassUtil.getClassName(entityClass, true), "ID", id);
return entity;
}
/**
* 设置排序
*
@@ -217,6 +213,21 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
}
}
/**
* 根据 ID 查询
*
* @param id ID
* @param isCheckExists 是否检查存在
* @return 实体信息
*/
protected T getById(Serializable id, boolean isCheckExists) {
T entity = baseMapper.selectById(id);
if (isCheckExists) {
CheckUtils.throwIfNotExists(entity, ClassUtil.getClassName(entityClass, true), "ID", id);
}
return entity;
}
/**
* 新增前置处理
*

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.base;
import java.io.Serializable;
/**
* 通用业务接口
*
* @param <T> 实体类型
* @author Charles7c
* @since 1.2.0
*/
public interface IService<T> {
/**
* 根据 ID 查询
*
* @param id ID
* @return 实体信息
*/
T getById(Serializable id);
}