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

本文最后更新于 2026年5月24日 下午

获取字典列表接口的实现


获取字典列表接口的需求

根据字典名称或字典编码模糊查询

根据状态筛选

按创建时间降序排序

分页查询


代码实现

前期准备

用 EasyCode 插件生成需要的文件

注意这里也生成了 sys_dict_item 表的相关文件,后续要用上


在 DeptController 中添加接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 字典接口
*/
@RestController
@RequestMapping("/dicts")
@Api(tags = "字典", description = "字典接口")
public class DictController
{
@Autowired
private SysDictService sysDictService;

@GetMapping
@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 getDictList(Integer pageNum, Integer pageSize, @Valid DictListDto dictListDto)
{
return sysDictService.getDictList(pageNum, pageSize, dictListDto);
}
}

创建 DictListVo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "字典响应对象")
public class DictListVo
{
@ApiModelProperty(value = "字典ID", example = "1")
private Long id;

@ApiModelProperty(value = "字典名", example = "性别")
private String name;

@ApiModelProperty(value = "字典编码", example = "sex")
private String dictCode;

@ApiModelProperty(value = "状态(0-正常,1-禁用)", example = "0")
private String status;
}

在 SysDictServiceImpl 中

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
@Autowired
private SysDictService sysDictService;

//获取字典列表
@Override
public ResponseResult getDictList(Integer pageNum, Integer pageSize, DictListDto dictListDto)
{
//构建查询条件
LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<>();

//根据字典名称或字典编码模糊查询
if (StringUtils.hasText(dictListDto.getKeywords())) {
queryWrapper.and(wrapper -> wrapper
.like(SysDict::getName, dictListDto.getKeywords())
.or()
.like(SysDict::getDictCode, dictListDto.getKeywords())
);
}

//根据状态筛选
if (StringUtils.hasText(dictListDto.getStatus())) {
queryWrapper.eq(SysDict::getStatus, dictListDto.getStatus());
}

//按创建时间降序排序
queryWrapper.orderByDesc(SysDict::getCreateTime);

//分页查询
Page<SysDict> page = new Page<>(pageNum, pageSize);
sysDictService.page(page, queryWrapper);

//查询字典表
List<SysDict> dictList = page.getRecords();

//封装查询结果为Vo返回
List<DictListVo> dictListVos = BeanCopyUtils.copyBeanList(dictList, DictListVo.class);

PageVo pageVo = new PageVo(dictListVos, 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日
许可协议