Java web项目创建笔记10 之《Mybatis分页查询(2)》

该博客主要介绍了分页功能的测试步骤,包括建立测试VO类、修改Mapper文件、插入测试数据等,若能生成分页sql则测试成功,还提供了例子程序webapp2_3.rar及下载链接。

二、测试分页功能

1、建立测试VO类TeacherVo.java

package com.study.base.vo;

import com.study.base.mybatis.page.Pageable;

public class TeacherVo extends Pageable {
	int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
}

2、修改TeacherMapper.java

package com.study.base.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.study.base.mybatis.annotation.MyBatis;
import com.study.base.vo.TeacherVo;
import com.study.testBean.Teacher;

@MyBatis
public interface TeacherMapper {
	/**
	 * 根据id
	 */
	public Teacher getTeacherById(@Param("id") int id);
	/**
	 * 分页查询测试方法
	 * @param teacherVo
	 * @return
	 */
	public List<Teacher> getTeacherPageable(TeacherVo teacherVo);
}

3、修改TeacherMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.study.base.dao.TeacherMapper">
	<resultMap id="TeacherInf" type="com.study.testBean.Teacher">
		<result column="ID" property="id" />
		<result column="NAME" property="name" />
	</resultMap>

	<sql id="Teacher_Column_List">
		ID, NAME
	</sql>

	<select id="getTeacherById" resultMap="TeacherInf" parameterType="int">
		select
		<include refid="Teacher_Column_List" />
		from tbl_teacher_inf
		<where>
			ID = #{id}
		</where>
	</select>
	
	<select id="getTeacherPageable"  resultMap="TeacherInf" parameterType="com.study.base.vo.TeacherVo">
	select
	<include refid="Teacher_Column_List" />
		from tbl_teacher_inf
		<where>
			ID > #{id}
		</where>
	</select>
</mapper>

4、插入测试数据

insert into tbl_teacher_inf values (4, '马超');
insert into tbl_teacher_inf values (5, '黄忠');
insert into tbl_teacher_inf values (6, '赵云');
insert into tbl_teacher_inf values (7, '魏延');
insert into tbl_teacher_inf values (8, '关平');
insert into tbl_teacher_inf values (9, '周仓');
insert into tbl_teacher_inf values (10, '关兴');
insert into tbl_teacher_inf values (11, '张苞');
insert into tbl_teacher_inf values (12, '陈到');
insert into tbl_teacher_inf values (13, '李严');
insert into tbl_teacher_inf values (14, '姜维');
insert into tbl_teacher_inf values (15, '廖化');
insert into tbl_teacher_inf values (16, '马谡');
insert into tbl_teacher_inf values (17, '马岱');
insert into tbl_teacher_inf values (18, '陈式');
insert into tbl_teacher_inf values (19, '雷铜');
insert into tbl_teacher_inf values (20, '吴兰');

5、修改MyTest.java

package webapp;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.study.base.dao.TeacherMapper;
import com.study.base.vo.TeacherVo;
import com.study.testBean.Student;
import com.study.testBean.Teacher;
import com.study.util.ApplicationContextHolder;
import com.study.util.PropertiesUtil;
import com.study.util.ReflectInvokeUtil;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml","classpath:spring-mvc.xml"})
@ActiveProfiles("dev")
public class MyTest {

	@Value("${flag}")
	String flag;
	
	@Autowired
	Student stu;
	
	@Autowired
	TeacherMapper teacherMapper;
	
	@Test
	public void testInvoke() throws Exception {
		System.out.println("第一个测试方法*******");
		ApplicationContext ac = ApplicationContextHolder.getApplicationContext();
		ReflectInvokeUtil.invokeMethod("com.study.testBean.Student", "sayHello", null);
	}
	
	@Test
	public void testById() {
		System.out.println("第二个测试方法*******");
		ApplicationContext ac = ApplicationContextHolder.getApplicationContext();
		Student stu = (Student)ApplicationContextHolder.getBean("id.student1");
		stu.sayHello();
		System.out.println("**********");
	}
	
