mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-10 08:57:14 +08:00
优化:优化后端 CRUD 公共组件(移除 BaseService 中无用的默认实现,抽取 BaseRequest 基类来方便使用分组校验),并同步调整部门管理 API
This commit is contained in:
@@ -37,7 +37,7 @@ public @interface CrudRequestMapping {
|
||||
/**
|
||||
* API 列表
|
||||
*/
|
||||
Api[] api() default Api.ALL;
|
||||
Api[] api() default {Api.PAGE, Api.DETAIL, Api.CREATE, Api.UPDATE, Api.DELETE};
|
||||
|
||||
/**
|
||||
* API 枚举
|
||||
|
@@ -44,14 +44,12 @@ import top.charles7c.cnadmin.common.model.vo.R;
|
||||
* @param <Q>
|
||||
* 查询条件
|
||||
* @param <C>
|
||||
* 创建信息
|
||||
* @param <U>
|
||||
* 修改信息
|
||||
* 创建或修改信息
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 10:45
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D, Q, C, U> {
|
||||
public abstract class BaseController<S extends BaseService<V, D, Q, C>, V, D, Q, C extends BaseRequest> {
|
||||
|
||||
@Autowired
|
||||
protected S baseService;
|
||||
@@ -63,9 +61,10 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D,
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 分页列表信息
|
||||
* @return 分页信息
|
||||
*/
|
||||
@Operation(summary = "分页查询列表")
|
||||
@ResponseBody
|
||||
@GetMapping
|
||||
protected R<PageDataVO<V>> page(@Validated Q query, @Validated PageQuery pageQuery) {
|
||||
PageDataVO<V> pageDataVO = baseService.page(query, pageQuery);
|
||||
@@ -80,6 +79,7 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D,
|
||||
* @return 列表信息
|
||||
*/
|
||||
@Operation(summary = "查询列表")
|
||||
@ResponseBody
|
||||
@GetMapping("/all")
|
||||
protected R<List<V>> list(@Validated Q query) {
|
||||
List<V> list = baseService.list(query);
|
||||
@@ -95,6 +95,7 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D,
|
||||
*/
|
||||
@Operation(summary = "查看详情")
|
||||
@Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
|
||||
@ResponseBody
|
||||
@GetMapping("/{id}")
|
||||
protected R<D> detail(@PathVariable Long id) {
|
||||
D detail = baseService.detail(id);
|
||||
@@ -109,8 +110,9 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D,
|
||||
* @return 自增 ID
|
||||
*/
|
||||
@Operation(summary = "新增")
|
||||
@ResponseBody
|
||||
@PostMapping
|
||||
protected R<Long> create(@Validated @RequestBody C request) {
|
||||
protected R<Long> create(@Validated(BaseRequest.Create.class) @RequestBody C request) {
|
||||
Long id = baseService.create(request);
|
||||
return R.ok("新增成功", id);
|
||||
}
|
||||
@@ -118,17 +120,15 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D,
|
||||
/**
|
||||
* 修改
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @param request
|
||||
* 修改信息
|
||||
* @return /
|
||||
*/
|
||||
@Operation(summary = "修改")
|
||||
@Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
|
||||
@PutMapping("/{id}")
|
||||
protected R update(@PathVariable Long id, @Validated @RequestBody U request) {
|
||||
baseService.update(id, request);
|
||||
@ResponseBody
|
||||
@PutMapping
|
||||
protected R update(@Validated(BaseRequest.Update.class) @RequestBody C request) {
|
||||
baseService.update(request);
|
||||
return R.ok("修改成功");
|
||||
}
|
||||
|
||||
@@ -141,6 +141,7 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C, U>, V, D,
|
||||
*/
|
||||
@Operation(summary = "删除")
|
||||
@Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
|
||||
@ResponseBody
|
||||
@DeleteMapping("/{ids}")
|
||||
protected R delete(@PathVariable List<Long> ids) {
|
||||
baseService.delete(ids);
|
||||
|
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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.cnadmin.common.base;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Request 基类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/30 21:51
|
||||
*/
|
||||
@Data
|
||||
public class BaseRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 分组校验-创建
|
||||
*/
|
||||
public @interface Create {}
|
||||
|
||||
/**
|
||||
* 分组校验-修改
|
||||
*/
|
||||
public @interface Update {}
|
||||
}
|
@@ -16,7 +16,6 @@
|
||||
|
||||
package top.charles7c.cnadmin.common.base;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
@@ -32,13 +31,11 @@ import top.charles7c.cnadmin.common.model.vo.PageDataVO;
|
||||
* @param <Q>
|
||||
* 查询条件
|
||||
* @param <C>
|
||||
* 创建信息
|
||||
* @param <U>
|
||||
* 修改信息
|
||||
* 创建或修改信息
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 16:54
|
||||
*/
|
||||
public interface BaseService<V, D, Q, C, U> {
|
||||
public interface BaseService<V, D, Q, C extends BaseRequest> {
|
||||
|
||||
/**
|
||||
* 分页查询列表
|
||||
@@ -49,9 +46,7 @@ public interface BaseService<V, D, Q, C, U> {
|
||||
* 分页查询条件
|
||||
* @return 分页列表信息
|
||||
*/
|
||||
default PageDataVO<V> page(Q query, PageQuery pageQuery) {
|
||||
return new PageDataVO<>();
|
||||
}
|
||||
PageDataVO<V> page(Q query, PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
@@ -60,9 +55,7 @@ public interface BaseService<V, D, Q, C, U> {
|
||||
* 查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
default List<V> list(Q query) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<V> list(Q query);
|
||||
|
||||
/**
|
||||
* 查看详情
|
||||
@@ -71,9 +64,7 @@ public interface BaseService<V, D, Q, C, U> {
|
||||
* ID
|
||||
* @return 详情信息
|
||||
*/
|
||||
default D detail(Long id) {
|
||||
return null;
|
||||
}
|
||||
D detail(Long id);
|
||||
|
||||
/**
|
||||
* 新增
|
||||
@@ -82,19 +73,15 @@ public interface BaseService<V, D, Q, C, U> {
|
||||
* 创建信息
|
||||
* @return 自增 ID
|
||||
*/
|
||||
default Long create(C request) {
|
||||
return null;
|
||||
}
|
||||
Long create(C request);
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @param request
|
||||
* 修改信息
|
||||
*/
|
||||
default void update(Long id, U request) {}
|
||||
void update(C request);
|
||||
|
||||
/**
|
||||
* 删除
|
||||
@@ -102,5 +89,5 @@ public interface BaseService<V, D, Q, C, U> {
|
||||
* @param ids
|
||||
* ID 列表
|
||||
*/
|
||||
default void delete(List<Long> ids) {}
|
||||
void delete(List<Long> ids);
|
||||
}
|
||||
|
@@ -40,10 +40,19 @@ import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
* Mapper 接口
|
||||
* @param <T>
|
||||
* 实体类
|
||||
* @param <V>
|
||||
* 列表信息
|
||||
* @param <D>
|
||||
* 详情信息
|
||||
* @param <Q>
|
||||
* 查询条件
|
||||
* @param <C>
|
||||
* 创建或修改信息
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 21:52
|
||||
*/
|
||||
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C, U> implements BaseService<V, D, Q, C, U> {
|
||||
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C extends BaseRequest>
|
||||
implements BaseService<V, D, Q, C> {
|
||||
|
||||
@Autowired
|
||||
protected M baseMapper;
|
||||
@@ -68,8 +77,7 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C, U>
|
||||
|
||||
@Override
|
||||
public D detail(Long id) {
|
||||
T entity = baseMapper.selectById(id);
|
||||
CheckUtils.throwIfNull(entity, String.format("ID为 [%s] 的记录已不存在", id));
|
||||
T entity = this.getById(id);
|
||||
return BeanUtil.copyProperties(entity, detailVoClass);
|
||||
}
|
||||
|
||||
@@ -79,7 +87,7 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C, U>
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(Long id, U request) {
|
||||
public void update(C request) {
|
||||
T entity = BeanUtil.copyProperties(request, entityClass);
|
||||
baseMapper.updateById(entity);
|
||||
}
|
||||
@@ -90,6 +98,19 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C, U>
|
||||
baseMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ID 查询
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @return 实体信息
|
||||
*/
|
||||
protected T getById(Long id) {
|
||||
T entity = baseMapper.selectById(id);
|
||||
CheckUtils.throwIfNull(entity, String.format("ID为 [%s] 的记录已不存在", id));
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类 Class 对象
|
||||
*
|
||||
|
Reference in New Issue
Block a user