mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 22:57:19 +08:00 
			
		
		
		
	feat(extension/crud): 新增多种实体 Base 模型降低 BaseService 耦合
This commit is contained in:
		| @@ -0,0 +1,67 @@ | ||||
| /* | ||||
|  * 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.extension.crud.model.entity; | ||||
|  | ||||
| import com.baomidou.mybatisplus.annotation.FieldFill; | ||||
| import com.baomidou.mybatisplus.annotation.TableField; | ||||
|  | ||||
| import java.io.Serial; | ||||
| import java.time.LocalDateTime; | ||||
|  | ||||
| /** | ||||
|  * 实体类基类 | ||||
|  * | ||||
|  * <p> | ||||
|  * 通用字段:创建人、创建时间 | ||||
|  * </p> | ||||
|  * | ||||
|  * @author Charles7c | ||||
|  * @since 2.0.1 | ||||
|  */ | ||||
| public class BaseCreateDO extends BaseIdDO { | ||||
|  | ||||
|     @Serial | ||||
|     private static final long serialVersionUID = 1L; | ||||
|  | ||||
|     /** | ||||
|      * 创建人 | ||||
|      */ | ||||
|     @TableField(fill = FieldFill.INSERT) | ||||
|     private Long createUser; | ||||
|  | ||||
|     /** | ||||
|      * 创建时间 | ||||
|      */ | ||||
|     @TableField(fill = FieldFill.INSERT) | ||||
|     private LocalDateTime createTime; | ||||
|  | ||||
|     public Long getCreateUser() { | ||||
|         return createUser; | ||||
|     } | ||||
|  | ||||
|     public void setCreateUser(Long createUser) { | ||||
|         this.createUser = createUser; | ||||
|     } | ||||
|  | ||||
|     public LocalDateTime getCreateTime() { | ||||
|         return createTime; | ||||
|     } | ||||
|  | ||||
|     public void setCreateTime(LocalDateTime createTime) { | ||||
|         this.createTime = createTime; | ||||
|     } | ||||
| } | ||||
| @@ -18,10 +18,8 @@ package top.continew.starter.extension.crud.model.entity; | ||||
|  | ||||
| import com.baomidou.mybatisplus.annotation.FieldFill; | ||||
| import com.baomidou.mybatisplus.annotation.TableField; | ||||
| import com.baomidou.mybatisplus.annotation.TableId; | ||||
|  | ||||
| import java.io.Serial; | ||||
| import java.io.Serializable; | ||||
| import java.time.LocalDateTime; | ||||
|  | ||||
| /** | ||||
| @@ -30,17 +28,11 @@ import java.time.LocalDateTime; | ||||
|  * @author Charles7c | ||||
|  * @since 1.0.0 | ||||
|  */ | ||||
| public class BaseDO implements Serializable { | ||||
| public class BaseDO extends BaseIdDO { | ||||
|  | ||||
|     @Serial | ||||
|     private static final long serialVersionUID = 1L; | ||||
|  | ||||
|     /** | ||||
|      * ID | ||||
|      */ | ||||
|     @TableId | ||||
|     private Long id; | ||||
|  | ||||
|     /** | ||||
|      * 创建人 | ||||
|      */ | ||||
| @@ -65,14 +57,6 @@ public class BaseDO implements Serializable { | ||||
|     @TableField(fill = FieldFill.UPDATE) | ||||
|     private LocalDateTime updateTime; | ||||
|  | ||||
|     public Long getId() { | ||||
|         return id; | ||||
|     } | ||||
|  | ||||
|     public void setId(Long id) { | ||||
|         this.id = id; | ||||
|     } | ||||
|  | ||||
|     public Long getCreateUser() { | ||||
|         return createUser; | ||||
|     } | ||||
|   | ||||
| @@ -0,0 +1,52 @@ | ||||
| /* | ||||
|  * 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.extension.crud.model.entity; | ||||
|  | ||||
| import com.baomidou.mybatisplus.annotation.TableId; | ||||
|  | ||||
| import java.io.Serial; | ||||
| import java.io.Serializable; | ||||
|  | ||||
| /** | ||||
|  * 实体类基类 | ||||
|  * | ||||
|  * <p> | ||||
|  * 通用字段:ID 主键 | ||||
|  * </p> | ||||
|  * | ||||
|  * @author Charles7c | ||||
|  * @since 2.0.1 | ||||
|  */ | ||||
| public class BaseIdDO implements Serializable { | ||||
|  | ||||
|     @Serial | ||||
|     private static final long serialVersionUID = 1L; | ||||
|  | ||||
|     /** | ||||
|      * ID | ||||
|      */ | ||||
|     @TableId | ||||
|     private Long id; | ||||
|  | ||||
|     public Long getId() { | ||||
|         return id; | ||||
|     } | ||||
|  | ||||
|     public void setId(Long id) { | ||||
|         this.id = id; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,67 @@ | ||||
| /* | ||||
|  * 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.extension.crud.model.entity; | ||||
|  | ||||
| import com.baomidou.mybatisplus.annotation.FieldFill; | ||||
| import com.baomidou.mybatisplus.annotation.TableField; | ||||
|  | ||||
| import java.io.Serial; | ||||
| import java.time.LocalDateTime; | ||||
|  | ||||
| /** | ||||
|  * 实体类基类 | ||||
|  * | ||||
|  * <p> | ||||
|  * 通用字段:创建人、创建时间 | ||||
|  * </p> | ||||
|  * | ||||
|  * @author Charles7c | ||||
|  * @since 2.0.1 | ||||
|  */ | ||||
| public class BaseUpdateDO extends BaseIdDO { | ||||
|  | ||||
|     @Serial | ||||
|     private static final long serialVersionUID = 1L; | ||||
|  | ||||
|     /** | ||||
|      * 修改人 | ||||
|      */ | ||||
|     @TableField(fill = FieldFill.UPDATE) | ||||
|     private Long updateUser; | ||||
|  | ||||
|     /** | ||||
|      * 修改时间 | ||||
|      */ | ||||
|     @TableField(fill = FieldFill.UPDATE) | ||||
|     private LocalDateTime updateTime; | ||||
|  | ||||
|     public Long getUpdateUser() { | ||||
|         return updateUser; | ||||
|     } | ||||
|  | ||||
|     public void setUpdateUser(Long updateUser) { | ||||
|         this.updateUser = updateUser; | ||||
|     } | ||||
|  | ||||
|     public LocalDateTime getUpdateTime() { | ||||
|         return updateTime; | ||||
|     } | ||||
|  | ||||
|     public void setUpdateTime(LocalDateTime updateTime) { | ||||
|         this.updateTime = updateTime; | ||||
|     } | ||||
| } | ||||
| @@ -18,7 +18,6 @@ package top.continew.starter.extension.crud.service; | ||||
|  | ||||
| import cn.hutool.core.lang.tree.Tree; | ||||
| import jakarta.servlet.http.HttpServletResponse; | ||||
| import top.continew.starter.extension.crud.model.req.BaseReq; | ||||
| import top.continew.starter.extension.crud.model.query.PageQuery; | ||||
| import top.continew.starter.extension.crud.model.query.SortQuery; | ||||
| import top.continew.starter.extension.crud.model.resp.PageResp; | ||||
| @@ -35,7 +34,7 @@ import java.util.List; | ||||
|  * @author Charles7c | ||||
|  * @since 1.0.0 | ||||
|  */ | ||||
| public interface BaseService<L, D, Q, C extends BaseReq> { | ||||
| public interface BaseService<L, D, Q, C> { | ||||
|  | ||||
|     /** | ||||
|      * 分页查询列表 | ||||
|   | ||||
| @@ -39,8 +39,7 @@ import top.continew.starter.data.mybatis.plus.base.BaseMapper; | ||||
| import top.continew.starter.data.mybatis.plus.query.QueryWrapperHelper; | ||||
| import top.continew.starter.data.mybatis.plus.service.impl.ServiceImpl; | ||||
| import top.continew.starter.extension.crud.annotation.TreeField; | ||||
| import top.continew.starter.extension.crud.model.entity.BaseDO; | ||||
| import top.continew.starter.extension.crud.model.req.BaseReq; | ||||
| import top.continew.starter.extension.crud.model.entity.BaseIdDO; | ||||
| import top.continew.starter.extension.crud.model.query.PageQuery; | ||||
| import top.continew.starter.extension.crud.model.query.SortQuery; | ||||
| import top.continew.starter.extension.crud.model.resp.PageResp; | ||||
| @@ -65,7 +64,7 @@ import java.util.Optional; | ||||
|  * @author Charles7c | ||||
|  * @since 1.0.0 | ||||
|  */ | ||||
| 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> { | ||||
| public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseIdDO, L, D, Q, C> extends ServiceImpl<M, T> implements BaseService<L, D, Q, C> { | ||||
|  | ||||
|     private final Class<?>[] typeArgumentCache = ClassUtils.getTypeArguments(this.getClass()); | ||||
|     protected final Class<L> listClass = this.currentListClass(); | ||||
| @@ -75,7 +74,7 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO, | ||||
|  | ||||
|     @Override | ||||
|     public PageResp<L> page(Q query, PageQuery pageQuery) { | ||||
|         QueryWrapper<T> queryWrapper = this.handleQueryWrapper(query); | ||||
|         QueryWrapper<T> queryWrapper = this.buildQueryWrapper(query); | ||||
|         IPage<T> page = baseMapper.selectPage(pageQuery.toPage(), queryWrapper); | ||||
|         PageResp<L> pageResp = PageResp.build(page, listClass); | ||||
|         pageResp.getList().forEach(this::fill); | ||||
| @@ -171,7 +170,7 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO, | ||||
|      * @return 列表信息 | ||||
|      */ | ||||
|     protected <E> List<E> list(Q query, SortQuery sortQuery, Class<E> targetClass) { | ||||
|         QueryWrapper<T> queryWrapper = this.handleQueryWrapper(query); | ||||
|         QueryWrapper<T> queryWrapper = this.buildQueryWrapper(query); | ||||
|         // 设置排序 | ||||
|         this.sort(queryWrapper, sortQuery); | ||||
|         List<T> entityList = baseMapper.selectList(queryWrapper); | ||||
| @@ -222,11 +221,12 @@ public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseDO, | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 处理查询条件 | ||||
|      * 构建 QueryWrapper | ||||
|      * | ||||
|      * @param query 查询条件 | ||||
|      * @return QueryWrapper | ||||
|      */ | ||||
|     protected QueryWrapper<T> handleQueryWrapper(Q query) { | ||||
|     protected QueryWrapper<T> buildQueryWrapper(Q query) { | ||||
|         QueryWrapper<T> queryWrapper = new QueryWrapper<>(); | ||||
|         // 解析并拼接查询条件 | ||||
|         return QueryWrapperHelper.build(query, queryFields, queryWrapper); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user