Appearance
06 · 详情接口与自定义接口
配套文档:05 增删改查;本文讲「标准五件套之外」的接口怎么写
一、详情接口(findById):富 VO 组装
详情接口的本质:主表查一次 + 一堆 Service 补充查询拼装成一个富 VO。CustomerInfoController:
java
@PostMapping("/findById")
public Result<CustomerInfoVo> findOne(@RequestBody Long id) {
return Result.data(customerInfoService.getCustomerInfoById(id));
}
getCustomerInfoById 的组装过程(逐步给 VO 填值):
java
CustomerInfo entity = this.baseMapper.selectById(id); // 1. 主表
CustomerInfoVo vo = CustomerInfoConverter.convertEntityToVo(entity); // 2. 实体→VO
vo.setEditable(checkEditCustomerPermission(id)); // 3. 权限:当前用户能否编辑
// 4. 字典码 → 中文名(natureType 是枚举 code,查出 label)
vo.setNatureTypeName(saasClientDictTypeServer.findDictItemByItemValue(NATURE_TYPE, entity.getNatureType()).getItemLabel());
// 5. 行业:末级行业名 + 祖先路径拼出 industryIds
vo.setIndustryName(industryDictService.getIndustryNameById(industryId));
// 6. 继续补地区列表、品牌名、首位签约人、客户等级...
注意点:
- 详情 VO ≠ 实体:VO 里多了一堆
xxxName(中文名)、industryIds(路径数组)这类展示字段,实体里只有xxxCode/xxxId这类存储字段; - 权限融合:
checkEditCustomerPermission查「当前用户是否是跟进人/领导」,决定前端是否显示编辑按钮; - 逐个补全:每个关联字段都是单独一个 Service 调用,没有一次 JOIN 全查出来——因为详情只查一条,性能不敏感,可读性优先。
二、自定义接口的四种典型类型
2.1 下拉搜索(getSelectList)
给前端下拉框用,返回精简的 id + name + 少量字段:
java
// Controller
@PostMapping("/getSelectList")
public Result<List<Map<String, Object>>> getSelectList(@RequestParam(required = false) String customerName) {
return Result.data(customerInfoService.getCustomerListForDropdown(customerName));
}
// Service:模糊查客户,再批量查工商信息回填(避免 N+1)
public List<Map<String, Object>> getCustomerListForDropdown(String customerName) {
QueryWrapper<CustomerInfo> wrapper = new QueryWrapper<>();
if (customerName != null && !customerName.trim().isEmpty()) {
wrapper.like("customer_name", customerName.trim());
}
wrapper.select("id", "customer_name", "nature_type"); // 只查需要的列
List<CustomerInfo> list = this.list(wrapper);
// 收集所有 id → 一次性批量查工商 → group 成 Map → 回填
List<Long> ids = list.stream().map(CustomerInfo::getId).collect(toList());
Map<Long, CustomerBusinessInfo> bizMap = customerBusinessInfoService.getCustomerBusinessMapByCustomerIds(ids);
return list.stream().map(c -> { ... item.put("customer_license", bizMap.get(...)...); ... });
}
套路:wrapper.select(...) 只查需要的列 + 批量查关联表回填,避免循环查询(N+1)。
2.2 数据范围查询(queryMyCustomers / queryTeamCustomers)
同一个列表,换个数据权限,就是新接口。核心是注入不同的 EXISTS 子查询(详见 04 第六章、07 第三章):
java
@PostMapping("/query/my/customers") // 我的客户
@PostMapping("/query/team/customers") // 团队客户
2.3 导出(export)
导出走异步任务,接口立刻返回一个任务 id,前端轮询拿结果文件:
java
@PostMapping("/export/all")
public Result<Long> exportAll(@RequestBody QueryModel model) {
return Result.data(customerInfoService.exportAll(model)); // 返回 taskId
}
2.4 外部服务对接
CRM 不自己存工商数据,而是转发给集成中心(saasEnterpriseServer):
java
@GetMapping("/query/businessInfoList")
public Result<List<SaasEnterpriseQueryVo>> getBusinessInfoList(@RequestParam String customerName) {
return Result.data(customerInfoService.getCustomerBusinessInfoList(customerName));
}
// Service 内部调 component/integrated/SaasEnterpriseServer(Feign 封装)
三、内部复用的「查询工具方法」
CustomerInfoService 里还有一批供其他模块内部调用的方法(不是对外接口,但常被引用):
| 方法 | 用途 |
|---|---|
findByCustomerIds / findByCustomerNames | 按 id/名批量查实体 |
getCustomerMapByIds | 批量查后转 Map<id, CustomerInfo>(Collectors.toMap) |
findVosByCustomerNameLike | 模糊查 VO 列表 |
isCustomerNameExist | 查重(save/edit 时用) |
这些方法体现了**「批量查询 + 转 Map 回填」**的项目惯例,是避免 N+1 的核心手段。
四、小结:自定义接口的万能公式
在 CRM 里加一个自定义接口,就是三步:
text
1. Controller 加 @PostMapping/@GetMapping,调 Service
2. ServiceImpl 写方法:查主表 → 收集 id → 批量查关联 → group 成 Map → 回填/组装
3. 返回 Result<VO/List/Boolean>(查询用 VO,写入用 Boolean)
没有隐藏魔法:所有「自定义接口」都是普通 Spring MVC 方法,只是遵循了这套分层 + 批量查询的约定。
五、下一步
07 讲三条「隐形的规则」:多租户、逻辑删除、数据权限——理解它们,才能真正读懂 Provider 里那些「多出来的 WHERE 和 EXISTS」是怎么来的。
