本文最后更新于 2026年5月17日 下午
获取日志列表接口的实现
获取日志列表接口的需求
根据关键字(操作人或页面标题)模糊查询文件
根据访问时间降序排序
支持分页查询
代码实现
新建 LogListDto
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 LogListDto {
@ApiModelProperty(value = "关键字(用于模糊搜索)", example = "日志") private String keywords;
@ApiModelProperty(value = "开始时间", example = "2026-04-01 00:00:00") private String startTime;
@ApiModelProperty(value = "结束时间", example = "2026-04-30 23:59:59") private String endTime; }
|
再创建 VisitLogListVo
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
| @Data @AllArgsConstructor @NoArgsConstructor @ApiModel(description = "日志响应对象") public class VisitLogListVo { @ApiModelProperty(value = "日志ID", example = "1") private Long id;
@ApiModelProperty(value = "访问者IP地址(不明文入库)", example = "127.0.0.0") private String ip;
@ApiModelProperty(value = "操作人(游客/用户(显示用户名))", example = "云梦泽") private String operator;
@ApiModelProperty(value = "访问的页面URL", example = "/post") private String pageUrl;
@ApiModelProperty(value = "页面标题", example = "文章") private String pageTitle;
@ApiModelProperty(value = "来源页面(页面前地址)", example = "/log") private String referrer;
@ApiModelProperty(value = "设备类型(PC/Mobile/Tablet)", example = "PC") private String deviceType;
@ApiModelProperty(value = "地区(国内外)", example = "中国|xx省|xx市|") private String region;
@ApiModelProperty(value = "浏览器类型", example = "Edge") private String browser;
@ApiModelProperty(value = "操作系统", example = "Windows") private String os;
@ApiModelProperty(value = "访问时间", example = "2026-05-16 19:20:30") private Date visitTime;
@ApiModelProperty(value = "访问时间", example = "[2026-04-01, 2026-05-15]") private List<String> visitTime; }
|
创建 LogController
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
|
@RestController @RequestMapping("/logs") @Api(tags = "日志管理", description = "日志管理相关接口") public class LogController { @Autowired private VisitLogService visitLogService;
@GetMapping @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<PageVo> getLogList(Integer pageNum, Integer pageSize, @Valid LogListDto logListDto) { return visitLogService.getLogList(pageNum, pageSize, logListDto); }
}
|
创建 VisitLogServiceImpl
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 56 57 58
|
@Service("visitLogService") public class VisitLogServiceImpl extends ServiceImpl<VisitLogMapper, VisitLog> implements VisitLogService { @Autowired private VisitLogService visitLogService;
@Override public ResponseResult<PageVo> getLogList(Integer pageNum, Integer pageSize, LogListDto logListDto) { LambdaQueryWrapper<VisitLog> queryWrapper = new LambdaQueryWrapper<>();
if (StringUtils.hasText(logListDto.getKeywords())) { queryWrapper.and(wrapper -> wrapper .like(VisitLog::getOperator, logListDto.getKeywords()) .or() .like(VisitLog::getPageTitle, logListDto.getKeywords()) ); }
if (logListDto.getVisitTime() != null && logListDto.getVisitTime().size() == 2) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startTime = sdf.parse(logListDto.getVisitTime().get(0) + " 00:00:00"); Date endTime = sdf.parse(logListDto.getVisitTime().get(1) + " 23:59:59");
queryWrapper.between(VisitLog::getVisitTime, startTime, endTime); } catch (ParseException e) { throw new RuntimeException("访问时间格式不正确", e); } }
queryWrapper.orderByDesc(VisitLog::getVisitTime);
Page<VisitLog> page = new Page<>(pageNum, pageSize); visitLogService.page(page, queryWrapper);
List<VisitLogListVo> visitLogListVos = BeanCopyUtils.copyBeanList(page.getRecords(), VisitLogListVo.class); PageVo pageVo = new PageVo(visitLogListVos, page.getTotal());
return ResponseResult.okResult(pageVo); } }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/