分页插件PageHelper+spring单元测试

spring单元测试---注解法

1、junit单元测试包

	
            junit
            junit
            4.12
            test
        

2、spring测试包

	
            org.springframework
            spring-test
            4.1.3.RELEASE
        
3、测试类

	@RunWith(SpringJUnit4ClassRunner.class)
	@ContextConfiguration({"classpath:spring/applicationContext-*.xml"})
	public class TestPageHelper {...}

spring单元测试---手动装载

1、junit测试包

	
            junit
            junit
            4.12
            test
        
2、测试类
@Test
    public void testPageHelper1(){
    //获得mapper代理对象
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
    TbItemMapper itemMapper = context.getBean(TbItemMapper.class); ...}

PageHelper分页

1、分页jar包

 	
            com.github.pagehelper
            pagehelper
            4.0.2
        

2、mybatis配置(SqlMapConfig.xml)

    
    
        
            
            
        
    


3、java代码

	@Autowired
    private TbItemMapper tbItemMapper;
    //注解加载配置问价
    @Test
    public void testPageHelper(){
        //设置分页
        PageHelper.startPage(1,30);
        //执行查询
        TbItemExample example = new TbItemExample();
        List list = tbItemMapper.selectByExample(example);
        //取分页结果
        PageInfo pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        System.out.println("total:"+total);
        int pages = pageInfo.getPages();
        System.out.println("pages:"+pages);
        int pageSize = pageInfo.getPageSize();
        System.out.println("pageSize:"+pageSize);
    }
OK!

你可能感兴趣的:(java,study)