Back to Repositories

Testing PageHelper Implementation with MyBatis Mapper in SpringAll

This test suite demonstrates the integration of Spring Boot with MyBatis Mapper and PageHelper for pagination functionality. It validates core user management operations and pagination capabilities in a Spring Boot application.

Test Coverage Overview

The test suite covers essential user management operations including pagination, data retrieval, and filtering functionality.

  • Pagination testing with PageHelper integration
  • User data retrieval with example-based filtering
  • CRUD operations validation (commented code)
  • Order by clause implementation testing

Implementation Analysis

The testing approach utilizes Spring Boot Test framework with JUnit4 integration. The implementation showcases the use of PageHelper for pagination control and MyBatis Mapper for database operations.

Key patterns include Example-based criteria building, service layer abstraction, and PageInfo wrapper utilization for result set management.

Technical Details

  • SpringJUnit4ClassRunner for test execution
  • @SpringBootTest for application context loading
  • PageHelper for pagination management
  • MyBatis Mapper for ORM operations
  • Example class for dynamic query building

Best Practices Demonstrated

The test implementation follows Spring Boot testing best practices with proper dependency injection and test configuration.

  • Clear separation of concerns between service and test layers
  • Proper use of Spring Boot test annotations
  • Structured pagination implementation
  • Comprehensive CRUD operation testing setup

wuyouzhuguli/springall

27.Spring-Boot-Mapper-PageHelper/src/main/java/com/springboot/ApplicationTest.java

            
package com.springboot;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.bean.User;
import com.springboot.service.UserService;

import tk.mybatis.mapper.entity.Example;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTest {

	@Autowired
	private UserService userService;

	@Test
	public void test() throws Exception {

		// User user = new User();
		// user.setId(userService.getSequence("seq_user"));
		// user.setUsername("scott");
		// user.setPasswd("ac089b11709f9b9e9980e7c497268dfa");
		// user.setCreateTime(new Date());
		// user.setStatus("0");
		// this.userService.save(user);

		
//		Example example = new Example(User.class);
//		example.createCriteria().andCondition("username like '%i%'");
//		example.setOrderByClause("id desc");
//		List<User> userList = this.userService.selectByExample(example);
//		for (User u : userList) {
//			System.out.println(u.getUsername());
//		}
//		
//		List<User> all = this.userService.selectAll();
//		for (User u : all) {
//			System.out.println(u.getUsername());
//		}
//		
//		User user = new User();
//		user.setId(1l);
//		user = this.userService.selectByKey(user);
//		System.out.println(user.getUsername());
//
//		user.setId(4l);
//		this.userService.delete(user);

		PageHelper.startPage(2, 2);
		List<User> list = userService.selectAll();
		PageInfo<User> pageInfo = new PageInfo<User>(list);
		List<User> result = pageInfo.getList();
		for (User u : result) {
			System.out.println(u.getUsername());
		}
	}
}