一、 描述
项目中用到页面查询,并且涉及到两个表分页查询,搜索条件涉及到两个表。
二、 思路
1、Mapper中自定义表关联的SQL,传入page和组装的QueryWrapper,mybitsPlus会自行把SQL组装在一起。
2、页面搜索条件入参校验,校验是否为空,不为空加入到查询条件,指定对应表中的字段。
三、代码案例
主要看:
5、Service对应的接口方法,处理传参和queryWrapper相关字段;
6、mapper;
7、页面请求接口controller方法getScheduingByRoutineFlight。
1、分页参数PageParam,设置页码和每页条数
@ApiModel("分页参数")
@Data
public class PageParam implements Serializable {
private static final Integer PAGE_NO = 1;
private static final Integer PAGE_SIZE = 10;
@ApiModelProperty(value = "页码,从 1 开始", required = true,example = "1")
@NotNull(message = "页码不能为空")
@Min(value = 1, message = "页码最小值为 1")
private Integer pageNo = PAGE_NO;
@ApiModelProperty(value = "每页条数,最大值为 100", required = true, example = "10")
@NotNull(message = "每页条数不能为空")
@Min(value = 1, message = "页码最小值为 1")
@Max(value = 100, message = "页码最大值为 100")
private Integer pageSize = PAGE_SIZE;
}
2、通用CommonResult ,根据自身项目封装
@Data
public class CommonResult<T> implements Serializable {
/**
* 错误码
*
* @see ErrorCode#getCode()
*/
private Integer code;
/**
* 返回数据
*/
private T data;
/**
* 错误提示,用户可阅读
*
* @see ErrorCode#getMsg() ()
*/
private String msg;
/**
* 将传入的 result 对象,转换成另外一个泛型结果的对象
*
* 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
*
* @param result 传入的 result 对象
* @param 返回的泛型
* @return 新的 CommonResult 对象
*/
public static <T> CommonResult<T> error(CommonResult<?> result) {
return error(result.getCode(), result.getMsg());
}
public static <T> CommonResult<T> error(Integer code, String message) {
Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!");
CommonResult<T> result = new CommonResult<>();
result.code = code;
result.msg = message;
return result;
}
public static <T> CommonResult<T> error(ErrorCode errorCode) {
return error(errorCode.getCode(), errorCode.getMsg());
}
public static <T> CommonResult<T> success(T data) {
CommonResult<T> result = new CommonResult<>();
result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
result.data = data;
result.msg = "成功";
return result;
}
public static <T> CommonResult<T> successMsg(T data,String msg) {
CommonResult<T> result = new CommonResult<>();
result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
result.data = data;
result.msg = msg;
return result;
}
public static boolean isSuccess(Integer code) {
return Objects.equals(code, GlobalErrorCodeConstants.SUCCESS.getCode());
}
@JsonIgnore // 避免 jackson 序列化
public boolean isSuccess() {
return isSuccess(code);
}
@JsonIgnore // 避免 jackson 序列化
public boolean isError() {
return !isSuccess();
}
// ========= 和 Exception 异常体系集成 =========
/**
* 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常
*/
public void checkError() throws ServiceException {
if (isSuccess()) {
return;
}
// 业务异常
throw new ServiceException(code, msg);
}
public static <T> CommonResult<T> error(ServiceException serviceException) {
return error(serviceException.getCode(), serviceException.getMessage());
}
}
3、查询实体SchedulingPageReqVo
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SchedulingPageReqVo extends PageParam {
/**
* 航班日期
*/
private String flightDate;
/**
* 航班号
*/
private String flightNo;
/**
* 整机负责人/放行
*/
private String releasePersonnel;
/**
* 发布状态
*/
private String pushStatus;
/**
* 执行地点
*/
private String carryOutPlace;
/**
* 始发站
*/
private String depAirportName;
/**
* 到达站
*/
private String arrAirportName;
/**
* 机号
*/
private String acReg;
/**
* 排班类型
*/
private Integer scheduleType;
}
4、返回实体
@Data
public class SchedulingPagRespVo {
/**
* 航班日期
*/
private String flightDate;
/**
* 始发站
*/
private String depAirport;
private String depAirportName;
/**
* 到达站
*/
private String arrAirport;
private String arrAirportName;
.......
}
5、Service对应的接口方法,处理传参和queryWrapper相关字段
@Override
public IPage<SchedulingPagRespVo> getSchedulingByFlightPag(SchedulingPageReqVo reqVO) {
IPage<SchedulingPagRespVo> page = new Page<>(reqVO.getPageNo(),reqVO.getPageSize());
QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sc.deleted",0);
queryWrapper.eq("dy.deleted",0);
queryWrapper.eq("schedule_type",1);
if(StringUtils.isNotEmpty(reqVO.getAcReg())){
queryWrapper.like("dy.ac_reg",reqVO.getAcReg());
}
if(StringUtils.isNotEmpty(reqVO.getFlightDate())){
queryWrapper.eq("dy.flight_date",reqVO.getFlightDate());
}
if(StringUtils.isNotEmpty(reqVO.getFlightNo())){
queryWrapper.eq("dy.flight_no",reqVO.getFlightNo());
}
if(StringUtils.isNotEmpty(reqVO.getReleasePersonnel())){
queryWrapper.eq("sc.release_personnel",reqVO.getReleasePersonnel());
}
if(StringUtils.isNotEmpty(reqVO.getReleasePersonnel())){
queryWrapper.eq("sc.push_status",reqVO.getPushStatus());
}
return faultMobleSchedulingMapper.getPageByFlightCustom(page,queryWrapper);
}
6、mapper
/**
*@Description: t_fault_mobile_scheduling 和 t_fault_flight_dynamic 关联分页查询
*@Param:
*@return:
*@Author: guaiyiBug
*@date: 2022/6/15
**/
@Select("select sc.shift_system,sc.air_stand,sc.project_leader,sc.work_items,sc.project_coordinator,sc.check_personnel,sc.release_personnel, sc.schedule_type, " +
"sc.tool_administrator,sc.job_card_no,sc.carry_out_place,sc.remarks,sc.repeat_work_id,sc.is_repeat_work,sc.routine_job_status,sc.push_status," +
"dy.flight_date,dy.dep_airport,dy.arr_airport,dy.flight_no, dy.ac_reg,dy.ac_type,dy.ac_type,dy.ac_engine," +
"dy.htd,dy.etd,dy.atd,dy.hta,dy.eta,dy.ata,dy.flight_cancel,dy.flight_vr,dy.flight_type,dy.task_type," +
"dy.task_type,dy.dep_airport_name,dy.arr_airport_name,dy.stand_out_position,dy.stand_inn_position " +
"from t_fault_mobile_scheduling sc " +
"join t_fault_flight_dynamic dy on sc.dynamic_id = dy.dynamic_id "
+"${ew.customSqlSegment}")
IPage<SchedulingPagRespVo> getPageByFlightCustom(IPage<SchedulingPagRespVo> page, @Param("ew") QueryWrapper<Object> query);
7、页面请求接口controller方法getScheduingByRoutineFlight
@GetMapping("/page")
@ApiOperation("获得航线排班")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
@PreAuthorize("@ss.hasPermission('fault:fault-moble-scheduling:query')")
public CommonResult<PageResult<SchedulingPagRespVo>> getScheduingByRoutineFlight(@Valid SchedulingPageReqVo pageVO){
PageResult<SchedulingPagRespVo> pageResult = new PageResult<>();
//分页获取例行
IPage<SchedulingPagRespVo> schedulingPag = faultMobleSchedulingService.getSchedulingByFlightPag(pageVO);
//获取到的数据根据自身项目封装返回给前台
pageResult.setTotal(schedulingPag.getTotal());
pageResult.setList(schedulingPag.getRecords());
log.info("获得航线排班表关联查询pageVO: {}", pageVO);
System.out.println("tFaultMobleSchedulingDOPageResult获得航线排班表关联查询结果: " + pageResult);
return success(pageResult);
}
8、关联查询打印出的SQL
==> Preparing: SELECT COUNT(*) FROM t_fault_mobile_scheduling sc JOIN t_fault_flight_dynamic dy ON sc.dynamic_id = dy.dynamic_id WHERE (sc.deleted = ? AND dy.deleted = ? AND schedule_type = ?)
==> Parameters: 0(Integer), 0(Integer), 1(Integer)
<== Columns: COUNT(*)
<== Row: 55
<== Total: 1
==> Preparing: select sc.shift_system,sc.air_stand,sc.project_leader,sc.work_items,sc.project_coordinator,sc.check_personnel,sc.release_personnel, sc.schedule_type, sc.tool_administrator,sc.job_card_no,sc.carry_out_place,sc.remarks,sc.repeat_work_id,sc.is_repeat_work,sc.routine_job_status,sc.push_status,dy.flight_date,dy.dep_airport,dy.arr_airport,dy.flight_no, dy.ac_reg,dy.ac_type,dy.ac_type,dy.ac_engine,dy.htd,dy.etd,dy.atd,dy.hta,dy.eta,dy.ata,dy.flight_cancel,dy.flight_vr,dy.flight_type,dy.task_type,dy.task_type,dy.dep_airport_name,dy.arr_airport_name,dy.stand_out_position,dy.stand_inn_position from t_fault_mobile_scheduling sc join t_fault_flight_dynamic dy on sc.dynamic_id = dy.dynamic_id WHERE (sc.deleted = ? AND dy.deleted = ? AND schedule_type = ?) LIMIT ?
==> Parameters: 0(Integer), 0(Integer), 1(Integer), 10(Long)