	@Test
	public void testProperties() {
		System.out.println("第三个测试方法*******");
		System.out.println("flag is:" + flag);
		System.out.println("appName is:" + PropertiesUtil.getProperties("appName"));
	}
	
	@Test
	public void testAutowired() {
		System.out.println("第四个测试方法*******");
		stu.sayHello();
		System.out.println("**********");
	}
	
	@Test
	public void testMybatis() {
		System.out.println("第五个测试方法*******");
		Teacher teacher = teacherMapper.getTeacherById(1);
		System.out.println("id is: " + teacher.getId());
		System.out.println("name is: " + teacher.getName());
	}
	
	@Test
	public void testPageable() {
		System.out.println("第六个测试方法*******");
		TeacherVo teacherVo = new TeacherVo();
		teacherVo.setId(2);
		teacherVo.setPageNum(5);
		teacherVo.setPageSize(4);
		List<Teacher> list = teacherMapper.getTeacherPageable(teacherVo);
		System.out.println("list size is:" + list.size());
	}
}

6、执行testPageable方法
生成分页sql,则测试成功

22:52:35.699 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test method: context [DefaultTestContext@5442a311 testClass = MyTest, testInstance = webapp.MyTest@2ea6e30c, testMethod = testPageable@MyTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@548e7350 testClass = MyTest, locations = '{classpath:applicationContext.xml, classpath:spring-mvc.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{dev}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null].
第六个测试方法*******
22:52:35.746 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
22:52:35.755 [main] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@687ef2e0] was not registered for synchronization because synchronization is not active
22:52:36.078 [main] DEBUG com.study.base.mybatis.page.PaginationInterceptor - 生成分页SQL : select
	 
		ID, NAME
	 
		from tbl_teacher_inf
		 WHERE ID > ?
22:52:36.090 [main] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
22:52:36.114 [main] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [418513504, URL=jdbc:mysql://localhost:3306/webapp2?serverTimezone=UTC, UserName=user2@localhost, MySQL Connector/J] will not be managed by Spring
22:52:36.128 [main] DEBUG com.study.base.dao.TeacherMapper.getTeacherPageable - ==>  Preparing: select ID, NAME from tbl_teacher_inf WHERE ID > ? limit 16 , 4 
22:52:36.146 [main] DEBUG com.study.base.dao.TeacherMapper.getTeacherPageable - ==> Parameters: 2(Integer)
22:52:36.166 [main] DEBUG com.study.base.dao.TeacherMapper.getTeacherPageable - <==      Total: 2
22:52:36.170 [main] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - Committing JDBC Connection [418513504, URL=jdbc:mysql://localhost:3306/webapp2?serverTimezone=UTC, UserName=user2@localhost, MySQL Connector/J]
22:52:36.172 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@687ef2e0]
list size is:2
22:52:36.175 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - After test method: context [DefaultTestContext@5442a311 testClass = MyTest, testInstance = webapp.MyTest@2ea6e30c, testMethod = testPageable@MyTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@548e7350 testClass = MyTest, locations = '{classpath:applicationContext.xml, classpath:spring-mvc.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{dev}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null], method annotated with @DirtiesContext [false] with mode [null].
22:52:36.175 [main] DEBUG org.springframework.test.context.web.ServletTestExecutionListener - Resetting RequestContextHolder for test context [DefaultTestContext@5442a311 testClass = MyTest, testInstance = webapp.MyTest@2ea6e30c, testMethod = testPageable@MyTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@548e7350 testClass = MyTest, locations = '{classpath:applicationContext.xml, classpath:spring-mvc.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{dev}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]].
22:52:36.178 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - After test class: context [DefaultTestContext@5442a311 testClass = MyTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@548e7350 testClass = MyTest, locations = '{classpath:applicationContext.xml, classpath:spring-mvc.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{dev}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null].

7、例子程序webapp2_3.rar
链接: https://pan.baidu.com/s/1IMHuxHbhMS1yh7MjT5M4rw 提取码: eyqh

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值