一. 需求:
如下图所示,需要同时查询用户id 和版本号,这里使用MybatisPlus的构造器构造or()条件查询
二. 代码如下:
1.入参对象
package com.leigod.modules.gamemaster.dto.Request; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import lombok.Data; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; /** * @author: Sam.yang * @date: 2020/11/19 17:44 * @desc: 崩溃日志上传 */ @Data @ApiModel(value = "崩溃日志上传d对象") public class CrashLogQryRequest { /** * 开始时间 */ @ApiModelProperty(value = "查询开始时间 格式 yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") private Date begin; /** * 结束时间 */ @ApiModelProperty(value = "查询结束时间 格式 yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd") private Date end; /** * 用户id */ @ApiModelProperty(value = "用户id") private String query; @ApiModelProperty(value = "日志类型 1.崩溃日志, 2.语音异常") private Integer type; /** * 渠道 */ @ApiModelProperty(value = "渠道") private Integer reqChannel; /** * 解决状态 0-已解决 1-为解决 */ @ApiModelProperty(value = "解决状态 0-已解决 1-为解决 不传则查询全部") private Integer status; }
2.接口定义:
/** * 查询崩溃日志列表 * * @param pageSize 每页记录数 * @param pageNo 当前页码 * @param request 查询对象 * @return 分页结果 */ @GetMapping("/list") @ApiOperation(value = "崩溃日志列表(批量查询)") public Result<IPage<CrashLog>> listCrashLog(@RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNo, CrashLogQryRequest request) { LambdaQueryWrapper<CrashLog> wrapper = Wrappers.<CrashLog>lambdaQuery() .eq(!ObjectUtils.isEmpty(request.getStatus()), CrashLog::getStatus, request.getStatus()) .eq(!ObjectUtils.isEmpty(request.getReqChannel()), CrashLog::getReqChannel, request.getReqChannel()) .eq(!ObjectUtils.isEmpty(request.getType()), CrashLog::getType, request.getType()) .orderByDesc(CrashLog::getCreateTime); if (StringUtils.isNotBlank(request.getQuery())) { wrapper.nested(x -> x.like(CrashLog::getVersion, request.getQuery()).or().like(CrashLog::getUserId, request.getQuery())); } if (!ObjectUtils.isEmpty(request.getBegin())) { wrapper.ge(CrashLog::getCrashTime, DateUtil.beginOfDay(request.getBegin())); } if (!ObjectUtils.isEmpty(request.getEnd())) { wrapper.le(CrashLog::getCrashTime, DateUtil.endOfDay(request.getEnd())); } Page<CrashLog> dbPage = crashLogService.page(new Page<>(pageNo, pageSize), wrapper); return Result.ok(dbPage); }
最终打印出来的SQL结果:
SELECT id,user_id,version,crash_log_url,crash_time,type,req_channel,create_time,create_by,update_by,update_time,status FROM crash_log WHERE (type = ? AND (version LIKE ? OR user_id LIKE ?)) ORDER BY create_time DESC