分页插件PageHelper+spring单元测试

来源:互联网 发布:算法工程师面试题 编辑:程序博客网 时间:2024/06/03 10:59

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!

原创粉丝点击