SSM整合-分页后台代码和使用Spring单元测试测试分页请求

首先从index.jsp页面出发,要发一个请求
index.jsp:

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

然后到控制器中处理请求
com.atguigu.crud.controller.EmployeeController:

package com.atguigu.crud.controller;

import com.atguigu.crud.bean.Employee;
import com.atguigu.crud.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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 java.util.List;

/**
 * @author nyh
 * @create 2018-08-19  13:55
 * 处理员工CRUD(增删改查)请求的控制器
 **/
@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    /**
     *查询员工数据(分页查询)
     *要做分页查询,就要从页面传过来一个pn告诉我们从第几页开始查
     * defaultValue代表如果没有传就从第一页开始
     * @return
     */
    @RequestMapping("/emps")
    public String getEmps(@RequestParam(value = "pn",defaultValue = "1") Integer pn ,
                          Model model){
        /**
         *引入pagehelper分页插件后,在查询之前只需要调用startPage方法
         * 第一个参数是从第几页开始查,也就是页面传过来的pn
         * 第二个参数是每页有多少条记录
         */
        PageHelper.startPage(pn,5);
        //startPage方法后面紧跟的这个查询就是一个分页查询了
        List<Employee> emps = employeeService.getAll();
        //使用psgeInfo包装查询后的结果,只需要将pageInfo交给页面就可以了
        // pageInfo中已经封装好了详细的分页信息了,navigatePages为5代表要连续显示的页数
        PageInfo page = new PageInfo(emps,5);
        model.addAttribute("pageInfo",page);
        return "list";
    }
}

别忘了在views中新建list.jsp页面
上面的PageHelper分页插件要加入jar

        <!--引入pagehelper分页插件-->
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.3</version>
        </dependency>

控制器调用Service查出数据

package com.atguigu.crud.service;

import com.atguigu.crud.bean.Employee;
import com.atguigu.crud.dao.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author nyh
 * @create 2018-08-19  14:01
 **/
@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    //查询出带部门信息的所有员工信息
    public List<Employee> getAll(){
        return employeeMapper.selectByExampleWithDept(null);
    }
}

接下来使用spring测试模块提供的测试请求功能,测试crud模块的正确性
新建MVCTest测试类

package com.atguigu.crud.test;

import com.atguigu.crud.bean.Employee;
import com.github.pagehelper.PageInfo;
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 java.util.List;

/**
 * 使用spring测试模块提供的测试请求功能,测试crud模块的正确性
 * @author nyh
 * @create 2018-08-19  18:30
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
/*
首先加上spring的配置文件
classpath:applicationContext.xml
因为是测试请求功能,所以还要加上springmvc的配置文件
file:/src/main/webapp/WEB-INF/ispatcherServlet-servlet.xml
*/
@ContextConfiguration(locations = {"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"})

public class MVCTest {

    //上面的@WebAppConfiguration注解让我们可以用@Autowired自动装配springmvc的IOC容器,
    // 不加的话是不可以自动装配IOC容器自身的
    @Autowired
    WebApplicationContext context;

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

    //@Before:每次先调用这个方法
    @Before
    public void initMockMvc(){
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();

    }

    //
    @Test
    public void testPage() throws Exception {
        /**
         * 模拟请求拿到返回值
         * perform:模拟我们发送请求
         *param:请求要带的参数
         * andReturn:拿到返回值
         */

        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "9")).andReturn();

        //请求成功后,请求域中会有pageInfo,我们可以取出pageInfo中的数据进行验证就可以了
        MockHttpServletRequest request = result.getRequest();
        PageInfo pageInfo =(PageInfo) request.getAttribute("pageInfo");
        System.out.println("当前页码:"+pageInfo.getPageNum());
        System.out.println("总页码:"+pageInfo.getPageSize());
        System.out.println("总记录数:"+pageInfo.getTotal());
        System.out.println("在页面需要连续显示的页码:");
        int[] navigatepageNums = pageInfo.getNavigatepageNums();
        for (int navigatepageNum : navigatepageNums) {
            System.out.println("    "+navigatepageNum);
        }

        List<Employee> list = pageInfo.getList();
        for (Employee employee : list) {
            System.out.println("ID:"+employee.getEmpId()+"Name:"+employee.getEmpName());
        }
    }
}

测试结果:
这里写图片描述
可以看到测试通过了,然后就可以放心到页面上显示出来就可以了.

猜你喜欢

转载自blog.csdn.net/qq_36901488/article/details/81812685