分布式电商项目(04)--商品列表查询及分页

前言:前面写了后天管理系统工程搭建以及框架的整合测试,今天写一下商品列表的分页查询

1 需求分析

前台使用easyui的分页工具,后台则使用mybatis分页插件pagehelper

如上图所示,打开后台首页,点击查询商品,按下F12,可以看到easyui的分页界面会向controller发送两个数据page:1,rows:30

controller通过service层以及dao层查询到数据之后也需要将数据封装成easyui需要的格式,而easyui需要的数据格式如下

{
    total:"2",
    rows:[
            {"id":"1","name":"username1"},
            {"id":"2","name":"username2"}
         ]
}

对应现在的场景就是将数据封装成total:商品总数,rows:商品信息列表的格式

2 具体实现

2.1 封装通用的分页工具类

由于分页在后面肯定还会用到,现在在common工程下写一个easyui分页的工具类,具体的代码如下:

public class EUDataGridResult {

    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;
    }
}

2.2 编写接口及其实现类

在service工程下编写service类及其实现类

ItemService的代码如下:

public interface ItemService {

    EUDataGridResult getItemList(Integer page, Integer rows);
   
}

ItemServiceImpl的代码如下:

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper itemMapper;

    /**
     * 商品列表查询
     * @param page
     * @param rows
     * @return
     */
    @Override
    public EUDataGridResult getItemList(Integer page, Integer rows) {
        TbItemExample example =  new TbItemExample();
        PageHelper.startPage(page,rows);
        List<TbItem> list = itemMapper.selectByExample(example);
        EUDataGridResult result = new EUDataGridResult();
        result.setRows(list);
        PageInfo<TbItem> info = new PageInfo<>(list);
        result.setTotal(info.getTotal());
        return result;
    }

   

}

2.3 编写Controller

ItemController的代码如下:

package com.taotao.controller;

import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.taotao.pojo.EUDataGridResult;
import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbItem;

import com.taotao.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ItemController {

    @Autowired
    private ItemService itemService;


    /**
     * 商品列表查询
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/item/list")
    @ResponseBody
    public EUDataGridResult getItemList(Integer page, Integer rows ){
        EUDataGridResult result = itemService.getItemList(page, rows);
        return result;
    }


}

此处要注意,RequestMapping中的值一定要与jsp页面中的请求的值是一致的

3 测试

运行项目,点击查询商品可以查询出商品列表即为成功,即出现如下图所示的界面:

4 相关文件

下面提供一些相关的资源下载,包括后台管理系统的静态资源,博主使用的本地仓库等

链接:https://pan.baidu.com/s/1mWDQznk0N5um_YMB7Greiw

提取码:1gh3

猜你喜欢

转载自www.cnblogs.com/Cryptonym/p/9961059.html