crud方法命名示例

以下是基于表名dste_project_indicator(项目指标表)的完整命名示例,覆盖各类增删改查场景:

1. 表名与实体类映射

java 复制代码
// 表名:dste_project_indicator
// 实体类:DsteProjectIndicatorEntity
public class DsteProjectIndicatorEntity {
    private Long id;
    private Long projectId;
    private String indicatorCode;
    private String indicatorName;
    // 其他字段...
}

2. 基础增删改查方法

java 复制代码
// 新增
int insert(DsteProjectIndicatorEntity entity);
int save(DsteProjectIndicatorEntity entity);

// 修改
int update(DsteProjectIndicatorEntity entity);
int modify(DsteProjectIndicatorEntity entity);

// 逻辑删除
int logicDeleteById(Long id);
int markAsDeletedById(Long id);

// 物理删除
int deleteById(Long id);
int removeById(Long id);

// 查询单条
DsteProjectIndicatorEntity selectById(Long id);
DsteProjectIndicatorEntity getById(Long id);

// 查询多条
List<DsteProjectIndicatorEntity> selectList(QueryParam param);
List<DsteProjectIndicatorEntity> listByCondition(QueryParam param);

3. 批量操作方法

java 复制代码
// 批量新增
int batchInsert(List<DsteProjectIndicatorEntity> entityList);
int insertBatch(List<DsteProjectIndicatorEntity> entityList);

// 批量修改
int batchUpdate(List<DsteProjectIndicatorEntity> entityList);
int updateBatch(List<DsteProjectIndicatorEntity> entityList);

// 批量逻辑删除
int batchLogicDeleteByIds(List<Long> ids);
int batchMarkAsDeleted(List<Long> ids);

// 批量物理删除
int batchDeleteByIds(List<Long> ids);
int batchRemoveByIds(List<Long> ids);

4. 条件操作方法

java 复制代码
// 条件新增(根据另一个实体创建)
int insertByEntity(DsteProjectIndicatorEntity template);
int saveFromTemplate(DsteProjectIndicatorEntity template);

// 条件修改(根据条件更新部分字段)
int updateByCondition(UpdateParam updateParam, QueryParam queryParam);
int modifyFieldsByCondition(Map<String, Object> fields, QueryParam condition);

// 条件逻辑删除
int logicDeleteByCondition(QueryParam param);
int disableByCondition(QueryParam param);

// 条件物理删除
int deleteByCondition(QueryParam param);
int removeByCondition(QueryParam param);

5. 分页与统计方法

java 复制代码
// 分页查询
Page<DsteProjectIndicatorEntity> selectPage(PageParam pageParam, QueryParam queryParam);
IPage<DsteProjectIndicatorEntity> pageByCondition(PageParam page, QueryParam condition);

// 统计查询
Long countByCondition(QueryParam param);
Integer countActiveIndicators();

6. 复杂业务方法

java 复制代码
// 按项目ID查询指标列表
List<DsteProjectIndicatorEntity> selectByProjectId(Long projectId);
List<DsteProjectIndicatorEntity> listIndicatorsByProject(Long projectId);

// 按指标编码查询(唯一键)
DsteProjectIndicatorEntity selectByCode(String indicatorCode);
DsteProjectIndicatorEntity getByUniqueCode(String code);

// 逻辑删除并关联删除子指标
int logicDeleteWithChildren(Long id);
int disableIndicatorCascade(Long id);

// 批量新增并返回主键
List<Long> batchInsertAndReturnIds(List<DsteProjectIndicatorEntity> entityList);

命名原则总结

  1. 动词选择

    • 新增insert/save/create
    • 修改update/modify/edit
    • 删除delete/remove(物理)、logicDelete/disable/markAsDeleted(逻辑)
    • 查询select/get/list/page
  2. 参数与返回值

    • 单数形式(如 insert)处理单个实体
    • 复数形式(如 batchInsert)处理集合
    • ByXXX 后缀表示按条件操作
  3. 业务场景

    • 包含业务对象名称(如 ProjectIndicator
    • 特殊场景使用特定动词(如 archive/invalidate/restore
  4. 代码风格一致性

    • 保持项目内方法命名统一(如统一用 insertsave
    • 使用 Entity/Param/VO 明确参数类型

示例实现(MyBatis Mapper)

java 复制代码
public interface DsteProjectIndicatorMapper {
    // 单条新增
    @Insert("INSERT INTO dste_project_indicator (...) VALUES (...)")
    int insert(DsteProjectIndicatorEntity entity);

    // 批量新增
    @Insert("<script>INSERT INTO dste_project_indicator (...) VALUES " +
            "<foreach collection='list' item='item' separator=','>(...)</foreach></script>")
    int batchInsert(@Param("list") List<DsteProjectIndicatorEntity> entityList);

    // 条件逻辑删除
    @Update("UPDATE dste_project_indicator SET delete_at = NOW() WHERE project_id = #{projectId}")
    int logicDeleteByProjectId(@Param("projectId") Long projectId);

    // 分页查询
    List<DsteProjectIndicatorEntity> selectPage(
        @Param("page") PageParam pageParam, 
        @Param("condition") QueryParam condition
    );
}

根据实际业务需求(如数据库类型、ORM框架、事务要求等),选择最合适的命名方式和实现策略。

相关推荐
feiyangqingyun1 天前
Qt项目作品在苹果macos上编译运行效果/视频监控系统/物联网平台等
开发语言·qt·macos
GL-Yang1 天前
2025年-集合类面试题
java·面试
你不是我我1 天前
【Java 开发日记】我们来说一说 Redisson 的原理
java·开发语言
kk”1 天前
C++ stack 和 queue
开发语言·c++
Matlab仿真实验室1 天前
基于Matlab实现双目图计算深度图
开发语言·数码相机·matlab·双目图计算深度图
李憨憨1 天前
Java处理大型 Excel 文件(超过 100 万行)难题
java
QT 小鲜肉1 天前
【数据结构与算法基础】05. 栈详解(C++ 实战)
开发语言·数据结构·c++·笔记·学习·算法·学习方法
老K的Java兵器库1 天前
Collections 工具类 15 个常用方法源码:sort、binarySearch、reverse、shuffle、unmodifiableXxx
java·开发语言·哈希算法
武子康1 天前
Java-153 深入浅出 MongoDB 全面的适用场景分析与选型指南 场景应用指南
java·开发语言·数据库·mongodb·性能优化·系统架构·nosql
rit84324991 天前
ES6 箭头函数:告别 `this` 的困扰
开发语言·javascript·es6