本文最后更新于 2026年5月24日 晚上
获取字典项列表接口的实现
获取字典项列表接口的需求
根据字典编码查询字典项所属字典是否存在
根据字典项标签或字典项值模糊查询
根据状态筛选
按排序顺序排序和按创建时间倒序排序
代码实现
在 DeptController 中添加接口
1 2 3 4 5 6 7 8 9 10 11 12
| @GetMapping("/{dictCode}/items") @PreAuthorize("@ps.hasPermission('blog:dict:query')") @SystemLog(businessName = "分页查询字典项列表") @ApiOperation(value = "字典项列表", notes = "分页查询字典项列表", response = PageVo.class) @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", value = "页号", dataType = "int", paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "int", paramType = "query") }) public ResponseResult getDictItemList(Integer pageNum, Integer pageSize, @PathVariable String dictCode, @Valid DictItemListDto dictItemListDto) { return sysDictItemService.getDictItemList(pageNum, pageSize, dictCode, dictItemListDto); }
|
创建 DictItemListVo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| @Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "字典项响应对象") public class DictItemListVo { @ApiModelProperty(value = "字典项ID", example = "1") private Long id;
@ApiModelProperty(value = "字典项标签", example = "男") private String label;
@ApiModelProperty(value = "关联字典编码,与sys_dict表中的dict_code对应", example = "sex") private String dictCode;
@ApiModelProperty(value = "字典项值", example = "1") private String value;
@ApiModelProperty(value = "状态(0-正常,1-禁用)", example = "0") private String status;
@ApiModelProperty(value = "排序", example = "1") private Integer sort;
@ApiModelProperty(value = "标签类型,用于前端样式展示(如success、warning等)", example = "success") private String tagType; }
|
创建 DictItemListDto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
@Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "字典项列表请求对象") public class DictItemListDto {
@Pattern(regexp = "^$|^[01]$", message = "状态只能为0或1") @ApiModelProperty(value = "状态", example = "0") private String status;
@ApiModelProperty(value = "关键字(字典项标签或字典项值)", example = "字典") private String keywords; }
|
在 SysDictItemServiceImpl 中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| @Autowired private SysDictItemService sysDictItemService;
@Autowired private SysDictService sysDictService;
@Override @Transactional(rollbackFor = Exception.class) public ResponseResult getDictItemList(Integer pageNum, Integer pageSize, String dictCode, DictItemListDto dictItemListDto) { LambdaQueryWrapper<SysDict> dictQueryWrapper = new LambdaQueryWrapper<>(); dictQueryWrapper.eq(SysDict::getDictCode, dictCode); SysDict dict = sysDictService.getOne(dictQueryWrapper); if (dict == null) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "该字典不存在!"); }
LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(SysDictItem::getDictCode, dictCode);
if (StringUtils.hasText(dictItemListDto.getKeywords())) { queryWrapper.and(wrapper -> wrapper .like(SysDictItem::getLabel, dictItemListDto.getKeywords()) .or() .like(SysDictItem::getValue, dictItemListDto.getKeywords()) ); }
if (StringUtils.hasText(dictItemListDto.getStatus())) { queryWrapper.eq(SysDictItem::getStatus, dictItemListDto.getStatus()); }
queryWrapper.orderByAsc(SysDictItem::getSort) .orderByDesc(SysDictItem::getCreateTime);
Page<SysDictItem> page = new Page<>(pageNum, pageSize); sysDictItemService.page(page, queryWrapper);
List<SysDictItem> dictItemList = page.getRecords();
List<DictItemListVo> dictItemListVos = BeanCopyUtils.copyBeanList(dictItemList, DictItemListVo.class);
PageVo pageVo = new PageVo(dictItemListVos, page.getTotal());
return ResponseResult.okResult(pageVo); }
|
需要注意的是
为了避免重复代码过多
我将 获取当前用户拥有的数据权限范围 相关的代码提取出来成 applyDeptDataScopeFilter 方法方便调用了
并且在 AdminUserServiceImpl 中也进行了类似的操作
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/