mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-11 06:57:14 +08:00
refactor(data/mybatis-plus): 重构 IService 及 BaseService 体系结构
This commit is contained in:
@@ -14,9 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package top.charles7c.continew.starter.data.core.service;
|
package top.charles7c.continew.starter.data.mybatis.plus.service;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用业务接口
|
* 通用业务接口
|
||||||
@@ -25,13 +23,5 @@ import java.io.Serializable;
|
|||||||
* @author Charles7c
|
* @author Charles7c
|
||||||
* @since 1.2.0
|
* @since 1.2.0
|
||||||
*/
|
*/
|
||||||
public interface IService<T> {
|
public interface IService<T> extends com.baomidou.mybatisplus.extension.service.IService<T> {
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据 ID 查询
|
|
||||||
*
|
|
||||||
* @param id ID
|
|
||||||
* @return 实体信息
|
|
||||||
*/
|
|
||||||
T getById(Serializable id);
|
|
||||||
}
|
}
|
@@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.data.mybatis.plus.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ClassUtil;
|
||||||
|
import top.charles7c.continew.starter.core.util.ReflectUtils;
|
||||||
|
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
|
||||||
|
import top.charles7c.continew.starter.data.mybatis.plus.base.BaseMapper;
|
||||||
|
import top.charles7c.continew.starter.data.mybatis.plus.service.IService;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用业务实现类
|
||||||
|
*
|
||||||
|
* @param <M> Mapper 接口
|
||||||
|
* @param <T> 实体类型
|
||||||
|
* @author Charles7c
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
public class ServiceImpl<M extends BaseMapper<T>, T> extends com.baomidou.mybatisplus.extension.service.impl.ServiceImpl<M, T> implements IService<T> {
|
||||||
|
|
||||||
|
protected final List<Field> entityFields = ReflectUtils.getNonStaticFields(this.entityClass);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getById(Serializable id) {
|
||||||
|
return this.getById(id, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 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;
|
||||||
|
}
|
||||||
|
}
|
@@ -65,15 +65,6 @@ public interface BaseService<L, D, Q, C extends BaseReq> {
|
|||||||
*/
|
*/
|
||||||
List<L> list(Q query, SortQuery sortQuery);
|
List<L> list(Q query, SortQuery sortQuery);
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询列表
|
|
||||||
*
|
|
||||||
* @return 列表信息
|
|
||||||
*/
|
|
||||||
default List<L> list() {
|
|
||||||
return list(null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查看详情
|
* 查看详情
|
||||||
*
|
*
|
||||||
|
@@ -23,27 +23,24 @@ import cn.hutool.core.collection.CollUtil;
|
|||||||
import cn.hutool.core.lang.Opt;
|
import cn.hutool.core.lang.Opt;
|
||||||
import cn.hutool.core.lang.tree.Tree;
|
import cn.hutool.core.lang.tree.Tree;
|
||||||
import cn.hutool.core.lang.tree.TreeNodeConfig;
|
import cn.hutool.core.lang.tree.TreeNodeConfig;
|
||||||
import cn.hutool.core.util.ClassUtil;
|
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
import cn.hutool.core.text.CharSequenceUtil;
|
import cn.hutool.core.text.CharSequenceUtil;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import top.charles7c.continew.starter.core.constant.StringConstants;
|
import top.charles7c.continew.starter.core.constant.StringConstants;
|
||||||
import top.charles7c.continew.starter.core.util.ClassUtils;
|
import top.charles7c.continew.starter.core.util.ClassUtils;
|
||||||
import top.charles7c.continew.starter.core.util.ReflectUtils;
|
import top.charles7c.continew.starter.core.util.ReflectUtils;
|
||||||
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
|
|
||||||
import top.charles7c.continew.starter.core.util.validate.ValidationUtils;
|
import top.charles7c.continew.starter.core.util.validate.ValidationUtils;
|
||||||
import top.charles7c.continew.starter.data.mybatis.plus.base.BaseMapper;
|
import top.charles7c.continew.starter.data.mybatis.plus.base.BaseMapper;
|
||||||
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryWrapperHelper;
|
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryWrapperHelper;
|
||||||
|
import top.charles7c.continew.starter.data.mybatis.plus.service.impl.ServiceImpl;
|
||||||
import top.charles7c.continew.starter.extension.crud.annotation.TreeField;
|
import top.charles7c.continew.starter.extension.crud.annotation.TreeField;
|
||||||
import top.charles7c.continew.starter.extension.crud.model.entity.BaseDO;
|
import top.charles7c.continew.starter.extension.crud.model.entity.BaseDO;
|
||||||
import top.charles7c.continew.starter.extension.crud.model.req.BaseReq;
|
import top.charles7c.continew.starter.extension.crud.model.req.BaseReq;
|
||||||
import top.charles7c.continew.starter.data.core.service.IService;
|
|
||||||
import top.charles7c.continew.starter.extension.crud.model.query.PageQuery;
|
import top.charles7c.continew.starter.extension.crud.model.query.PageQuery;
|
||||||
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;
|
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;
|
||||||
import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
|
import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
|
||||||
@@ -51,7 +48,6 @@ import top.charles7c.continew.starter.extension.crud.service.BaseService;
|
|||||||
import top.charles7c.continew.starter.extension.crud.util.TreeUtils;
|
import top.charles7c.continew.starter.extension.crud.util.TreeUtils;
|
||||||
import top.charles7c.continew.starter.file.excel.util.ExcelUtils;
|
import top.charles7c.continew.starter.file.excel.util.ExcelUtils;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -69,17 +65,12 @@ import java.util.Optional;
|
|||||||
* @author Charles7c
|
* @author Charles7c
|
||||||
* @since 1.0.0
|
* @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>, IService<T> {
|
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO, L, D, Q, C extends BaseReq> extends ServiceImpl<M, T> implements BaseService<L, D, Q, C> {
|
||||||
|
|
||||||
@Autowired
|
private final Class<?>[] typeArgumentCache = ClassUtils.getTypeArguments(this.getClass());
|
||||||
protected M baseMapper;
|
|
||||||
|
|
||||||
private final Class<?>[] typeArguments = ClassUtils.getTypeArguments(this.getClass());
|
|
||||||
protected final Class<T> entityClass = this.currentEntityClass();
|
|
||||||
protected final Class<L> listClass = this.currentListClass();
|
protected final Class<L> listClass = this.currentListClass();
|
||||||
protected final Class<D> detailClass = this.currentDetailClass();
|
protected final Class<D> detailClass = this.currentDetailClass();
|
||||||
protected final Class<Q> queryClass = this.currentQueryClass();
|
protected final Class<Q> queryClass = this.currentQueryClass();
|
||||||
private final List<Field> entityFields = ReflectUtils.getNonStaticFields(this.entityClass);
|
|
||||||
private final List<Field> queryFields = ReflectUtils.getNonStaticFields(this.queryClass);
|
private final List<Field> queryFields = ReflectUtils.getNonStaticFields(this.queryClass);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -128,28 +119,9 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 查询列表
|
|
||||||
*
|
|
||||||
* @param query 查询条件
|
|
||||||
* @param sortQuery 排序查询条件
|
|
||||||
* @param targetClass 指定类型
|
|
||||||
* @return 列表信息
|
|
||||||
*/
|
|
||||||
protected <E> List<E> list(Q query, SortQuery sortQuery, Class<E> targetClass) {
|
|
||||||
QueryWrapper<T> queryWrapper = this.handleQueryWrapper(query);
|
|
||||||
// 设置排序
|
|
||||||
this.sort(queryWrapper, sortQuery);
|
|
||||||
List<T> entityList = baseMapper.selectList(queryWrapper);
|
|
||||||
if (entityClass == targetClass) {
|
|
||||||
return (List<E>)entityList;
|
|
||||||
}
|
|
||||||
return BeanUtil.copyToList(entityList, targetClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public D get(Long id) {
|
public D get(Long id) {
|
||||||
T entity = this.getById(id, false);
|
T entity = super.getById(id, false);
|
||||||
D detail = BeanUtil.toBean(entity, detailClass);
|
D detail = BeanUtil.toBean(entity, detailClass);
|
||||||
this.fill(detail);
|
this.fill(detail);
|
||||||
return detail;
|
return detail;
|
||||||
@@ -190,24 +162,23 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
|
|||||||
ExcelUtils.export(list, "导出数据", detailClass, response);
|
ExcelUtils.export(list, "导出数据", detailClass, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public T getById(Serializable id) {
|
|
||||||
return this.getById(id, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 ID 查询
|
* 查询列表
|
||||||
*
|
*
|
||||||
* @param id ID
|
* @param query 查询条件
|
||||||
* @param isCheckExists 是否检查存在
|
* @param sortQuery 排序查询条件
|
||||||
* @return 实体信息
|
* @param targetClass 指定类型
|
||||||
|
* @return 列表信息
|
||||||
*/
|
*/
|
||||||
protected T getById(Serializable id, boolean isCheckExists) {
|
protected <E> List<E> list(Q query, SortQuery sortQuery, Class<E> targetClass) {
|
||||||
T entity = baseMapper.selectById(id);
|
QueryWrapper<T> queryWrapper = this.handleQueryWrapper(query);
|
||||||
if (isCheckExists) {
|
// 设置排序
|
||||||
CheckUtils.throwIfNotExists(entity, ClassUtil.getClassName(entityClass, true), "ID", id);
|
this.sort(queryWrapper, sortQuery);
|
||||||
|
List<T> entityList = baseMapper.selectList(queryWrapper);
|
||||||
|
if (entityClass == targetClass) {
|
||||||
|
return (List<E>)entityList;
|
||||||
}
|
}
|
||||||
return entity;
|
return BeanUtil.copyToList(entityList, targetClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -318,22 +289,13 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
|
|||||||
/* 删除后置处理 */
|
/* 删除后置处理 */
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取当前实体类型
|
|
||||||
*
|
|
||||||
* @return 当前实体类型
|
|
||||||
*/
|
|
||||||
protected Class<T> currentEntityClass() {
|
|
||||||
return (Class<T>)this.typeArguments[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前列表信息类型
|
* 获取当前列表信息类型
|
||||||
*
|
*
|
||||||
* @return 当前列表信息类型
|
* @return 当前列表信息类型
|
||||||
*/
|
*/
|
||||||
protected Class<L> currentListClass() {
|
protected Class<L> currentListClass() {
|
||||||
return (Class<L>)this.typeArguments[2];
|
return (Class<L>)this.typeArgumentCache[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -342,7 +304,7 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
|
|||||||
* @return 当前详情信息类型
|
* @return 当前详情信息类型
|
||||||
*/
|
*/
|
||||||
protected Class<D> currentDetailClass() {
|
protected Class<D> currentDetailClass() {
|
||||||
return (Class<D>)this.typeArguments[3];
|
return (Class<D>)this.typeArgumentCache[3];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -351,6 +313,6 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO,
|
|||||||
* @return 当前查询条件类型
|
* @return 当前查询条件类型
|
||||||
*/
|
*/
|
||||||
protected Class<Q> currentQueryClass() {
|
protected Class<Q> currentQueryClass() {
|
||||||
return (Class<Q>)this.typeArguments[4];
|
return (Class<Q>)this.typeArgumentCache[4];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user