首页商品分类展示——淘淘商城(十四)

需求

分析

总结

{
  "data": [
    {
      "u": "/products/1.html",
      "n": "<a href='/products/1.html'>图书、音像、电子书刊</a>",
      "i": [
        {
          "u": "/products/2.html",
          "n": "电子书刊",
          "i": [
            "/products/3.html|电子书",
            "/products/4.html|网络原创",
            "/products/5.html|数字杂志",
            "/products/6.html|多媒体图书"
          ]
        },
        {
          "u": "/products/7.html",
          "n": "音像",
          "i": [
            "/products/8.html|音乐",
            "/products/9.html|影视",
            "/products/10.html|教育音像"
          ]
        },
        {
          "u": "/products/11.html",
          "n": "英文原版",
          "i": [
            "/products/12.html|少儿",
            "/products/13.html|商务投资",
            "/products/14.html|英语学习与考试",
            "/products/15.html|小说",
            "/products/16.html|传记",
            "/products/17.html|励志"
          ]
        },
 ......

Service层

  • taotao-rest的com.taotao.rest.pojo包下创建商品分类的节点类CatNode,和商品类目查询结果CatResult。
    • CatNode中可以使用 @JsonProperty 注解注释用jackson转化为json后变量的名称。
    • 子节点可能为String也可能为CatNode,所以items类型为?,data的类型也为?。
public class CatNode {

    @JsonProperty("u")
    private String url;

    @JsonProperty("n")
    private String name;

    /**
     * 子节点。子节点若为叶节点则为String,若非叶节点则为CatNode
     */
    @JsonProperty("i")
    private List<?> items;

    public CatNode(String url, String name, List<?> items) {
        this.url = url;
        this.name = name;
        this.items = items;
    }

    public String getUrl() {
        return url;
    }

    public String getName() {
        return name;
    }

    public List<?> getItems() {
        return items;
    }
}

public class CatResult {

    private List<?> data;

    public List<?> getData() {
        return data;
    }

    public void setData(List<?> data) {
        this.data = data;
    }
}
  • 创建ItemCatService和实现类,查询所有商品分类列表。
    • 商品分类列表需要使用递归查询。
public interface ItemCatService {

    CatResult getItemCatList();
}

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public CatResult getItemCatList() {
        CatResult result = new CatResult();
        List<?> list = getListByParentId(0);
        result.setData(list);
        return result;
    }

    /**
     * 递归获得商品类目
     */
    private List<?> getListByParentId(long parentId) {
        List list = new ArrayList<>();

        //查询条件
        TbItemCatExample example = new TbItemCatExample();
        Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);

        List<TbItemCat> itemCatList = itemCatMapper.selectByExample(example);
        for (TbItemCat itemCat : itemCatList) {
            if (itemCat.getIsParent()) {
                String url = "/products/" + itemCat.getId() + ".html";
                String name = null;
                if (parentId == 0) {
                    name = "<a href='/products/" + itemCat.getId() + ".html'>" + itemCat.getName() + "</a>";
                } else {
                    name = itemCat.getName();
                }
                List<?> items = getListByParentId(itemCat.getId());
                list.add(new CatNode(url, name, items));
            } else {
                String s = "/products/" + itemCat.getId() + ".html|" + itemCat.getName();
                list.add(s);
            }
        }

        return list;
    }
}

Controller层

  • 接受回调参数名,返回商品类目信息的json作为参数的js字符串。
    • 返回类型为 “application/json;charset=UTF-8”, 否则会乱码。
    • 受前端界面限制,只选取前14类商品(也可修改前端页面显示全部商品,但是逻辑较复杂,省略)。
@Controller
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;

    /**
     * @param callback 回调函数名称
     */
    @RequestMapping(value = "itemcat/all", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String getItemCatList(String callback) {
        CatResult catResult = itemCatService.getItemCatList();

        //选取前14类商品
        List newData = new ArrayList<>(14);
        List<?> data = catResult.getData();
        for (int i = 0; i < 14; i++) {
            newData.add(data.get(i));
        }
        catResult.setData(newData);

        return callback + "(" + JsonUtils.objectToJson(catResult) + ")";
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值