Files
aibridge/docs/codedev/05-table-operation.md
T
2026-05-18 09:28:15 +08:00

5.2 KiB

表格操作示例

基于 TableExampleController 和 tableDemo 的 CRUD + 特殊操作 源码分析时间:2026-04-01


一、功能概述

TableExample 是最完整的 CRUD 参考,包含:

  • 基础 CRUD
  • 树形表格
  • 批注功能
  • 行编辑
  • 标记功能

二、后端实现

2.1 列表查询

@GetMapping
@SaCheckPermission("extend.tableDemo.commonTable")
public ActionResult<PageListVO<TableExampleListVO>> list(PaginationTableExample pagination) {
    List<TableExampleEntity> data = tableExampleService.getList(pagination);
    List<TableExampleListVO> list = JsonUtil.getJsonToList(data, TableExampleListVO.class);

    // 处理用户信息
    List<String> userIds = list.stream().map(TableExampleListVO::getRegistrant).collect(Collectors.toList());
    List<UserEntity> userList = userService.getUserName(userIds);
    for (TableExampleListVO vo : list) {
        UserEntity user = userList.stream()
            .filter(t -> t.getId().equals(vo.getRegistrant()))
            .findFirst().orElse(null);
        vo.setRegistrant(user != null ? user.getRealName() + "/" + user.getAccount() : "");
    }

    return ActionResult.page(list, JsonUtil.getJsonToBean(pagination, PaginationVO.class));
}

2.2 创建

@PostMapping
@SaCheckPermission("extend.tableDemo.commonTable")
public ActionResult create(@RequestBody @Valid TableExampleCrForm form) {
    TableExampleEntity entity = JsonUtil.getJsonToBean(form, TableExampleEntity.class);

    // 默认值处理
    entity.setCostAmount(entity.getCostAmount() == null ? new BigDecimal("0") : entity.getCostAmount());
    entity.setSign("0000000");  // 标记初始化

    tableExampleService.create(entity);
    return ActionResult.success(MsgCode.SU001.get());
}

2.3 批注功能

// 发送批注
@PostMapping("/{id}/Postil")
public ActionResult<Object> sendPostil(@PathVariable("id") String id, @RequestBody PostilSendForm form) {
    TableExampleEntity entity = tableExampleService.getInfo(id);

    // 构建批注
    PostilModel model = new PostilModel();
    model.setCreatorTime(DateUtil.getNow("+8"));
    model.setText(form.getText());
    model.setUserId(UserProvider.getUser().getUserName() + "/" + UserProvider.getUser().getUserAccount());

    List<PostilModel> list = new ArrayList<>();
    list.add(model);

    // 合并已有批注
    if (!StringUtil.isEmpty(entity.getPostilJson())) {
        list.addAll(JsonUtil.getJsonToList(entity.getPostilJson(), PostilModel.class));
    }

    entity.setPostilJson(JsonUtil.getObjectToString(list));
    entity.setPostilCount(list.size());
    tableExampleService.update(id, entity);

    return ActionResult.success(MsgCode.SU012.get());
}

// 获取批注
@GetMapping("/{id}/Actions/Postil")
public ActionResult<PostilInfoVO> getPostil(@PathVariable("id") String id) {
    TableExampleEntity entity = tableExampleService.getInfo(id);
    PostilInfoVO vo = new PostilInfoVO();
    vo.setPostilJson(entity.getPostilJson());
    return ActionResult.success(vo);
}

2.4 行编辑

@PutMapping("/{id}/Actions/RowsEdit")
public ActionResult<Object> rowEditing(@PathVariable("id") String id, @RequestBody @Valid TableExampleRowUpForm form) {
    TableExampleEntity entity = JsonUtil.getJsonToBean(form, TableExampleEntity.class);

    // 默认值处理
    entity.setCostAmount(entity.getCostAmount() == null ? new BigDecimal("0") : entity.getCostAmount());
    entity.setId(id);

    boolean flag = tableExampleService.rowEditing(entity);
    return flag ? ActionResult.success(MsgCode.SU004.get()) : ActionResult.fail(MsgCode.FA002.get());
}

三、Mapper 查询模式

// 关键字模糊查询
default List<Entity> getList(String keyword) {
    QueryWrapper<Entity> wrapper = new QueryWrapper<>();
    if (!StringUtils.isEmpty(keyword)) {
        wrapper.lambda()
            .and(t -> t.like(Entity::getCustomerName, keyword)
                .or().like(Entity::getProjectName, keyword));
    }
    wrapper.lambda().orderByDesc(Entity::getRegisterDate);
    return this.selectList(wrapper);
}

// 标签查询
default List<Entity> getList(Pagination pagination) {
    QueryWrapper<Entity> wrapper = new QueryWrapper<>();
    String sign = pagination.getFSign();
    if (!StringUtils.isEmpty(sign)) {
        String[] arraySign = sign.split(",");
        for (int i = 0; i < arraySign.length; i++) {
            String item = arraySign[i];
            if (i == 0) {
                wrapper.lambda().like(Entity::getProjectCode, item);
            } else {
                wrapper.lambda().or(t -> t.like(Entity::getProjectCode, item));
            }
        }
    }
    // 排序
    if (StringUtils.isEmpty(pagination.getSidx())) {
        wrapper.lambda().orderByDesc(Entity::getRegisterDate);
    } else {
        wrapper.lambda().orderByDesc(pagination.getSidx());
    }
    return this.selectList(wrapper);
}

四、参考文件

文件 说明
TableExampleController.java 完整 CRUD + 批注/行编辑
TableExampleMapper.java QueryWrapper 查询模式
TableExampleServiceImpl.java 业务逻辑
tableDemo/commonTable 基础列表
tableDemo/postilTable 批注表格
tableDemo/redactTable 行编辑表格