见代码注释说明
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;
}
}