9、商品列表查询

商品列表查询

点击查询商品:
在这里插入图片描述
发送的请求为:GET http://localhost:8081/item/list?page=1&rows=30

对应的 jsp 页面为:item-list.jsp

  • 请求的 url:/item/list
  • 请求的参数:page=1&rows=30
  • 响应的 json 数据格式:
    EasyUI 中 datagrid 控件要求的数据格式为:
    {total:”2”,rows:[{“id”:”1”,”name”:”张三”},{“id”:”2”,”name”:”李四”}]}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<table class="easyui-datagrid" id="itemList" title="商品列表" 
       data-options="singleSelect:false,collapsible:true,pagination:true,url:'/item/list',method:'get',pageSize:30,toolbar:toolbar">
    <thead>
        <tr>
        	<th data-options="field:'ck',checkbox:true"></th>
        	<th data-options="field:'id',width:60">商品ID</th>
            <th data-options="field:'title',width:200">商品标题</th>
            <th data-options="field:'cid',width:100">叶子类目</th>
            <th data-options="field:'sellPoint',width:100">卖点</th>
            <th data-options="field:'price',width:70,align:'right',formatter:E3.formatPrice">价格</th>
            <th data-options="field:'num',width:70,align:'right'">库存数量</th>
            <th data-options="field:'barcode',width:100">条形码</th>
            <th data-options="field:'status',width:60,align:'center',formatter:E3.formatItemStatus">状态</th>
            <th data-options="field:'created',width:130,align:'center',formatter:E3.formatDateTime">创建日期</th>
            <th data-options="field:'updated',width:130,align:'center',formatter:E3.formatDateTime">更新日期</th>
        </tr>
    </thead>
</table>
......

编写实体类封装 EasyUI 中 datagrid 控件要求的数据格式

由于服务层和表现层都要用,故放到 e3-common 中:
在这里插入图片描述
响应的 json 数据格式 EasyUIDataGridResult :

package cn.ynx.e3mall.common.pojo;

import java.io.Serializable;
import java.util.List;

public class EasyUIDataGridResult implements Serializable {

    private Integer total;

    private List<?> rows;

    public Integer getTotal() {
        return total;
    }

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

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

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

}

分页处理

逆向工程生成的代码是不支持分页处理的,如果想进行分页需要自己编写 mapper ,这样就失去逆向工程的意义了。为了提高开发效率可以使用 mybatis 的分页插件 PageHelper 。

分页插件 PageHelper

Mybatis 分页插件 - PageHelper 说明

如果你也在用 Mybatis ,建议尝试该分页插件,这个一定是最方便使用的分页插件。
该插件目前支持 Oracle 、 Mysql 、 MariaDB 、 SQLite 、 Hsqldb 、 PostgreSQL 六种数据库分页。

使用方法

第一步:把 PageHelper 依赖的 jar 包添加到 e3-manager-dao 工程中。(已经加入了)
第二步:在Mybatis配置xml中配置拦截器插件:
SqlMapConfig.xml 里面添加:

    <plugins>
        <!-- com.github.pagehelper为PageHelper类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

第三步:在代码中使用。

//1、设置分页信息:
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectIf(1);

//2、取分页信息
//分页后,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>,
Page<Country> listCountry = (Page<Country>)list;
listCountry.getTotal();

3、取分页信息的第二种方法
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());

分页测试

package cn.ynx.e3mall.pagehelper;

import cn.ynx.e3mall.mapper.TbItemMapper;
import cn.ynx.e3mall.pojo.TbItem;
import cn.ynx.e3mall.pojo.TbItemExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class PageHelperTest {

    @Test
    public void testPageHelper() throws Exception {
        //初始化spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
        //获得Mapper的代理对象
        TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
        //设置分页信息
        PageHelper.startPage(1, 30);
        //执行查询
        TbItemExample example = new TbItemExample();
        List<TbItem> list = itemMapper.selectByExample(example);
        //取分页信息
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo.getTotal());
        System.out.println(pageInfo.getPages());
        System.out.println(pageInfo.getPageNum());
        System.out.println(pageInfo.getPageSize());
    }

}

在这里插入图片描述

商品列表查询

编写 Service 层

  • 参数:int page ,int rows
  • 业务逻辑:查询所有商品列表,要进行分页处理。
  • 返回值:EasyUIDataGridResult
package cn.ynx.e3mall.service;

import cn.ynx.e3mall.common.pojo.EasyUIDataGridResult;
import cn.ynx.e3mall.pojo.TbItem;

public interface ItemService {

    TbItem getTbItemById(Long itemId);
    EasyUIDataGridResult getTbItemList(int page, int rows);

}
    @Override
    public EasyUIDataGridResult getTbItemList(int page, int rows) {
        //设置分页信息
        PageHelper.startPage(page, rows);
        //执行查询
        TbItemExample example = new TbItemExample();
        List<TbItem> list = tbItemMapper.selectByExample(example);
        //取分页信息
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);

        //创建返回结果对象
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        result.setTotal((int)pageInfo.getTotal());
        result.setRows(list);

        return result;
    }

发布服务:

    <dubbo:service interface="cn.ynx.e3mall.service.ItemService" ref="itemServiceImpl" timeout="300000" />

编写 Controller 层

引用服务:

    <!-- 引用dubbo服务 -->
    <dubbo:application name="e3-manager-web"/>
    <dubbo:registry protocol="zookeeper" address="192.168.25.11:2181"/>
    <dubbo:reference interface="cn.ynx.e3mall.service.ItemService" id="itemService" />
  • 初始化表格请求的 url :/item/list
  • Datagrid 默认请求参数:
    • page:当前的页码,从1开始。
    • rows:每页显示的记录数。
  • 响应的数据:json 数据。EasyUIDataGridResult
    @RequestMapping("/list")
    @ResponseBody
    public EasyUIDataGridResult getTbItemList(Integer page, Integer rows){
        EasyUIDataGridResult tbItemList = itemService.getTbItemList(page, rows);
        return tbItemList;
    }

测试:

点击下面的按钮,都能正常工作。
在这里插入图片描述

IDEA 的 maven 安装工程跳过测试

在这里插入图片描述

查询商品出现警告问题

十二月 07, 2018 10:38:32 下午 com.alibaba.com.caucho.hessian.io.SerializerFactory getDeserializer
警告: Hessian/Burlap: 'com.github.pagehelper.Page' is an unknown class in WebappClassLoader
  context: 
  delegate: false
  repositories:
----------> Parent Classloader:
ClassRealm[plugin>org.apache.tomcat.maven:tomcat7-maven-plugin:2.2, parent: sun.misc.Launcher$AppClassLoader@18b4aac2]
:
java.lang.ClassNotFoundException: com.github.pagehelper.Page

原因:
e3-manager-web 中没有 PageHelper 的相关依赖,加入依赖即可。

猜你喜欢

转载自blog.csdn.net/weixin_42112635/article/details/84887731
今日推荐