基于maven+ssm的增删改查之测试相关ssm环境是否成功
Posted 西西嘛呦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于maven+ssm的增删改查之测试相关ssm环境是否成功相关的知识,希望对你有一定的参考价值。
接上一节。
1、首先我们在com.gong.curd.controller中新建EmployeeController.java(我们使用分页技术)
package com.gong.curd.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.gong.curd.bean.Employee; import com.gong.curd.service.EmployeeService; @Controller public class EmployeeController { @Autowired EmployeeService employeeService; @RequestMapping("/emps") public String getEmps(@RequestParam(value="pn",defaultValue="1") Integer pn, Model model) { PageHelper.startPage(pn,5); List<Employee> emps = employeeService.getAll(); PageInfo<Employee> page = new PageInfo<>(emps,5); model.addAttribute("pageInfo", page); return "list"; } }
2、在com.gong.curd.service新建EmployeeService.java
package com.gong.curd.service; import java.util.List; import com.gong.curd.bean.Employee; public interface EmployeeService { public List<Employee> getAll(); }
3、在com.gong.serviceImpl新建EmployeeServiceImpl.java
package com.gong.curd.serviceImpl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.gong.curd.bean.Employee; import com.gong.curd.dao.EmployeeMapper; import com.gong.curd.service.EmployeeService; @Service public class EmployeeServiceImpl implements EmployeeService { @Autowired EmployeeMapper employeeMapper; public List<Employee> getAll() { return employeeMapper.selectByExampleWithDept(null); } }
4、在com.gong.curd.text新建MvcTest.java用于测试ssm环境:同样的,我们转换成ssm环境进行测试时,需要指定spring和springmvc配置文件。我们在views文件夹下新建一个list.jsp文件,里面暂时按默认就好,不需要添加其他的信息。
package com.gong.curd.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.curd.bean.Employee; import com.github.pagehelper.PageInfo; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations= {"classpath:applicationContext.xml", "file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"}) public class MvcTest { @Autowired WebApplicationContext context; MockMvc mockMvc; @Before public void initMockMvc() { mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void testPage() throws Exception { //模拟请求 MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")) .andReturn(); 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()); } } }
我们模拟发送请求到"/emps",并带上参数pn,运行之后:控制台看到输出
DEBUG [main] - Forwarding to resource [/WEB-INF/views/list.jsp] in InternalResourceView ‘list‘
DEBUG [main] - MockRequestDispatcher: forwarding to [/WEB-INF/views/list.jsp]
DEBUG [main] - Successfully completed request
DEBUG [main] - Returning cached instance of singleton bean ‘sqlSessionFactory‘
当前页码:1
总页码:10
总记录数:49
连续显示页码:
1
2
3
4
5
ID:1==>Name:tom
deptName: 开发部
ID:2==>Name:f4aa02
deptName: 开发部
ID:3==>Name:6fcaa3
deptName: 开发部
ID:4==>Name:4864d4
deptName: 开发部
ID:5==>Name:5a3135
deptName: 开发部
说明我们自己定义的Mybatis语句以及ssm环境是成功的。下一节将结合分页技术,利用bootstrap实现员工信息在前端进行显示。
以上是关于基于maven+ssm的增删改查之测试相关ssm环境是否成功的主要内容,如果未能解决你的问题,请参考以下文章
基于maven+ssm的增删改查之使用mybatis逆向工程生成相关文件