mybatis-pageHelper分页插件

分页插件pagerHelper

该插件支持Oracle、mySql、MariaDB、SQlite、Sql server、hsqldb等6种数据库。

<pagehelper.version>3.4.2</pagehelpder.version>

使用步骤

1、把pageHelper maven工程导入到workspace中。run as》install
2、在service工程中的mybatis》sqlmapConfig.xml中添加配置,添加拦截,拦截pagehelper工程的com.github.pagehelper.PageHelper类。

<configuration>
    <!-- 配置分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 配置数据库的方言 -->
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

测试

1、在mybatis的配置文件中配置分页插件
2、在执行查询之前配置分页条件,使用PageHelper的静态方法

PageHelper.startPage(1, 10);

3、执行查询

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
//创建Example对象
TbItemExample example = new TbItemExample();
//Criteria criteria = example.createCriteria();//查询条件
List<TbItem> list = itemMapper.selectByExample(example);

4、取分页信息,使用PageInfo对象取

PageInfo<TbItem> pageInfo = new PageInfo<>(list);
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("总记页数:" + pageInfo.getPages());
System.out.println("返回的记录数:" + list.size());
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容