您的位置:首页 > 其它

ssm练手(CRUD) 3、利用pagehelper分页,并进行测试

2017-09-05 20:54 363 查看
如题,本篇进行分页测试

1)准备

1.1 maven引入所需的jar包(此处我一开就已经引入了,如图)



1.2)service层

在service层下创建class,代码如下



package com.atguigu.crud.service;

import java.util.List;

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

import com.atguigu.crud.bean.Employee;
import com.atguigu.crud.dao.EmployeeMapper;

@Service
public class EmployeeService {

@Autowired
EmployeeMapper employeeMapper;

//查询所有员工
public List<Employee> getAll() {
// TODO Auto-generated method stub
return employeeMapper.selectByExampleWithDept(null);
}

}


1.3)controller层写控制



package com.atguigu.crud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.atguigu.crud.bean.Employee;
import com.atguigu.crud.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/*
* 处理员工CRUD请求
*/
@Controller
public class EmployeeController {

@Autowired
EmployeeService employeeService;

//查询员工数据(分页查询)
//接受所有向/emps 发送的请求
@RequestMapping("/emps")
public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
//查出了所有员工。但不是分页
//引入PageHelper分页插件
//在查询之前只需要调用如下方法,从第n页查,每次查5条数据
PageHelper.startPage(pn, 5);
//startPage后面紧跟的这个查询就是一个分页查询
List<Employee> emps=employeeService.getAll();
//将查询到的所有数据利用pageInfo进行包装,
//可以显示一些信息(比如在第几页啥的);此处的5表示连续显示的页数
PageInfo page=new PageInfo(emps,5);
model.addAttribute("pageInfo",page);

return "/list";
}
}


1.4)index.jsp代码如下



<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:forward page="/emps"></jsp:forward>


简单说明下运行机制好了:首先我们运行index.jsp文件,然后该文件就会立马跳转请求进入“/emps”;然后controller利用@RequestMapping(“/emps”)接收到该请求,并实现pagehelper,然后返回个字符串“/list”。由于我们在“dispatcherServlet-servlet.xml”中配置了
cea1
前缀和后缀,最终会返回到(“WEB-INF/views/list.jsp”)。由此保护了jsp文件(因为WEB-INF下的文件无法直接访问,因此需要用这种跳转的方式进行访问

2)测试

由于此处是mvc模式,因此测试中我们用到了MockMvc(虚拟mvc)进行测试,并且模拟发送了对”/emps”的跳转请求,从而进行测试,代码如下

package com.atguigu.crud.test;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import com.atguigu.crud.bean.Employee;
import com.github.pagehelper.PageInfo;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})
@WebAppConfiguration

public class MvcTest {

//相当于传入springmvc的ioc
@Autowired
WebApplicationContext context;

//虚拟的mvc请求,获取到处理结果
MockMvc mockMvc;

//每次都初始化一次
@Before
public void initMockMvc(){
//创建虚拟mvc,这样就能返回个Mockmvc
mockMvc=MockMvcBuilders.webAppContextSetup(context).build();
}

@Test
public void testPage() throws Exception{
//发送模拟请求 param是参数,并拿到返回值
MvcResult result=mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "6")).andReturn();

//请求成功后,请求域中会有pageInfo,我们取出pageInfo进行验证
MockHttpServletRequest request=result.getRequest();
PageInfo pi=(PageInfo) request.getAttribute("pageInfo");
System.out.println("当前页码:"+pi.getPageNum());
System.out.println("总页码:"+pi.getPages());
System.out.println("总记录数:"+pi.getTotal());
System.out.println("连续显示的页码");
int[] nums=pi.getNavigatepageNums();
for(int i:nums){
System.out.println(" "+i);
}

//获取员工数据
List<Employee> list= pi.getList();
for(Employee employee:list){
System.out.println("id:"+employee.getEmpId()+" name:"+employee.getEmpName());
}
}
}


我们可以修改框中的部分,来显示当前页面的数据



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  测试 分页