第一步:创建分类表

第二步:分类表的实体类

package com.ljl.pojo;import javax.persistence.*;public class Category {/*** 主键*/@Idprivate Integer id;/*** 分类名称*/private String name;/*** 分类类型*/private Integer type;/*** 父id*/@Column(name = "father_id")private Integer fatherId;/*** 图标*/private String logo;/*** 口号*/private String slogan;/*** 分类图*/@Column(name = "cat_image")private String catImage;/*** 背景颜色*/@Column(name = "bg_color")private String bgColor;/*** 获取主键** @return id - 主键*/public Integer getId() {return id;}/*** 设置主键** @param id 主键*/public void setId(Integer id) {this.id = id;}/*** 获取分类名称** @return name - 分类名称*/public String getName() {return name;}/*** 设置分类名称** @param name 分类名称*/public void setName(String name) {this.name = name;}/*** 获取分类类型** @return type - 分类类型*/public Integer getType() {return type;}/*** 设置分类类型** @param type 分类类型*/public void setType(Integer type) {this.type = type;}/*** 获取父id** @return father_id - 父id*/public Integer getFatherId() {return fatherId;}/*** 设置父id** @param fatherId 父id*/public void setFatherId(Integer fatherId) {this.fatherId = fatherId;}/*** 获取图标** @return logo - 图标*/public String getLogo() {return logo;}/*** 设置图标** @param logo 图标*/public void setLogo(String logo) {this.logo = logo;}/*** 获取口号** @return slogan - 口号*/public String getSlogan() {return slogan;}/*** 设置口号** @param slogan 口号*/public void setSlogan(String slogan) {this.slogan = slogan;}/*** 获取分类图** @return cat_image - 分类图*/public String getCatImage() {return catImage;}/*** 设置分类图** @param catImage 分类图*/public void setCatImage(String catImage) {this.catImage = catImage;}/*** 获取背景颜色** @return bg_color - 背景颜色*/public String getBgColor() {return bgColor;}/*** 设置背景颜色** @param bgColor 背景颜色*/public void setBgColor(String bgColor) {this.bgColor = bgColor;}
}

第三步:mapper接口

package com.ljl.mapper;import com.ljl.my.mapper.MyMapper;
import com.ljl.pojo.Category;public interface CategoryMapper extends MyMapper<Category> {
}

第四步:service层

package com.ljl.service;import com.ljl.pojo.Carousel;
import com.ljl.pojo.Category;import java.util.List;public interface CategoryService {/*** 查询所有一级分类* @param* @return*/public List<Category> queryAllRootLevelCat();}

第五步:service实现层

package com.ljl.service.impl;import com.ljl.mapper.CategoryMapper;
import com.ljl.pojo.Category;
import com.ljl.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;import java.util.List;@Service
public class CategoryServiceImpl implements CategoryService {@Autowiredprivate CategoryMapper categoryMapper;@Overridepublic List<Category> queryAllRootLevelCat() {Example example=new Example(Category.class);Example.Criteria criteria = example.createCriteria();criteria.andEqualTo("type",1);//表示类型为一级分类List<Category> result = categoryMapper.selectByExample(example);return result;}
}

第六步:controller层

