在springboot中,要使用baomidou.mybatisplus对查询结果进行分页的逻辑过程
com.baomidou
mybatis-plus-boot-starter
3.2.0
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
/**
* MybatisPlusConfigurer
*
* Description
*
* Creation Time: 2019/11/25 16:37.
*
*/
@Configuration
public class MybatisPlusConfigurer {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.a.b.c.entity.alarm.GDWarnConfig;
import com.a.b.c.entity.alarm.GAStatistics;
import java.util.Date;
import java.util.List;
import java.util.Map;
public interface GAQueryService {
/**
* get alert result.
*
* params timeSlots:[startTime:Date,endTime:Date]
* params pageNum:
* params pageSize:
*
* @return Page
*/
Page getAlertResult(Map timeSlots, List deviceCodeList, List indicatorIdList, List indicatorRuleNameList, String status, int pageNum, int pageSize);
}
若不想在service层,写分页的处理,可以直接在mapper层引入!详情可见https://blog.csdn.net/Milan__Kundera/article/details/97615996
本案例是在service层,进行自定义分页
List getAlertResult(Map params);
Collections.sort(result, new Comparator() {
@Override
public int compare(GAStatistics o1, GAStatistics o2) {
long time1 = o1.getAlertTime().getTime();
long time2 = o2.getAlertTime().getTime();
long subtract = time1 -time2;
return subtract == 0?0:subtract > 0?-1:1;
}
});
if (pageSize == -1){
GAStatisticsPage.setRecords(result);
}else {
GAStatisticsPage.setRecords(getPagingDate(result,pageNum,pageSize));
}
}else if (Objects.equals("1",status)){
Map params = new HashMap<>();
GAStatisticsPage.setCurrent(pageNum);
GAStatisticsPage.setSize(pageSize);
params.put("page",GAStatisticsPage);
params.put("timeSlots",timeSlots);
params.put("deviceCode",deviceCodeList);
params.put("indicatorId",indicatorIdList);
params.put("indicatorRuleName",indicatorRuleNameList);
List alertResultList = gemAlarmQueryMapper.getAlertResult(params);
/*alertResultList = transformData(alertResultList);*/
GAStatisticsPage.setRecords(alertResultList);
}
return GAStatisticsPage;
直接把参数封装在map中,进而送到mapper映射的SQL进行分页查询!
Page alertResultPage = gemAlarmQueryService.getAlertResult(timeSlots, null, null, null, "0", 12, -1);
List alertResultList = alertResultPage.getRecords();
//根据级别、时间排序
Collections.sort(alertResultList, new Comparator() {
@Override
public int compare(GAStatistics o1, GAStatistics o2) {
try {
String indicatorRuleName1 = o1.getIndicatorRuleName();
String indicatorRuleName2 = o2.getIndicatorRuleName();
long alertTime1 = o1.getAlertTime().getTime();
long alertTime2 = o2.getAlertTime().getTime();
if (!Objects.equals(indicatorRuleName1,indicatorRuleName2)){
int ascSubtract = indicatorRuleName1.compareTo(indicatorRuleName2);
return ascSubtract>0?1:-1;
}else {
long timeSubtract = alertTime1 - alertTime2;
return timeSubtract>0?-1:1;
}
}catch (Exception e){
logger.error(e.getMessage());
return 0;
}
}
});