mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 22:57:17 +08:00 
			
		
		
		
	优化:优化后端公共 CRUD 组件-修改接口,将 id 从请求体提取到路径变量,更符合 RESTful 风格
This commit is contained in:
		| @@ -144,7 +144,7 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C>, V, D, Q, | ||||
|     @Operation(summary = "新增数据") | ||||
|     @ResponseBody | ||||
|     @PostMapping | ||||
|     protected R<Long> add(@Validated(BaseRequest.Add.class) @RequestBody C request) { | ||||
|     protected R<Long> add(@Validated @RequestBody C request) { | ||||
|         this.checkPermission("add"); | ||||
|         Long id = baseService.add(request); | ||||
|         return R.ok("新增成功", id); | ||||
| @@ -155,14 +155,16 @@ public abstract class BaseController<S extends BaseService<V, D, Q, C>, V, D, Q, | ||||
|      * | ||||
|      * @param request | ||||
|      *            修改信息 | ||||
|      * @param id | ||||
|      *            ID | ||||
|      * @return / | ||||
|      */ | ||||
|     @Operation(summary = "修改数据") | ||||
|     @ResponseBody | ||||
|     @PutMapping | ||||
|     protected R update(@Validated(BaseRequest.Update.class) @RequestBody C request) { | ||||
|     @PutMapping("/{id}") | ||||
|     protected R update(@Validated @RequestBody C request, @PathVariable Long id) { | ||||
|         this.checkPermission("update"); | ||||
|         baseService.update(request); | ||||
|         baseService.update(request, id); | ||||
|         return R.ok("修改成功"); | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -18,14 +18,10 @@ package top.charles7c.cnadmin.common.base; | ||||
|  | ||||
| import java.io.Serializable; | ||||
|  | ||||
| import javax.validation.constraints.NotNull; | ||||
| import javax.validation.constraints.Null; | ||||
| import javax.validation.groups.Default; | ||||
|  | ||||
| import lombok.Data; | ||||
|  | ||||
| import io.swagger.v3.oas.annotations.media.Schema; | ||||
|  | ||||
| /** | ||||
|  * Request 基类 | ||||
|  * | ||||
| @@ -37,14 +33,6 @@ public class BaseRequest implements Serializable { | ||||
|  | ||||
|     private static final long serialVersionUID = 1L; | ||||
|  | ||||
|     /** | ||||
|      * ID | ||||
|      */ | ||||
|     @Schema(description = "ID") | ||||
|     @Null(message = "新增时,ID 必须为空", groups = Add.class) | ||||
|     @NotNull(message = "修改时,ID 不能为空", groups = Update.class) | ||||
|     private Long id; | ||||
|  | ||||
|     /** | ||||
|      * 分组校验-创建 | ||||
|      */ | ||||
|   | ||||
| @@ -100,8 +100,10 @@ public interface BaseService<V, D, Q, C extends BaseRequest> { | ||||
|      * | ||||
|      * @param request | ||||
|      *            修改信息 | ||||
|      * @param id | ||||
|      *            ID | ||||
|      */ | ||||
|     void update(C request); | ||||
|     void update(C request, Long id); | ||||
|  | ||||
|     /** | ||||
|      * 删除 | ||||
|   | ||||
| @@ -28,9 +28,6 @@ import org.springframework.transaction.annotation.Transactional; | ||||
|  | ||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||||
| import com.baomidou.mybatisplus.core.metadata.IPage; | ||||
| import com.baomidou.mybatisplus.core.metadata.TableInfo; | ||||
| import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||||
| import com.baomidou.mybatisplus.core.toolkit.Assert; | ||||
| import com.baomidou.mybatisplus.core.toolkit.ReflectionKit; | ||||
|  | ||||
| import cn.hutool.core.bean.BeanUtil; | ||||
| @@ -75,7 +72,7 @@ import top.charles7c.cnadmin.common.util.validate.CheckUtils; | ||||
|  * @author Charles7c | ||||
|  * @since 2023/1/26 21:52 | ||||
|  */ | ||||
| public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C extends BaseRequest> | ||||
| public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO, V, D, Q, C extends BaseRequest> | ||||
|     implements BaseService<V, D, Q, C> { | ||||
|  | ||||
|     @Autowired | ||||
| @@ -84,14 +81,12 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext | ||||
|     private final Class<T> entityClass; | ||||
|     private final Class<V> voClass; | ||||
|     private final Class<D> detailVoClass; | ||||
|     private final String entityIdName; | ||||
|  | ||||
|     public BaseServiceImpl() { | ||||
|         this.entityClass = (Class<T>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 1); | ||||
|         this.voClass = (Class<V>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 2); | ||||
|         this.detailVoClass = | ||||
|             (Class<D>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 3); | ||||
|         this.entityIdName = this.currentEntityIdName(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -178,19 +173,15 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext | ||||
|         if (request == null) { | ||||
|             return 0L; | ||||
|         } | ||||
|         // 保存信息 | ||||
|         T entity = BeanUtil.copyProperties(request, entityClass); | ||||
|         baseMapper.insert(entity); | ||||
|         TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); | ||||
|         Object idValue = tableInfo.getPropertyValue(entity, entityIdName); | ||||
|         return Convert.toLong(idValue); | ||||
|         return entity.getId(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     @Transactional(rollbackFor = Exception.class) | ||||
|     public void update(C request) { | ||||
|         Object idValue = ReflectUtil.getFieldValue(request, entityIdName); | ||||
|         T entity = this.getById(idValue); | ||||
|     public void update(C request, Long id) { | ||||
|         T entity = this.getById(id); | ||||
|         BeanUtil.copyProperties(request, entity, CopyOptions.create().ignoreNullValue()); | ||||
|         baseMapper.updateById(entity); | ||||
|     } | ||||
| @@ -258,17 +249,4 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T, V, D, Q, C ext | ||||
|             detailVO.setUpdateUserString(ExceptionUtils.exToNull(() -> userService.getNicknameById(updateUser))); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 获取实体类 ID 名称 | ||||
|      * | ||||
|      * @return 实体类 ID 名称 | ||||
|      */ | ||||
|     private String currentEntityIdName() { | ||||
|         TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); | ||||
|         Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); | ||||
|         String keyProperty = tableInfo.getKeyProperty(); | ||||
|         Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); | ||||
|         return keyProperty; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user