SSM架构商城项目(十)

7. 首页-电脑分类列表

7.1. 功能分析

已知161:电脑办公,且其下有:

 id  | parent_id | name
-----+-----------+---------
 162 |       161 | 电脑整机
 171 |       161 | 电脑配件
 186 |       161 | 外设产品
 202 |       161 | 网络产品
 210 |       161 | 办公设备
 229 |       161 | 文具/耗材
 244 |       161 | 服务产品

由于主页只显示了3个二级分类,也就表示这3个二级分类是人为设定的,在显示数据时,应该作为已知条件!

所以,主页上需要显示的电脑的分类是id=162id=171id=186的!

需要获取的数据包括:id, name

获取数据的条件:status=1 AND parent_id=?

排序规则:sort_order DESC

7.2. 首页-电脑分类列表-持久层

首先,创建与数据表结构对应的实体类com.company.store.entity.GoodsCategory

public class GoodsCategory extends BaseEntity {
    private Long id;
    private Long parentId;
    private String name;
    private Integer status;
    private Integer sortOrder;
    private Integer isParent;
    // SET/GET方法等
}

创建接口文件com.company.store.mapper.GoodsCategoryMapper,然后添加抽象方法:

List<GoodsCategory> getListByParent(Long parentId);

复制得到文件src\main\resources\mappers\GoodsCategoryMapper.xml,然后配置以上抽象方法的映射:

SELECT 
    id, name 
FROM 
    t_goods_category
WHERE 
    parent_id=? AND status=1 
ORDER BY 
    sort_order DESC 

完成,执行测试,分别根据id=162id=171id=186获取数据。

7.3. 首页-电脑分类列表-业务层

创建IGoodsCategoryService接口,在接口中声明与持久层相同的方法。

创建GoodsCategoryServiceImpl类,使用@Service("goodsCategoryService")注解,声明@Autowired private GoodsCategorMapper goodsCategoryMapper;,实现以上接口,并直接调用持久层对象的同名方法来实现抽象方法。

小结:通常,绝大部分查询可能并不涉及过多的业务!

7.4. 首页-电脑分类列表-控制器层

分析获取某个分类的列表的请求:

请求路径:/goodsCategory/list.do
请求类型:GET
请求参数:parent_id=?
响应方式:ResponseResult<List<GoodsCategory>>
是否拦截:否,无需登录即可访问

创建com.company.store.controller.GoodsCategoryController类,使用@Controller@RequestMapping("/goodsCategory")注解,声明@Autowired private IGoodsCategoryService goodsCategoryService;,该类继承自BaseController

然后,在类中添加处理请求的方法:

@RequestMapping("/list.do")
@ResponseBody
public ResponseResult<List<GoodsCategory>> getListByParent(
    @RequestParam("parent_id") Long parentId) {
    // 调用业务层对象的getListByParent(parentId)方法,并获取返回值
    // 创建返回值对象
    // 向返回值对象的data属性封装数据
    // 执行返回
}

完成后,应该通过在浏览器地址栏中输入URL执行测试:

http://localhost:8080/TeduStore/goodsCategory/list.do?parent_id=162

http://localhost:8080/TeduStore/goodsCategory/list.do?parent_id=171

http://localhost:8080/TeduStore/goodsCategory/list.do?parent_id=186

7.5. 首页-电脑分类列表-前端页面

<script type="text/javascript">
function showGoodsCategoryList(parentId, container) {
    // 发AJAX,获取某parent_id对应的结果,显示到某位置
    var url = "../goodsCategory/list.do";
    var data = "parent_id=" + parentId;
    $.ajax({
        "url": url,
        "data": data,
        "type": "GET",
        "dataType": "json",
        "success": function(json) {
            container.empty();
            var list = json.data;
            // console.log("parent_id=" + parentId);
            for (var i=0; i < list.length; i++) {
                // console.log("id=" + list[i].id + ", name=" + list[i].name);
                var html = '<li><a href="##{id}">#{name}</a></li>';
                html = html.replace("#{id}", list[i].id);
                html = html.replace("#{name}", list[i].name);
                // console.log(html);
                container.append(html);
            }
            // console.log("END");
        }
    });
}

showGoodsCategoryList(162, $("#category_162"));
showGoodsCategoryList(171, $("#category_171"));
showGoodsCategoryList(186, $("#category_186"));
</script>

8. 首页-排名前3的电脑列表

8.1. 首页-排名前3的电脑列表-功能分析

已知笔记本电脑的分类是163(category_id=163),显示需求是显示前3名的笔记本电脑,关于前3名的判断标准是取出priority值最大的3条数据

需要获取的数据包括:id, image, title, price

获取数据的条件:category_id=163 AND status=1

排序规则:priority DESC

8.2. 首页-排名前3的电脑列表-持久层

首先,创建与数据表结构对应的实体类com.company.store.entity.Goods

public class Goods extends BaseEntity {
    private Long id;
    private Long categoryId;
    private String itemType;
    private String title;
    private String sellPoint;
    private Long price;
    private Integer num;
    private String barcode;
    private String image;
    private Integer status;
    private Integer priority;
    // SET/GET方法等
}

创建接口文件com.company.store.mapper.GoodsMapper,然后添加抽象方法:

List<Goods> getListByCategory(
    @Param("categoryId") Long categoryId, 
    @Param("offset") Integer offset, 
    @Param("count") Integer count);

复制得到文件src\main\resources\mappers\GoodsMapper.xml,然后配置以上抽象方法的映射:

SELECT 
    id, image, title, price
FROM 
    t_goods
WHERE 
    category_id=? AND status=1
ORDER BY 
    priority DESC 
LIMIT
    ?, ?

完成,执行测试,根据categoryId=163offset=0count=3获取数据。

8.3. 首页-排名前3的电脑列表-业务层

创建IGoodsService接口,在接口中声明:

List<Goods> getHotList(Long categoryId, Integer count);

创建GoodsServiceImpl类,使用@Service("goodsService")注解,声明@Autowired private GoodsMapper goodsMapper;,实现以上接口,并直接调用持久层对象的方法来实现抽象方法,实现时:

goodsMapper.getListByCategory(categoryId, 0, count);

完成后,执行单元测试。

8.4. 首页-排名前3的电脑列表-控制器层

分析获取某个分类的前3名商品的列表的请求:

请求路径:/goods/hot_list.do
请求类型:GET
请求参数:category_id=?
响应方式:ResponseResult<List<Goods>>
是否拦截:否,无需登录即可访问

创建com.company.store.controller.GoodsController类,使用@Controller@RequestMapping("/goods")注解,声明@Autowired private IGoodsService goodsService;,该类继承自BaseController

然后,在类中添加处理请求的方法:

@RequestMapping("/hot_list.do")
@ResponseBody
public ResponseResult<List<Goods>> getHotList(
    @RequestParam("category_id") Long categoryId) {
    // 调用业务层对象的getHotList(categoryId, 3)方法,并获取返回值
    // 创建返回值对象
    // 向返回值对象的data属性封装数据
    // 执行返回
}

完成后,应该通过在浏览器地址栏中输入URL执行测试:

http://localhost:8080/TeduStore/goods/hot_list.do?category_id=163

猜你喜欢

转载自www.cnblogs.com/wood-life/p/10290885.html