实现发送请求获取表中相关数据(更新中:第八天)

在这里插入图片描述

1、controller层(Sbasicmessage.java)

package com.fhx.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 org.springframework.web.servlet.ModelAndView;
import com.fhx.service.Sbasic;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class Sbasicmessage {
	@Autowired
	Sbasic sbasic;

	@RequestMapping("Sbasicmessage")
	public void sbage(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
		// PageHelper.startPage(显示第几页, 这个页面显示多少条数据);
		PageHelper.startPage(pn, 5);
		List<Sbasicmessage> list = sbasic.sb();
		// PageInfo(查询的结果集,有多少个页面切换按钮);
		PageInfo page = new PageInfo(list, 3);
		// addAttribute添加参数PageInfo
		model.addAttribute("PageInfo", page);

	}
}

2、service层(Sbasic.java)

package com.fhx.service;

import java.util.List;

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

import com.fhx.dao.SbasicmessageMapper;
import com.fhx.pojo.Sbasicmessage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

//service
@Service
public class Sbasic {
	@Autowired
	SbasicmessageMapper sm;
	public List sb(){
		
		//使用接口中的查询方法
		List list = sm.selectByExample(null);
		
		return list;
	}
}

3、Test层(MockMvcTest.java)

package com.fhx.test;


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.github.pagehelper.PageInfo;

//让测试运行于Spring测试环境
@RunWith(SpringJUnit4ClassRunner.class)
// 代表生成一个Web摸拟的容器
@WebAppConfiguration
//Spring整合JUnit4测试时,使用注解引入多个配置文件{spring.xml文件路径, springmvc配置文件路径(因为springmvc配置文件在别的文件夹下所以用绝对路径)}
@ContextConfiguration(locations = { "classpath:spring.xml", "file:src/main/webapp/WEB-INF/springmvc-servlet.xml" })
public class MockMvcTest {
	//
	@Autowired
	WebApplicationContext context;
	//虚拟MVC请求,MockMvc是一个类,这个类专门做摸拟请求
	MockMvc mvc;
	
	//junit
	@Before
	public void mock(){
		//初始化MVC
		mvc=MockMvcBuilders.webAppContextSetup(context).build();
	}
	
	@Test
	public void testPage() throws Exception{
	
	//使用虚拟MVC来获取到请求路径和参数,
	MvcResult result =	mvc.perform(MockMvcRequestBuilders.get("/Sbasicmessage").param("pn", "1")).andReturn();
	//MockHttpServletRequest虚拟请求
	MockHttpServletRequest mock = result.getRequest();
	//使用mock获取参数(getAttribute),返回给PageInfo。(PageInfo)分页
	PageInfo pageinfo =  (PageInfo) mock.getAttribute("PageInfo") ;

	System.out.println("当前页:"+pageinfo.getPageNum());
	System.out.println("每页的数量:"+pageinfo.getPageSize());
	System.out.println("当前页的数量:"+pageinfo.getSize());
	System.out.println("总页数:"+pageinfo.getPages());
	}

}

猜你喜欢

转载自blog.csdn.net/qq_43296599/article/details/85271980