编程那点事编程那点事

专注编程入门及提高
探究程序员职业规划之道!

商品列表页面的开发

本套电商系统教程的侧重点在后台,所以前台怎么开发的就不讲解了,都是现成的代码。

后台请求的url是/item/list,接收page和rows2个参数。

1、新增接口ItemService,代码如下

package com.codingwhy.service;
import com.codingwhy.pojo.EasyUIDataGridResult;
public interface ItemService {
    EasyUIDataGridResult getItemList(int page, int rows);
}

代码路径如下图所示:

新增接口ItemService

2、新增接口ItemService实现类ItemServiceImpl

package com.codingwhy.service.impl;

import com.codingwhy.mapper.TbItemMapper;
import com.codingwhy.pojo.EasyUIDataGridResult;
import com.codingwhy.pojo.TbItem;
import com.codingwhy.pojo.TbItemExample;
import com.codingwhy.service.ItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper itemMapper;

    @Override
    public EasyUIDataGridResult getItemList(int page, int rows) {
        TbItemExample example = new TbItemExample();

        PageHelper.startPage(page,rows);
        List<TbItem> list = itemMapper.selectByExample(example);

        EasyUIDataGridResult result = new EasyUIDataGridResult();
        result.setRows(list);

        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        result.setTotal(pageInfo.getTotal());
        return result;
    }
}

代码路径如下图所示

新增实现类ItemServiceImpl

3、新增ItemController,具体代码如下

package com.codingwhy.controller;
import com.codingwhy.pojo.EasyUIDataGridResult;
import com.codingwhy.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;
    @RequestMapping("/item/list")
    @ResponseBody
    public EasyUIDataGridResult getItemList(Integer page, Integer rows){
        EasyUIDataGridResult result = itemService.getItemList(page,rows);
        return result;
    }
}

代码路径如下图所示

新增ItemController类

然后,重启tomcat,在运行商城的后台管理系统,列表就加载出来了!

215400493.png

需要说明的是在本文中有2处代码是没有列出来的,一个是EasyUIDataGridResult类,一个是分页的配置,大家需要参考以下2篇文章

工具类EasyUIDataGridResult

商城系统分页处理:Mybatis分页插件PageHelper详解

未经允许不得转载: 技术文章 » Java编程 » 商品列表页面的开发