引入pageHelper

来源:互联网 发布:全国省市区sqlserver 编辑:程序博客网 时间:2024/05/18 00:09

pageHelper4 

* 支持条件查询

* 支持条件分页排序


一、使用方法

第一步:引入pageHelper的jar包。 pagehelper-4.1.1.jar

              依赖jar  jsqlparser-0.9.4.jar

第二步:需要在SqlMapConfig.xml中配置插件。

注意:pageHelper4 mybatis版本需要至少3.3 测试版本3.3

<configuration><!-- 别名 --><typeAliases><package name="com.xyj.ssm.po"/></typeAliases><!-- 配置分页插件 --><plugins><plugin interceptor="com.github.pagehelper.PageHelper"><!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->                <property name="dialect" value="mysql"/></plugin></plugins><!-- mapper 使用spring扫描 所以不需要配置 但是需要遵循规范 --></configuration>

特别注意:在Mybatis配置xml中配置拦截器插件:

<!--     plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:    properties?, settings?,     typeAliases?, typeHandlers?,     objectFactory?,objectWrapperFactory?,     plugins?,     environments?, databaseIdProvider?, mappers?-->

第三步:在查询的sql语句执行之前,添加一行代码:

PageHelper.startPage(1, 10);

第一个参数是page,要显示第几页。

第二个参数是rows,每页显示的记录数。

第四步:取查询结果的总数量。

创建一个PageInfo类的对象,从对象中取分页信息。


          public static void main(String[] args) {ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");// 使用mapper查询数据库ShopcouponcountMapper mapper = app.getBean(ShopcouponcountMapper.class);ShopcouponcountExample example = new ShopcouponcountExample();Criteria criteria = example.createCriteria();criteria.andUseridEqualTo(6);// 设置分页PageHelper.startPage(1, 10 , "scId desc");List<Shopcouponcount> list = mapper.selectByExample(example);for (Shopcouponcount shopcouponcount : list) {System.out.println(shopcouponcount.getCount());}PageInfo<Shopcouponcount> pageInfo = new PageInfo<Shopcouponcount>(list);System.out.println("共有数据" + pageInfo.getTotal() );}

注意:

PageHelper.startPage(1, 10);   页号必须从1 开始,否则取不到数据