mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
feat(extension/crud): 新增 Api.BATCH_DELETE 批量删除枚举,拆分单个删除和批量删除接口
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.continew.starter.data.mp.mapper;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
|
||||
import com.baomidou.mybatisplus.extension.toolkit.Db;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Mapper 基类
|
||||
*
|
||||
* @param <T> 实体类
|
||||
* @author Charles7c
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface BaseMapper<T> extends com.baomidou.mybatisplus.core.mapper.BaseMapper<T> {
|
||||
|
||||
/**
|
||||
* 批量插入记录
|
||||
*
|
||||
* @param entityList 实体列表
|
||||
* @return 是否成功
|
||||
*/
|
||||
default boolean insertBatch(Collection<T> entityList) {
|
||||
return Db.saveBatch(entityList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新记录
|
||||
*
|
||||
* @param entityList 实体列表
|
||||
* @return 是否成功
|
||||
*/
|
||||
default boolean updateBatchById(Collection<T> entityList) {
|
||||
return Db.updateBatchById(entityList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式查询
|
||||
*
|
||||
* @return QueryWrapper 的包装类
|
||||
*/
|
||||
default QueryChainWrapper<T> query() {
|
||||
return ChainWrappers.queryChain(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式查询(lambda 式)
|
||||
*
|
||||
* @return LambdaQueryWrapper 的包装类
|
||||
*/
|
||||
default LambdaQueryChainWrapper<T> lambdaQuery() {
|
||||
return ChainWrappers.lambdaQueryChain(this, this.currentEntityClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式查询(lambda 式)
|
||||
*
|
||||
* @param entity 实体对象
|
||||
* @return LambdaQueryWrapper 的包装类
|
||||
*/
|
||||
default LambdaQueryChainWrapper<T> lambdaQuery(T entity) {
|
||||
return ChainWrappers.lambdaQueryChain(this, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式更改
|
||||
*
|
||||
* @return UpdateWrapper 的包装类
|
||||
*/
|
||||
default UpdateChainWrapper<T> update() {
|
||||
return ChainWrappers.updateChain(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 链式更改(lambda 式)
|
||||
*
|
||||
* @return LambdaUpdateWrapper 的包装类
|
||||
*/
|
||||
default LambdaUpdateChainWrapper<T> lambdaUpdate() {
|
||||
return ChainWrappers.lambdaUpdateChain(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类 Class 对象
|
||||
*
|
||||
* @return 实体类 Class 对象
|
||||
*/
|
||||
default Class<T> currentEntityClass() {
|
||||
return (Class<T>)ClassUtil.getTypeArgument(this.getClass(), 0);
|
||||
}
|
||||
}
|
@@ -25,6 +25,7 @@ import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
import top.continew.starter.core.util.ExceptionUtils;
|
||||
import top.continew.starter.extension.crud.annotation.CrudApi;
|
||||
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
|
||||
import top.continew.starter.extension.crud.enums.Api;
|
||||
|
||||
@@ -52,8 +53,9 @@ public class CrudRequestMappingHandlerMapping extends RequestMappingHandlerMappi
|
||||
CrudRequestMapping crudRequestMapping = handlerType.getDeclaredAnnotation(CrudRequestMapping.class);
|
||||
// 过滤 API,如果非本类中定义,且 API 列表中不包含,则忽略
|
||||
Api[] apiArr = crudRequestMapping.api();
|
||||
Api api = ExceptionUtils.exToNull(() -> Api.valueOf(method.getName().toUpperCase()));
|
||||
if (method.getDeclaringClass() != handlerType && !ArrayUtil.contains(apiArr, api)) {
|
||||
CrudApi crudApi = AnnotatedElementUtils.findMergedAnnotation(method, CrudApi.class);
|
||||
if (method.getDeclaringClass() != handlerType && !ArrayUtil.contains(apiArr, ExceptionUtils
|
||||
.exToNull(crudApi::value))) {
|
||||
return null;
|
||||
}
|
||||
// 拼接路径(合并了 @RequestMapping 的部分能力)
|
||||
|
@@ -146,13 +146,27 @@ public abstract class AbstractCrudController<S extends CrudService<L, D, Q, C>,
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param req 删除请求参数
|
||||
* @param id ID
|
||||
*/
|
||||
@CrudApi(Api.DELETE)
|
||||
@Operation(summary = "删除数据", description = "删除数据")
|
||||
@Parameter(name = "id", description = "ID", example = "1", in = ParameterIn.PATH)
|
||||
@ResponseBody
|
||||
@DeleteMapping("/{id}")
|
||||
public void delete(@PathVariable("id") Long id) {
|
||||
baseService.delete(List.of(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除
|
||||
*
|
||||
* @param req 删除请求参数
|
||||
*/
|
||||
@CrudApi(Api.BATCH_DELETE)
|
||||
@Operation(summary = "批量删除数据", description = "批量删除数据")
|
||||
@ResponseBody
|
||||
@DeleteMapping
|
||||
public void delete(@Validated @RequestBody IdsReq req) {
|
||||
public void batchDelete(@Validated @RequestBody IdsReq req) {
|
||||
baseService.delete(req.getIds());
|
||||
}
|
||||
|
||||
|
@@ -63,4 +63,9 @@ public enum Api {
|
||||
* 导出
|
||||
*/
|
||||
EXPORT,
|
||||
|
||||
/**
|
||||
* 批量删除
|
||||
*/
|
||||
BATCH_DELETE,
|
||||
}
|
@@ -29,7 +29,6 @@ import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
@@ -40,6 +39,7 @@ import top.continew.starter.core.util.ClassUtils;
|
||||
import top.continew.starter.core.util.ReflectUtils;
|
||||
import top.continew.starter.core.validation.CheckUtils;
|
||||
import top.continew.starter.core.validation.ValidationUtils;
|
||||
import top.continew.starter.data.mp.mapper.BaseMapper;
|
||||
import top.continew.starter.data.mp.service.impl.ServiceImpl;
|
||||
import top.continew.starter.data.mp.util.QueryWrapperHelper;
|
||||
import top.continew.starter.extension.crud.annotation.DictModel;
|
||||
|
Reference in New Issue
Block a user