分页插件PageHelper

缺乏、安全感 2022-06-06 09:48 690阅读 0赞

1.1.1.1 分页插件PageHelper

1.1.1.1.1 官方网站:

https://github.com/pagehelper/Mybatis-PageHelper/tree/master/src/main/java/com/github/pagehelper

Center

1.1.1.1.2 实现原理

Center 1

Center 2

1.1.1.1.3 使用方法

第一步:引入pageHelper的jar包。

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

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!-- 配置分页插件 -->
  7. <plugins>
  8. <plugin interceptor="com.github.pagehelper.PageHelper">
  9. <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
  10. <property name="dialect" value="mysql"/>
  11. </plugin>
  12. </plugins>
  13. </configuration>

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

PageHelper.startPage(1, 10);

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

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

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

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

1.1.1.1.4 分页测试
  1. public class TestPageHelper {
  2. @Test
  3. public void testPageHelper() {
  4. //创建一个spring容器
  5. ApplicationContext applicationContext =new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
  6. //从spring容器中获得Mapper的代理对象
  7. TbItemMapper mapper = applicationContext.getBean(TbItemMapper.class);
  8. //执行查询,并分页
  9. TbItemExample example = new TbItemExample();
  10. //分页处理
  11. PageHelper.startPage(2, 10);
  12. List<TbItem> list = mapper.selectByExample(example);
  13. //取商品列表
  14. for (TbItem tbItem : list) {
  15. System.out.println(tbItem.getTitle());
  16. }
  17. //取分页信息
  18. PageInfo<TbItem> pageInfo = new PageInfo<>(list);
  19. long total =pageInfo.getTotal();
  20. System.out.println("共有商品:"+total);
  21. }
  22. }

注意:分页插件对逆向工程生成的代码支持不好,不能对有查询条件的查询分页。会抛异常。

使用我修改过的版本就可以了。

Center 3

Dao可以实现逆向工程生成的mapper文件+PageHelper实现。

1.1.1 Service层

接收分页参数,一个是page一个是rows。调用dao查询商品列表。并分页。返回商品列表。

返回一个EasyUIDateGrid支持的数据格式。需要创建一个Pojo。此pojo应该放到taotao-common工程中。

  1. public class EUDataGridResult {
  2. private long total;
  3. private List<?> rows;
  4. public long getTotal() {
  5. return total;
  6. }
  7. public void setTotal(long total) {
  8. this.total =total;
  9. }
  10. public List<?> getRows() {
  11. return rows;
  12. }
  13. public void setRows(List<?>rows) {
  14. this.rows =rows;
  15. }
  16. }






代码实现







  1. /*
  2. 商品列表查询
    <p>Title: getItemList</p>
  3. <p>Description:</p>
    @param page
  4. @param rows
    @return
  5. @see com.taotao.service.ItemService#getItemList(long, long)
    */
    @Override
    public EUDataGridResult getItemList(int page,int rows) {
    //查询商品列表
    TbItemExample example = new TbItemExample();
    //分页处理
    PageHelper.startPage(page, rows);
    List<TbItem> list = itemMapper.selectByExample(example);
    //创建一个返回值对象
    EUDataGridResult result = new EUDataGridResult();
    result.setRows(list);
    //取记录总条数
    PageInfo<TbItem> pageInfo = new PageInfo<>(list);
    result.setTotal(pageInfo.getTotal());
    return result;
    }

1.1.2 Controller

接收页面传递过来的参数page、rows。返回json格式的数据。EUDataGridResult

需要使用到@ResponseBody注解。

  1. @RequestMapping("/item/list")
  2. @ResponseBody
  3. public EUDataGridResult getItemList(Integer page, Integer rows) {
  4. EUDataGridResult result = itemService.getItemList(page, rows);
  5. return result;
  6. }

发表评论

表情:
评论列表 (有 0 条评论,690人围观)

还没有评论,来说两句吧...

相关阅读

    相关 【mybatis】PageHelper

        最近有个小项目,不和旧系统有关系,所以简单搭建了一个SSM框架。在项目进行中,遇到需要分页显示数据的需求,记得之前接触mybatis框架用的就是插件,很方便,所以这次