    @ApiOperation(value = "获取商品分类(一级分类)",notes="获取商品分类(一级分类)",httpMethod="GET")@GetMapping("/cats")public JSONResult cats(){List<Category> list = categoryService.queryAllRootLevelCat();return JSONResult.ok(list);}

第七步:根据一级分类查询子分类

select f.id as id,f.`name` as `name`,f.type as type,f.father_id as fatherId,c.id as subId,c.`name` as `subName`,c.type as subType,c.father_id as subFatherId,fromcategory f
LEFT JOINcategory c
ONf.id=c.father_id
where f.father_id=1

第八步:mapper接口

package com.ljl.mapper;import java.util.List;public interface CategoryMapperCustom  {public List getSubCatList(Integer rootCatId);
}

第九步:mapper实现

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ljl.mapper.CategoryMapper" ><!--column代表表名,property代表类的属性名--><resultMap id="myCategoryVO" type="com.ljl.pojo.vo.CategoryVO"><id column="id" property="id"  /><result column="name" property="name"  /><result column="type" property="type"  /><result column="fatherId" property="fatherId"  />
<!--    collection 标签:用于定义关联的list集合类型的封装规则property:对应三级分类的list属性名ofType:集合的类型,三级分类的VO--><collection property="SubCatList" ofType="com.ljl.pojo.vo.SubCategoryVO"><id column="subId" property="subId"  /><result column="subName" property="subName"  /><result column="subType" property="subType"  /><result column="subFatherId" property="subFatherId"  /></collection></resultMap><select id="getSubCatList" resultMap="myCategoryVO" parameterType="int">selectf.id as id,f.`name` as `name`,f.type as type,f.father_id as fatherId,c.id as subId,c.`name` as `subName`,c.type as subType,c.father_id as subFatherIdfromcategory fLEFT JOINcategory cONf.id=c.father_idwhere f.father_id=#{rootCatId}</select></mapper>

第十步:service接口

    /*** 根据一级分类id查询子分类信息* @param rootCatId* @return*/public List<CategoryVO> getSubCatList(Integer rootCatId);

第十一步:service实现类

    @Transactional(propagation = Propagation.SUPPORTS)@Overridepublic List<CategoryVO> getSubCatList(Integer rootCatId) {return categoryMapperCustom.getSubCatList(rootCatId);}

第十二步:controller类

    @ApiOperation(value = "获取商品子分类",notes="获取商品子分类",httpMethod="GET")@GetMapping("/subCat/{rootCatId}") //表示取得路径参数public JSONResult subCat(@ApiParam(name="rootCatId",value="一级分类id",required = true)@PathVariable Integer rootCatId){if(rootCatId==null){return JSONResult.errorMsg("分类不存在");}List<CategoryVO> list=categoryService.getSubCatList(rootCatId);return JSONResult.ok(list);}

备注:BO类

package com.ljl.pojo.vo;
import java.util.List;/*** 二级分类VO*/
public class CategoryVO {private Integer id;private String name;private String type;private Integer fatherId;//把三级分类封装到二级分类里面去private List<SubCategoryVO> subCatList;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getType() {return type;}public void setType(String type) {this.type = type;}public Integer getFatherId() {return fatherId;}public void setFatherId(Integer fatherId) {this.fatherId = fatherId;}public List<SubCategoryVO> getSubCatList() {return subCatList;}public void setSubCatList(List<SubCategoryVO> subCatList) {this.subCatList = subCatList;}
}
package com.ljl.pojo.vo;public class SubCategoryVO {private Integer subId;private String subName;private String subType;private Integer subFatherId;public Integer getSubId() {return subId;}public void setSubId(Integer subId) {this.subId = subId;}public String getSubName() {return subName;}public void setSubName(String subName) {this.subName = subName;}public String getSubType() {return subType;}public void setSubType(String subType) {this.subType = subType;}public Integer getSubFatherId() {return subFatherId;}public void setSubFatherId(Integer subFatherId) {this.subFatherId = subFatherId;}
}

电商商城之分类实现(重点)相关推荐

  1. 来客推仿拼多多电商商城小程序源码

    简介: 一款来客推内核仿拼多多电商商城小程序源码,APP+钱包+开源版. 安装步骤: 环境要求 环境配置正常,最好是PHP5.6+Mysql5.5 win下面可以使用phpstudy集成环境部署安装 ...

  2. Django框架的电商商城的设计与实现python语言

     摘要 随着计算机技术,网络技术的迅猛发展,Internet 的不断普及,网络在各个领域里发挥了越来越重要的作用.特别是随着近年人民生活水平不断提高,电商商城给商家的业务带来了更大的发展机遇. 在经济 ...

  3. 跨境电商系统开发-电商商城系统平台定制方案

    随着业务的拓展,不少企业开始将目光转向国外市场,那么如何定制一套属于想自己的跨境出海电商商城方案呢?需要做好以下关口把关: 欢迎名片交流探讨开发平台流程 买家端(h5/pc/app)  www.mar ...

