添加spring-test模块
<!-- spring-test --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.7.RELEASE</version> </dependency>
测试mapper接口是否成功
package com.dotcore.test; import com.dotcore.dao.DepartmentMapper; import com.dotcore.dao.EmployeeMapper; import com.dotcore.model.Department; import com.dotcore.model.Employee; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.UUID; /** * 测试dao层的工作 * * @author xuerr * @version 2017/5/3 * 推荐Spring的项目可以使用Spring的单元测试,可以自动注入我们需要的组件 * 1、导入SpringTest模块 * 2、@ContextConfiguration指定Spring配置的位置 * 3、直接autowire要使用的组件 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) public class MapperTest { @Autowired DepartmentMapper departmentMapper; @Autowired EmployeeMapper employeeMapper; @Autowired SqlSession sqlSession; /** * 测试DepartmetnMapper */ @Test public void testCRUD() { //方法一: //1.创建SpringIOC容器 // ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); //2、从容器中获取mapper // DepartmentMapper bean = ioc.getBean(DepartmentMapper.class); //方法二: // System.out.println(departmentMapper); //1、插入几个部门 // departmentMapper.insertSelective(new Department(null,"开发部")); // departmentMapper.insertSelective(new Department(null,"测试部")); //2、插入员工 // employeeMapper.insertSelective(new Employee(null,"jack","M","11@qq.com",5)); //这种批量插入速度就很慢了 // for(int i = 0; i < 1000; i++){ // employeeMapper.insertSelective(); // } //批量插入 EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); for (int i = 0; i < 1000; i++) { String UID = UUID.randomUUID().toString().substring(0, 5) + i; mapper.insertSelective(new Employee(null, UID,"M",UID+"@qq.com",5)); } } }
批量插入的时候我们得配置
<!--配置一个可以执行批量的sqlSession--> <bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSession"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> <constructor-arg name="executorType" value="BATCH"/> </bean>
=================================================================================================================================================
分页查询
1、引用一个分页插件PageHelper
https://github.com/pagehelper/Mybatis-PageHelper 看里面有个中文文档,引用完jar,点击 如何使用分页插件
首先你得添加jar包
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.1</version> </dependency>
1. 在 MyBatis 配置 xml 中配置拦截器插件
<!-- plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下: properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers? --> <plugins> <!-- com.github.pagehelper为PageHelper类所在包名 --> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 --> <!--<property name="param1" value="value1"/> --> <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--> <property name="dialect" value="mysql"/> </plugin> </plugins>
在控制器中对分页插件的使用
package com.dotcore.controller; import com.dotcore.model.Employee; import com.dotcore.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 xuerr * @version 2017/5/4 */ @Controller public class EmployeeController { @Autowired EmployeeService employeeService; /** * 查询员工数据(分页查询) * @return */ // @RequestMapping("/emps") // public String getEmps(){ // //这不是一个分页查询 // List<Employee> emps = employeeService.getAll(); // return "list"; // } @RequestMapping("/emps") public String getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn, Model model){ //引入PageHelper分页插件 //在查询之前只需要调用,传入页码,以及每页大小 PageHelper.startPage(pn,5); List<Employee> emps = employeeService.getAll(); //使用PageInfo包装查询的数据,只需要把PageInfo交给页面就好了 //封装详细的分页信息,包括我们查询出来的数据 PageInfo pageInfo = new PageInfo(emps,5); model.addAttribute("pageInfo",pageInfo); return "list"; } }
现在来个单元测试,测试成功后,就开始写页面
=================================================================================================================================================
单元测试(记住mysql的服务要开,我前面忘记了)
package com.dotcore.test; import com.dotcore.model.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; /** * @author xuerr * @version 2017/5/4 * 这里spring的配置文件是不够的,这里还要加一个springmvc的配置文件file。。。。 *使用Spring测试模块提供的测试请求功能,测试crud请求的正确性 * Spring4测试的时候,需要servlet3.0的支持 * */ //servlet版本不够,所以我们换高点上去 // <dependency> // <groupId>javax.servlet</groupId> // <artifactId>servlet-api</artifactId> // <!--服务器里有,发布到服务器上不需要这个包 --> // <scope>provided</scope> // <version>2.5</version> // </dependency> @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations = {"classpath:applicationContext.xml","file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml"}) public class MvcTest { //虚拟MVC请求,获取到处理结果 MockMvc mockMvc; //@WebAppConfiguration这个引用后,才能拿到context @Autowired WebApplicationContext context; //用junit的before @Before public void initMokcMvc(){ mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); } @Test public void testPage() throws Exception { //模拟请求,拿到返回值 MvcResult result = mockMvc.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()); } } }