分页插件PageHelper+spring单元测试

  • Post author:
  • Post category:其他



spring单元测试—注解法


1、junit单元测试包

	<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>



2、spring测试包

	<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>


3、测试类


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


spring单元测试—手动装载

1、junit测试包

	<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>


2、测试类

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


PageHelper分页


1、分页jar包

 	<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.0.2</version>
        </dependency>


2、mybatis配置(SqlMapConfig.xml)

    <!--配置分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!--指定数据库方言-->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>


3、java代码




	@Autowired
    private TbItemMapper tbItemMapper;
    //注解加载配置问价
    @Test
    public void testPageHelper(){
        //设置分页
        PageHelper.startPage(1,30);
        //执行查询
        TbItemExample example = new TbItemExample();
        List<TbItem> list = tbItemMapper.selectByExample(example);
        //取分页结果
        PageInfo<TbItem> 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!



版权声明:本文为xuzailin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。