自定义SQL
SELECT
vehicle_model_id,
vehicle_model_code,
vehicle_model_name,
COUNT(1) total,
SUM(mileage) mileage,
SUM(averageSpeed) / COUNT(1) averageSpeed,
SUM(mileage) / COUNT(1) averageMileage,
SUM(energy) energy,
SUM(energy) / COUNT(1) averageEnergy,
SUM(electricQuantity) electricQuantity,
SUM(electricQuantity) / COUNT(1) averageElectricQuantity,
SUM(charge) charge
FROM
${tableName} AS d
JOIN
business_info AS b ON d.did = b.did
GROUP BY vehicle_model_id , vehicle_model_code , vehicle_model_name
${ew.sqlSegment}
Mapper
List selectVehicleModelMileageDayStatistics(RowBounds rowBounds, @Param("tableName") String tableName, @Param("ew") Wrapper wrapper);
实现类
@Override
public Page vehicleModelMileageDayStatistics(String groupId, List vehicleModelCodeList, String day, String sortColumn, Boolean isAsc, Page page) {
Wrapper wrapper = new EntityWrapper<>();
if (StrUtil.isNotBlank(groupId)) {
wrapper.like(Business_info.GROUP_PATH, "#" + groupId + "#");
}
String tableName = "day";
if (StrUtil.isNotBlank(day)) {//如果传递了yyyyMMdd
if (day.equals(DateUtil.format(new Date(), "yyyyMMdd"))) {//查询今天
} else {
tableName += "_" + day;
}
}
if (CollectionUtil.isNotEmpty(vehicleModelCodeList)) {
wrapper.in("vehicle_model_code", vehicleModelCodeList);
}
if (StrUtil.isNotBlank(sortColumn)) {
if (isAsc != null) {
wrapper.orderBy(sortColumn, isAsc);
} else {
wrapper.orderBy(sortColumn);
}
}
page.setRecords(dayService.selectVehicleModelMileageDayStatistics(page, tableName, wrapper));
return page;
}
调用代码
Page p = bigDataNeApi.vehicleModelMileageDayStatistics("", null, "20170728", "mileage", false, new Page(1, 100));
log.info("{}", JSON.toJSONString(p, SerializerFeature.PrettyFormat));
mybatis-plus生成的语句为
SELECT vehicle_model_id, vehicle_model_code, vehicle_model_name, COUNT(1) total, SUM(mileage) mileage, SUM(averageSpeed) / COUNT(1) averageSpeed, SUM(mileage) / COUNT(1) averageMileage, SUM(energy) energy, SUM(energy) / COUNT(1) averageEnergy, SUM(electricQuantity) electricQuantity, SUM(electricQuantity) / COUNT(1) averageElectricQuantity, SUM(charge) charge FROM day AS d JOIN business_info AS b ON d.did = b.did GROUP BY vehicle_model_id , vehicle_model_code , vehicle_model_name WHERE ORDER BY mileage DESC
where 那个地方后面直接就跟了order by 导致错误。
难道映射文件不应该???
${ew.sqlSegment}
不是自定义的sql就没有出现这个问题。