mybatis pageHelper的使用

项目地址:https://github.com/pagehelper/Mybatis-PageHelper
1. 下载Jar包或引入依赖

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

2. 在mybatis配置文件中配置

<configuration>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialet" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

3. 测试分页插件

public class TestPageHelper {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext*.xml");
    @Test
    public void testPageHelper(){
        TbItemMapper mapper = applicationContext.getBean(TbItemMapper.class);
        TbItemExample example = new TbItemExample();
        
        //在执行sql语句前设置查询的页和记录数
        PageHelper.startPage(1,10);
        
        //根据查询条件查询商品
        List<TbItem> itemList = mapper.selectByExample(example);
        for (TbItem item:itemList){
            System.out.println(item.getTitle());
        }
        
        //获取分页信息
        PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(itemList);
        long count  = pageInfo.getTotal();
        
        System.out.println("共有商品"+count+"件");
        /*分页插件的其他属性和方法还有很多哦*/
    }
}

4. 注意
pageHelper对逆向工程生成的代码支持不好,不能对有查询条件的查询分页,也就是说selectByExample(example),该example如果有内容,那么分页插件就失效了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,285评论 25 708
  • 台阶测验是评价心肺功能适应水平的方法之一。它的优越性在于:可以在室内进行,能适合不同程度身体条件的人,且不需要昂贵...
    勤行乐道阅读 1,771评论 0 1
  • 作为地球上最智慧的大自然的物种――人来说,敢“说实话”估计会被冠以各种好听的高尚的称谓和赞美,因为我们每个人...
    燕倚东风一缕寒阅读 506评论 0 0
  • 6:20闹钟准时叫,起床,刷牙,洗脸,昏昏沉沉背着书包出门,楼道里安静得只能听到睡觉的声音。连阿姨都看不到,踩着水...
    又又鱼阅读 196评论 0 0