baomidou.mybatisplus对查询结果进行分页

在springboot中,要使用baomidou.mybatisplus对查询结果进行分页的逻辑过程

maven依赖

<!--mybatis-->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.2.0</version>
</dependency>

config代码

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;

/**
 * <b><code>MybatisPlusConfigurer</code></b>
 * <p>
 * Description
 * </p>
 * <b>Creation Time:</b> 2019/11/25 16:37.
 *
 */
@Configuration
public class MybatisPlusConfigurer {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

SQL映射XML

<?xml version="1.0" encoding="UTF-8" ?>


<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.a.b.c.mapper.alarm.GAQueryMapper">

<select id="getQueryTypeMapKeys" resultType="Map">
	SELECT  a.app_code || ',' || a.module  || ',' || a.dimension as querytype ,b.parameter
   from (SELECT * FROM public.g_a_indicator_relation
	<if test="appCode != null">
	WHERE
	app_code = #{appCode}
	</if>) a
   INNER JOIN public.g_a_indicators b
   ON a.indicator_id=b.indicator_id

</select>
  <select id="getAlertResult" parameterType="java.util.HashMap" resultMap="BaseResultMap">
	SELECT
	warn.alert_time,
	info.device_name,
	rule.indicator_name,
	rule.indicator_rule_name,
	'已恢复' as alert_status,
	warn.ensure_time,
	round(date_part('epoch', (warn.ensure_time-warn.alert_time))::NUMERIC/3600,2) as lasting_time
	FROM
	"public".g_d_w warn
	LEFT JOIN "public".g_d_w_rule rule ON warn.indicator_id = rule.indicator_id
	LEFT JOIN "public".g_a_detail_info info ON warn.device_code = info.device_code
	<where>
		<trim suffixOverrides="and">
			<if test="timeSlots.startTime != null">
				warn.alert_time &gt;= #{timeSlots.startTime} and
			</if>
			<if test="timeSlots.endTime != null">
				warn.alert_time  &lt;= #{timeSlots.endTime} and
			</if>
			<if test="deviceCode != null and deviceCode.size() > 0">
				info.device_code in
				<foreach collection="deviceCode" item="item" index="index" open="(" separator="," close=")">
						#{item,jdbcType=BIGINT}
				</foreach>
				and
			</if>
			<if test="indicatorId != null and indicatorId.size() > 0">
				rule.indicator_id in
				<foreach collection="indicatorId" item="item" index="index" open="(" separator="," close=")">
					#{item,jdbcType=INTEGER}
				</foreach>
				and
			</if>
			<if test="indicatorRuleName != null and indicatorRuleName.size() > 0">
				rule.indicator_rule_name in
				<foreach collection="indicatorRuleName" item="item" index="index" open="(" separator="," close=")">
					#{item}
				</foreach>
				and
			</if>
		</trim>
	</where>
	order by alert_time desc
</select>
<select id="queryBrandsByPage" resultType="com.leyou.item.pojo.Brand">
	select * from tb_brand where `name` like concat('%',#{key},'%')
</select>
</mapper>

service层

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<GAStatistics>
     */
    Page<GAStatistics> getAlertResult(Map<String,Date> timeSlots, List<Long> deviceCodeList, List<Integer> indicatorIdList, List<String> indicatorRuleNameList, String status, int pageNum, int pageSize);
    
}

mapper层

若不想在service层,写分页的处理,可以直接在mapper层引入!详情可见https://blog.csdn.net/Milan__Kundera/article/details/97615996

本案例是在service层,进行自定义分页
​​​​​​​List<GmpAlarmStatistics> getAlertResult(Map params);

impl层

Collections.sort(result, new Comparator<GAStatistics>() {
		@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<String,Object> 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<GAStatistics> alertResultList = gemAlarmQueryMapper.getAlertResult(params);
	/*alertResultList = transformData(alertResultList);*/
	GAStatisticsPage.setRecords(alertResultList);
}
return GAStatisticsPage;

直接把参数封装在map中,进而送到mapper映射的SQL进行分页查询!

controller层

Page<GAStatistics> alertResultPage = gemAlarmQueryService.getAlertResult(timeSlots, null, null, null, "0", 12, -1);
List<GAStatistics> alertResultList = alertResultPage.getRecords();
//根据级别、时间排序
Collections.sort(alertResultList, new Comparator<GAStatistics>() {
	@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;
		}
	}
});

 

发布了160 篇原创文章 · 获赞 25 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/as4589sd/article/details/104539914