taotao-Mybatis分页插件(商品列表展示)

2018-07-26  本文已影响68人  小小蒜头

分页插件的原理:

原理

使用方法:

插件名称:PageHelper,该插件目前支持Oracle、MySQL、MariaDB、SQLite、Hsqldb、 PostgreSQL六种数据库分页。

第一步:在Mybatis的配置文件比如SqlMapConfig.xml中配置一个plugin

添加相关jar包

<pagehelper.version>3.4.2</pagehelper.version>
<dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>${pagehelper.version}</version>
</dependency>

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>
    <!--配置分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!--指定使用的数据库-->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

第二步:在sql语句执行之前,添加一个PageHelper.startPage(page,rows);

第三步:取分页结果。创建一个PageInfo对象,参数就是查询结果返回的list,从PageInfo对象中取分页结果

1、展示商品列表

1.1 页面分析

Ajax请求

请求的参数:'http://localhost:8080/item/list?page=1&rows=30'

响应的数据:json数据

包含total、rows两个属性:Total,查询结果的总记录数;Rows,是集合,包含显示的所有数据,其中集合中每个元素的key应该和dategrid的field对应。

Easyui中datagrid控件要求的数据格式为:{total:"2",rows:[{"id":"1","name":"张三"},{"id":"2","name":"李四"}]}

1.2 DAO层

之前逆向工程时已经自动生成

1.3 Service层

参数:page(显示的页码)、rows(每页显示的记录数)
返回值:创建一个pojo表示返回值。应该包含total、rows两个属性。
应该放到taotao-common工程中,和其他系统共用。

package com.taotao.common.pojo;

import java.util.List;

/**
 * Created by yvettee on 2018/7/26.
 */
public class EasyUIDataGridResult {
    private long total;
    private List<?> rows;

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<?> getRows() {
        return rows;
    }

    public void setRows(List<?> rows) {
        this.rows = rows;
    }
}

ItemService

package com.taotao.service;

import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.pojo.TbItem;

/**
 * Created by yvettee on 2018/7/24.
 */
public interface ItemService {
    TbItem getItemById(Long itemId);
    EasyUIDataGridResult getItemList(int page, int rows);
}

ItemServiceImpl

@Override
    public EasyUIDataGridResult getItemList(int page, int rows) {
        //分页处理
        PageHelper.startPage(page, rows);
        //执行查询
        TbItemExample tbItemExample = new TbItemExample();
        List<TbItem> list = this.tbItemMapper.selectByExample(tbItemExample);
        //取分页信息
        PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list);

        //返回处理结果
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return result;
    }

1.4 Controller层

接收两个参数:page、rows
调用Service查询商品列表
返回:EasyUIDataGridResult(json数据),需要使用@ResponseBody

ItemController

@RequestMapping("/item/list")
    @ResponseBody
    public EasyUIDataGridResult getItemList(Integer page,Integer rows) {
        EasyUIDataGridResult result = itemService.getItemList(page,rows);
        return result;
    }
结果

上篇:taotao-整合SSM框架
下篇:taotao-添加商品-类目选择
源代码:https://github.com/yvettee36/taotao

上一篇 下一篇

猜你喜欢

热点阅读