PageHelper分页只会作用于第一次查询,对第二次及以后的查询无效

controller 中使用分页

package com.xl.test.springtest.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xl.test.springtest.pojo.DeptInf;
import com.xl.test.springtest.service.DoubleQuery;

/**
 * 	使用PageHelper分页同时在同一个service事务中进行两次及以上的数据库查询
 * @author Administrator
 *
 */
@Controller		
public class PageHelperDoubleQueryController {
	
	@Autowired
	DoubleQuery doubleQuery;
	
	
	@RequestMapping("testPageHelper")
	@ResponseBody
	public PageInfo<DeptInf> DoubleQuery () {
		/*
		 * 	预期,对返回的DeptInfo集合进行分页,
		 * 	但是,结果没有分页
		 * 	原因,在DoubleQueryImpl中DeptInf的查询时是第二次查询,而PageHelper只对第一次查询起作用
		 */
		PageHelper.startPage(1, 2);
		List<DeptInf> result = doubleQuery.doubleQuery();
		PageInfo<DeptInf> pageInfo = new PageInfo<>(result);
		return pageInfo;
	}
	
}

serviceImpl 中进行多次查询

package com.xl.test.springtest.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xl.test.springtest.dao.DoubleQueryMapper;
import com.xl.test.springtest.dao.OtherQueryMapper;
import com.xl.test.springtest.pojo.DeptInf;
import com.xl.test.springtest.pojo.JobInf;
import com.xl.test.springtest.service.DoubleQuery;

@Service
@Transactional
public class DoubleQueryImpl implements DoubleQuery {
	
	@Autowired
	DoubleQueryMapper doubleQueryMapper;
	
	@Autowired
	OtherQueryMapper otherQueryMapper;
	
	/**
	 * 	本方法中使用了两次数据库查询,PageHelper的分页,只会作用于第一次的查询
	 */
	@Override
	public List<DeptInf> doubleQuery() {
		System.out.println("开始第一次查询。。。");
		
		// 第一次查询, “辅助查询” ;分页作用于本次查询
		List<JobInf> result1 = otherQueryMapper.secondQuery();
		
		System.out.println("开始第二次查询。。。");
		
		// 第二次查询,“目标查询” ,使用第一次的查询结果进行传参;分页对本次查询无效
		List<DeptInf> result2 = doubleQueryMapper.firstQuery(result1);
		
		return result2;
	}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值