程序跳转描述: 访问index.jsp界面,页面发送查询员工列表请求,完成查询并显示到listemp.jsp界面。
- 编写index界面,直接跳转到前端控制器:
<jsp:forward page="/emps"></jsp:forward>
- 编写控制器对应的代码:
package com.zr.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.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zr.crud.bean.Employee;
import com.zr.crud.service.EmployeeService;
/**
* 处理员工请求
* @author asus
*
*/
@Controller
public class EmployeeController {
@Autowired
EmployeeService employeeService;
/**
* 查询员工的数据(分页查询)
* @return
*/
@RequestMapping("/emps")
public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
System.out.println("开始分页查询");
//引入PageHelper分页查询插件,参数为页码与每页数量
PageHelper.startPage(pn, 3);
//startPage后面紧跟的查询就是一个分页查询
List< Employee> emps = employeeService.getAll();
//使用pageInfo包装查询出来的数据,第二个参数为连续显示的页数
PageInfo<Employee> page = new PageInfo<Employee>(emps,2);
//将pageInfo交给界面
model.addAttribute("pageInfo",page);
System.out.println("获取数据");
return "listemp";
}
}
返回的页面对应listemp.jsp,这里之前在springmvc的配置文件dispatcherServlet-servlet.xml中写了对应的前缀和后缀:
<!-- 配置视图解析器,方便页面返回 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
这里使用到了PageHelper的插件,在pom.xml中导入对应的依赖:
<!-- 导入PageHelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
导入完成后,加入了pagehelper和jsqlparser两个包。
根据PageHelper的说明文档,这里需要在mybatis的配置文件mybatis-config.xml中添加插件信息:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
- 编写对应服务层,使用dao层的Mapper类操作数据库:
package com.zr.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zr.crud.bean.Employee;
import com.zr.crud.dao.EmployeeMapper;
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
/**
* 查询所有的员工
* @return
*/
public List<Employee> getAll() {
return employeeMapper.selectByExample(null);
}
}
- 通过Spring测试对应的数据是否能够取到:
package com.zr.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.github.pagehelper.PageInfo;
import com.zr.crud.bean.Employee;
/**
* 使用spring测试模块测试请求功能
* Spring4测试的时候,需要servlet3.0支持
* @author asus
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:applicationContext.xml",
"file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml" })
public class MvcTest {
// 传入springMvc的Ioc
// 这里需要@WebAppConfiguration注解拿到ioc
@Autowired
WebApplicationContext context;
// 虚拟的mvc请求
MockMvc mocMvc;
@Before
public void initMockMvc() {
mocMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void testPage() throws Exception {
// 模拟请求,拿到返回值
MvcResult result = mocMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).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());
}
}
}
这里使用Junit测试发现出现ClassNotFound的错误,发现是servlet的错误,这个时候更改servlet版本,pom中替换为:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<!-- 这个包服务器里面已经有了,会报错 -->
<scope>provided</scope>
</dependency>
重新测试,能够在控制台看到输出数据,测试完成。