62,628
社区成员
发帖
与我相关
我的任务
分享 <!--Spring 的配置文件,配置和业务逻辑有关的-->
<context:component-scan base-package="cn.crud">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> @Test
public void testPage(){
try {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();
//请求成功之后请求域中会有 pageInfo,我们可以取出出pageInfo进行验证
MockHttpServletRequest request = mvcResult.getRequest();
PageInfo pageInfo = (PageInfo)request.getAttribute("pageInfo");
System.out.println("当前页码:" + pageInfo.getPageNum());
System.out.println("总页码:" + pageInfo.getPages());
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("在页面需要连续显示的页码:");
int[] nums = pageInfo.getNavigatepageNums();
for(int i : nums){
System.out.print(" "+ i);
}
System.out.println("");
//获取员工数据
List<Employee> list = pageInfo.getList();
for (Employee employee : list){
System.out.println("ID:" + employee.getEmpId() + "-------name:" + employee.getEmpName());
}
} catch (Exception e) {
e.printStackTrace();
}
}@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
/**
* 查询所有员工
* @return
*/
public List<Employee> getAll(){
return employeeMapper.selectByExampleWithDept(null);
}
}@Controller
public class EmployeeController {
@Autowired
EmployeeService employeeService;
/*
* 查询员工数据
* @return
*/
@RequestMapping("/emps")
public String getEmps(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model
model){
//在查询之前调用,传入页码以及每页的大小
PageHelper.startPage(pn,5);
List<Employee> emps = employeeService.getAll();
//用 pageInfo 包装查询后的结果,只需要将 pageInfo 交给页面就可以了
//封装了详细的分页信息,包括查询出来的数据,传入连续显示的页数
PageInfo page = new PageInfo(emps,5);
model.addAttribute("pageInfo",page);
return "list";
}
}