mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-11-06 00:57:12 +08:00
重构:初步封装后端 CRUD 公共组件(BaseController、BaseService、BaseServiceImpl)
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* 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.util.List;
|
||||
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.common.model.vo.R;
|
||||
|
||||
/**
|
||||
* 控制器基类
|
||||
*
|
||||
* @param <S>
|
||||
* 业务接口
|
||||
* @param <V>
|
||||
* 列表信息
|
||||
* @param <D>
|
||||
* 详情信息
|
||||
* @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> {
|
||||
|
||||
@Autowired
|
||||
protected S baseService;
|
||||
|
||||
/**
|
||||
* 分页查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 分页列表信息
|
||||
*/
|
||||
@Operation(summary = "分页查询列表")
|
||||
@GetMapping
|
||||
protected R<PageInfo<V>> page(@Validated Q query, @Validated PageQuery pageQuery) {
|
||||
PageInfo<V> pageInfo = baseService.page(query, pageQuery);
|
||||
return R.ok(pageInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
@Operation(summary = "查询列表")
|
||||
@GetMapping("/all")
|
||||
protected R<List<V>> list(@Validated Q query) {
|
||||
List<V> list = baseService.list(query);
|
||||
return R.ok(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看详情
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @return 详情信息
|
||||
*/
|
||||
@Operation(summary = "查看详情")
|
||||
@Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
|
||||
@GetMapping("/{id}")
|
||||
protected R<D> detail(@PathVariable Long id) {
|
||||
D detail = baseService.detail(id);
|
||||
return R.ok(detail);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param request
|
||||
* 创建信息
|
||||
* @return 自增 ID
|
||||
*/
|
||||
@Operation(summary = "新增")
|
||||
@PostMapping
|
||||
protected R<Long> create(@Validated @RequestBody C request) {
|
||||
Long id = baseService.create(request);
|
||||
return R.ok("新增成功", id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*
|
||||
* @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);
|
||||
return R.ok("修改成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param ids
|
||||
* ID 列表
|
||||
* @return /
|
||||
*/
|
||||
@Operation(summary = "删除")
|
||||
@Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
|
||||
@DeleteMapping("/{ids}")
|
||||
protected R delete(@PathVariable List<Long> ids) {
|
||||
baseService.delete(ids);
|
||||
return R.ok("删除成功");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
/**
|
||||
* 详情 VO 基类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 10:40
|
||||
*/
|
||||
@Data
|
||||
public class BaseDetailVO extends BaseVO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Long createUser;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@Schema(description = "创建人")
|
||||
private String createUserString;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Schema(description = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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 java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
|
||||
/**
|
||||
* 实体类基类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/12 23:02
|
||||
*/
|
||||
@Data
|
||||
public class BaseEntity implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Long createUser;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private Long updateUser;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
|
||||
/**
|
||||
* 业务接口基类
|
||||
*
|
||||
* @param <V>
|
||||
* 列表信息
|
||||
* @param <D>
|
||||
* 详情信息
|
||||
* @param <Q>
|
||||
* 查询条件
|
||||
* @param <C>
|
||||
* 创建信息
|
||||
* @param <U>
|
||||
* 修改信息
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 16:54
|
||||
*/
|
||||
public interface BaseService<V, D, Q, C, U> {
|
||||
|
||||
/**
|
||||
* 分页查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @param pageQuery
|
||||
* 分页查询条件
|
||||
* @return 分页列表信息
|
||||
*/
|
||||
default PageInfo<V> page(Q query, PageQuery pageQuery) {
|
||||
return new PageInfo<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询列表
|
||||
*
|
||||
* @param query
|
||||
* 查询条件
|
||||
* @return 列表信息
|
||||
*/
|
||||
default List<V> list(Q query) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看详情
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @return 详情信息
|
||||
*/
|
||||
default D detail(Long id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增
|
||||
*
|
||||
* @param request
|
||||
* 创建信息
|
||||
* @return 自增 ID
|
||||
*/
|
||||
default Long create(C request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*
|
||||
* @param id
|
||||
* ID
|
||||
* @param request
|
||||
* 修改信息
|
||||
*/
|
||||
default void update(Long id, U request) {}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*
|
||||
* @param ids
|
||||
* ID 列表
|
||||
*/
|
||||
default void delete(List<Long> ids) {}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* 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.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
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.core.toolkit.ReflectionKit;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
|
||||
import top.charles7c.cnadmin.common.model.query.PageQuery;
|
||||
import top.charles7c.cnadmin.common.model.vo.PageInfo;
|
||||
import top.charles7c.cnadmin.common.util.helper.QueryHelper;
|
||||
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
|
||||
|
||||
/**
|
||||
* 业务实现基类
|
||||
*
|
||||
* @param <M>
|
||||
* Mapper 接口
|
||||
* @param <T>
|
||||
* 实体类
|
||||
* @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> {
|
||||
|
||||
@Autowired
|
||||
protected M baseMapper;
|
||||
|
||||
protected Class<T> entityClass = currentEntityClass();
|
||||
protected Class<V> voClass = currentVoClass();
|
||||
protected Class<D> detailVoClass = currentDetailVoClass();
|
||||
|
||||
@Override
|
||||
public PageInfo<V> page(Q query, PageQuery pageQuery) {
|
||||
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
|
||||
IPage<T> page = baseMapper.selectPage(pageQuery.toPage(), queryWrapper);
|
||||
return PageInfo.build(page, voClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<V> list(Q query) {
|
||||
QueryWrapper<T> queryWrapper = QueryHelper.build(query);
|
||||
List<T> entityList = baseMapper.selectList(queryWrapper);
|
||||
return BeanUtil.copyToList(entityList, voClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public D detail(Long id) {
|
||||
T entity = baseMapper.selectById(id);
|
||||
CheckUtils.throwIfNull(entity, String.format("ID为 [%s] 的记录已不存在", id));
|
||||
return BeanUtil.copyProperties(entity, detailVoClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public abstract Long create(C request);
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(Long id, U request) {
|
||||
T entity = BeanUtil.copyProperties(request, entityClass);
|
||||
baseMapper.updateById(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(List<Long> ids) {
|
||||
baseMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实体类 Class 对象
|
||||
*
|
||||
* @return 实体类 Class 对象
|
||||
*/
|
||||
protected Class<T> currentEntityClass() {
|
||||
return (Class<T>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取列表信息类 Class 对象
|
||||
*
|
||||
* @return 列表信息类 Class 对象
|
||||
*/
|
||||
protected Class<V> currentVoClass() {
|
||||
return (Class<V>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取详情信息类 Class 对象
|
||||
*
|
||||
* @return 详情信息类 Class 对象
|
||||
*/
|
||||
protected Class<D> currentDetailVoClass() {
|
||||
return (Class<D>)ReflectionKit.getSuperClassGenericType(this.getClass(), BaseServiceImpl.class, 3);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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 java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
/**
|
||||
* VO 基类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/1/26 10:40
|
||||
*/
|
||||
@Data
|
||||
public class BaseVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@JsonIgnore
|
||||
private Long updateUser;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@Schema(description = "修改人")
|
||||
private String updateUserString;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@Schema(description = "修改时间")
|
||||
private LocalDateTime updateTime;
|
||||
}
|
||||
Reference in New Issue
Block a user