zoukankan      html  css  js  c++  java
  • 分页工具 Pagehelper的学习 (Spring+Mybatis)

    使用pageHelper 非常简单,只需要:

    1.在MyBatis中配置MyBatis的拦截器插件

    2.配置数据库的方言,来确定数据库

    3.设置分页也只需要调用类的静态方法:
    PageHelper.startPage(1, 30);

    /*

    MyBatis的配置文件

    SqlMapConfig.xml

    */

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <!-- 配置分页插件pageHelper 支持多种数据库 -->
    <plugins>
    <!-- 分页拦截器(MyBatis提供的拦截器) -->
    <plugin interceptor="com.github.pagehelper.PageHelper">
    <!--配置数据库方言 -->
    <property name="dialect" value="mysql" />
    </plugin>
    </plugins>
    </configuration>

    /*

    测试

    */

    public class PageHelperTest {
    @Test
    public void page() {
    // 1.获取mapper对象
    @SuppressWarnings("resource")
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
    "classpath:spring/applicationContext-*.xml");
    TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
    // 2.设置分页 PageHelper这个类只对最近的查询语句有效,第二个就无效了
    PageHelper.startPage(1, 30);
    // 3.执行查询
    TbItemExample example = new TbItemExample();
    List<TbItem> list = itemMapper.selectByExample(example);
    // 4.取得分页效果
    for (TbItem tbItem : list) {
    System.out.println(tbItem);
    }

    //pageInfo 包含了分页的一些信息:包括总记录数、总页数、当前页数、分页大小等
    PageInfo<TbItem> pageInfo = new PageInfo<>(list);
    int pages = pageInfo.getPages();
    System.out.println("pages:" + pages);
    int pageSize = pageInfo.getPageSize();
    System.out.println("pageSizes:" + pageSize);
    int size = pageInfo.getSize();
    System.out.println("size:" + size);
    long total = pageInfo.getTotal();
    System.out.println("total:" + total);
    }
    }

    结果如下:

    TbItem [id=858025, title=三星 I8552 白色 联通3G手机 双卡双待, sellPoint=经济实惠机器~~开春入手好时机~, price=79900, num=99999, barcode=null, image=http://image.taotao.com/jd/d958a21cec814fdeab934d43b4fb2e06.jpg, cid=560, status=1, created=Sun Mar 08 21:27:49 CST 2015, updated=Sun Mar 08 21:27:49 CST 2015]
    TbItem [id=860275, title=长虹(CHANGHONG) 3D51C1080i 51英寸 快门式3D智能Android 电视(黑色), sellPoint=智能安卓系统 可自由安装应用程序 <a target="blank" href="http://sale.jd.com/act/Kt0aHzbU7uR1M.html">“点击进入长虹新年专场”</a>, price=269900, num=99999, barcode=null, image=http://image.taotao.com/jd/08dabc37342943ffb717632f9ee40685.jpg, cid=76, status=1, created=Sun Mar 08 21:27:35 CST 2015, updated=Sun Mar 08 21:27:35 CST 2015]

    pages:104
    pageSizes:30
    size:30
    total:3096

  • 相关阅读:
    senlin __init__() got an unexpected keyword argument 'additional_headers'
    Delphi 全局画点TCanvas.Pixels[X,Y]
    sql server 列修改null 变成not null
    Delphi记录record中的变体
    delphi无边框可拖动窗体
    bootstrap相关资料
    重装windows导致grub损坏
    RabbitMQ的安装(Docker安装)
    RabbitMQ的安装以及使用(Windows环境)
    如果安装rabittmq后,输入http://localhost:15672不出页面的
  • 原文地址:https://www.cnblogs.com/Loadhao/p/6696866.html
Copyright © 2011-2022 走看看