『博客开发日记-后台』之获取字典项列表接口的实现

本文最后更新于 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
/**
* 字典项列表请求DTO
*/
@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();

//封装查询结果为Vo返回
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/


『博客开发日记-后台』之获取字典项列表接口的实现
http://example.com/2026/05/24/『博客开发日记-后台』之获取字典项列表接口的实现/
作者
云梦泽
发布于
2026年5月24日
更新于
2026年5月24日
许可协议