  4. jQuery电商导航热门分类布局

    jQuery电商导航热门分类布局 jQuery制作红色常用的商城网站左侧导航悬停显示热门分类和全部商家品牌布局代码.这是一款综合生活购物网站导航div布局. 演示地址 下载地址

  5. 电商商城系统活动设计

    2019独角兽企业重金招聘Python工程师标准>>> 电商商城系统活动设计 1.表结构设计 表结构设计如下: 活动主表 活动区域表 活动商品表 活动时间表 活动平台表 活动支付方式 ...

  6. 小程序电商商城怎么搭建?

    做电商的企业商家都会首先搭建好自己的电商商城,尤其是现在小程序盛行的时代,小程序电商商城更是做电商的企业商家的必备媒介.那么小程序电商商城怎么搭建?下面给大家说说一些流程作为参考. 一.准备事项 搭建 ...

  7. vue+element简单实现商城网站首页,模仿电商商城

    1.安装启动vue项目可参考 vue+element简单实现商城网站首页,模仿小米电商商城https://blog.csdn.net/lucky_fang/article/details/121544 ...

  8. 如何搭建一个靠谱的电商商城系统?

    在当今互联网时代,电商已成为一种普及化的购物方式,许多商家都希望能够打造一个自己的电商平台.然而,搭建一个靠谱的电商商城系统并不是一件容易的事情,需要考虑很多方面的因素.下面我将结合自己的经验,为大家 ...

  9. 多语言多商户跨境电商商城源码_平台开发部署选择

    多语言多商户跨境电商平台是一个允许来自不同国家和地区的消费者在同一平台上浏览和购买商品的在线购物平台.这种电商平台通常支持多种语言,并且有多个商户销售其商品,消费者可以选择不同的商户和商品.同时,该平 ...

最新文章

  1. swift菜鸟入门视频教程-03-字符串和字符
  2. 【RecyclerView】 十三、RecyclerView 数据更新 ( 移动数据 | 数据改变 )
  3. Docker过程汇总
  4. pimg src=http://img.blog.csdn.net/20150823142545135?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ...
  5. MySQL中innodb_page_cleaners详解
  6. 求Fibonacc数列中大于t(t3)的最小一个数 例 带入1000输出1597
  7. 多任务学习(MTL)在转化率预估上的应用
  8. 夯实Java基础(二十二)——Java8新特性之Lambda表达式
  9. 设计模式20_观察者
  10. 关于OPENCV 访问外部传进来的Mat矩阵元素的问题
  11. Java线程状态转换
  12. day022 python (re模块和 模块)
  13. Unix编程之size_t、ssize_t
  14. 黑马程序员基基基基础知识——结构(bilibili p24-p41)
  15. 网线之RJ45接口定义及网线线序
  16. 计算机思维和应用技术,计算机的思维与计算机应用关系分析
  17. 利用Python绘制三维的规则体(3维柱体、立方体和旋转棱柱)
  18. openbsd运行Linux应用程序,为什么默认的Linux安装运行的进程多于默认的OpenBSD安装?...
  19. USACO/ratios 3.2.4
  20. 通过快递鸟如何接入中通快递电子面单

热门文章

  1. 51单片机的应用——I/O口数据传送
  2. python去除拼音声调字母,替换为字母
  3. 美国计算机科学专业排名第一,美国计算机科学专业排名一览
  4. 《Wir wilden weisen Frauen》翻译——连载
  5. Linux服务器搭建测试环境笔记
  6. 欧姆龙e5dc温控器_欧姆龙E5DC-QX2ASM-800用户手册 数字温度控制器手册 - 广州凌控...
  7. .py文件和.yaml文件作为配置文件
  8. 面试太诚实居然被淘汰?真的不公平!
  9. 计算机频谱仪仿真,频谱分析仪模拟仿真.doc
  10. 我计算机桌面的word图标改变了咋办,桌面上word图标异常的处理方法