How does node+mysql, generate the following shopping cart interfaces (array package objects), such as the following figure and code

< H2 > problem, want to find out a data set similar to Taobao shopping cart < / H2 >

A user has a shopping cart, there are multiple stores under one shopping cart, and there are multiple items under one store
linked table query
1. Check out the shopping cart data according to the user id,
2. Under a shopping cart, there are multiple stores, black box
3. There are multiple items under the store, with a blue box
4. A commodity with information about it, red box

clipboard.png

mysql

id


id



clipboard.png

            data: [
                {
                    cart_goods_number: 3,
                    goods_id: 3,
                    goods_name: "honor7x",
                    goods_img: "honor7x.png",
                    goods_price: 1299,
                    shop_id: 4,
                    shop_name: "honor"
                },
                {
                    cart_goods_number: 2,
                    goods_id: 4,
                    goods_name: "honor6x",
                    goods_img: "honor6x.png",
                    goods_price: 1199,
                    shop_id: 4,
                    shop_name: "honor"
                },
                {
                    cart_goods_number: 1,
                    goods_id: 1,
                    goods_name: "iPhonex",
                    goods_img: "iPhonex.png",
                    goods_price: 8888,
                    shop_id: 1,
                    shop_name: "iPhone"
                }
            ],
< H2 > the data set I want looks like, < / H2 >

A user has a shopping cart, there are multiple stores under one shopping cart, and there are multiple items under one store

            data1: [
                {
                    shop_id: 4,
                    shop_name: "honor",
                    children: [
                        {
                            cart_goods_number: 3,
                            goods_id: 3,
                            goods_name: "honor7x",
                            goods_img: "honor7x.png",
                            goods_price: 1299
                        },
                        {
                            cart_goods_number: 2,
                            goods_id: 4,
                            goods_name: "honor6x",
                            goods_img: "honor6x.png",
                            goods_price: 1199
                        }
                    ]
                },
                {
                    shop_id: 1,
                    shop_name: "iPhone",
                    children: [
                        {
                            cart_goods_number: 1,
                            goods_id: 1,
                            goods_name: "iPhonex",
                            goods_img: "iPhonex.png",
                            goods_price: 8888
                        }
                    ]
                }
            ]
< H2 > Database < / H2 >

Shopping cart list

user id, merchandise quantity id, merchandise

CREATE TABLE `cart` (
  `cart_id` int(10) NOT NULL AUTO_INCREMENT,
  `user_id` int(10) DEFAULT NULL,
  `goods_id` int(10) DEFAULT NULL,
  `cart_goods_number` int(10) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `last_edit_time` datetime DEFAULT NULL,
  PRIMARY KEY (`cart_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

user table

CREATE TABLE `user` (
  `user_id` int(10) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(10) DEFAULT NULL,
  `user_password` varchar(10) DEFAULT NULL,
  `user_img` varchar(1024) DEFAULT NULL,
  `user_phone` varchar(11) DEFAULT NULL,
  `user_sex` int(2) NOT NULL DEFAULT "1" COMMENT "1:,2:",
  `user_address` varchar(100) DEFAULT NULL,
  `user_state` int(2) NOT NULL DEFAULT "1" COMMENT "1:,2:",
  `create_time` datetime DEFAULT NULL,
  `last_edit_time` datetime DEFAULT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `user_name` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

Commodity list

CREATE TABLE `goods` (
  `goods_id` int(10) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(10) DEFAULT NULL,
  `goods_img` varchar(1024) DEFAULT NULL,
  `goods_price` double DEFAULT NULL,
  `goods_number` int(10) DEFAULT NULL,
  `goods_priority` int(2) NOT NULL DEFAULT "1",
  `goods_state` int(2) NOT NULL DEFAULT "1" COMMENT "1:,2:",
  `shop_id` int(10) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `last_edit_time` datetime DEFAULT NULL,
  `goods_desc` varchar(1024) DEFAULT NULL,
  PRIMARY KEY (`goods_id`),
  UNIQUE KEY `goods_name` (`goods_name`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

Store table

CREATE TABLE `shop` (
  `shop_id` int(10) NOT NULL AUTO_INCREMENT,
  `shop_name` varchar(10) DEFAULT NULL,
  `shop_phone` varchar(11) DEFAULT NULL,
  `shop_img` varchar(1024) DEFAULT NULL,
  `shop_category_id` int(10) DEFAULT NULL,
  `shop_priority` int(2) NOT NULL DEFAULT "1",
  `shop_state` int(2) NOT NULL DEFAULT "1" COMMENT "1:,2:",
  `create_time` datetime DEFAULT NULL,
  `last_edit_time` datetime DEFAULT NULL,
  `shop_desc` varchar(1024) DEFAULT NULL,
  PRIMARY KEY (`shop_id`),
  UNIQUE KEY `shop_name` (`shop_name`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

define a json, and then insert the result into json by hierarchical query


this problem is actually very easy to solve. It can be realized with JSONObject nesting.

package testdemo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class demo {
    public static void main(String[] args){
        JSONObject root = new JSONObject();
        int shopNumber,goodsNumber;
        JSONArray shopArray = new JSONArray();
        for(shopNumber=0;shopNumber<2;shopNumberPP){
            JSONObject shopObj = new JSONObject();
            shopObj.put("shop_id",1);
            shopObj.put("shop_name","honor");
            shopObj.put("children",1);
            JSONArray goodsArray = new JSONArray();
            for(goodsNumber=0;goodsNumber<3;goodsNumberPP){
                JSONObject goods = new JSONObject();
                goods.put("cart_goods_number","3");
                goods.put("goods_id","1");
                goods.put("goods_name","honor7x");
                goods.put("goods_img","honor7x.png");
                goods.put("goods_price","1299");
                goodsArray.add(goods);
            }
            shopObj.put("children",goodsArray);
            shopArray.add(shopObj);
        }
        root.put("data1",shopArray);
        System.out.println(root.toJSONString());
    }
}
< hr >

output result:
{"data1": [{"shop_id": 1, "children": [{"goods_price": "1299", "goods_id": "1", "goods_name": "honor7x", "goods_img": "honor7x.png", "cart_goods_number": "3"}, {"goods_price": "1299", "goods_id": "1", "goods_name": "honor7x", "goods_img": "honor7x.png", "cart_goods_number": "3"}, {"goods_price": "1299", "goods_id": "1", "goods_name": "honor7x", "goods_img": "honor7x.png", "cart_goods_number": "3"}], "shop_name": "Honor House"}, {"shop_id": 1, "children": [{"goods_price": "1299", "goods_id": "1", "goods_name": "honor7x", "goods_img": "honor7x.png", "cart_goods_number": "3"}, {"goods_price": "1299", "goods_id": "1", "goods_name": "honor7x", "goods_img": "honor7x.png", "cart_goods_number": "3"}, {"goods_price": "1299", "goods_id": "1", "goods_name": "honor7x", "goods_img": "honor7x.png", "cart_goods_number": "3"}], "shop_name": "Home of Honor"}]}

clipboard.png

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eab220-202e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eab220-202e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?