[SSM Distributed Architecture E-commerce Project-05] New Products in the Background Management System

product data structure

write picture description here
Table Structure:
write picture description here

Why is the price stored in the smallest unit?
– To avoid problems with decimal calculations.

Product description table:

write picture description here

Why separate the product description from the basic data of the product?
1. The large amount of data described in the product will cause the data file to become larger and affect the query speed.
2. In the later stage, the storage of commodity description data will be transformed, so it is necessary to separate the description data

Click the event of the leaf node of the product category

write picture description here
Defined in item-add.jsp:
write picture description here

Add price on page

write picture description here

Use of KindEditor

Effects:
write picture description here
1. Import the js file
write picture description here
2. Define multi-line text
write picture description here
3. Create a rich text editor
write picture description here
Common.js through JS:
write picture description here

Submit an event

function submitForm(){
        if(!$('#itemAddForm').form('validate')){
            $.messager.alert('提示','表单还未填写完成!');
            return ;
        }
        //处理商品的价格的单位,将元转化为分
        $("#itemAddForm [name=price]").val(eval($("#itemAddForm [name=priceView]").val()) * 100);
        //将编辑器中的内容同步到隐藏多行文本中
        itemAddEditor.sync();

        //输入的规格参数数据保存为json
        var paramJson = [];
        $("#itemAddForm .params li").each(function(i,e){
            var trs = $(e).find("tr");
            var group = trs.eq(0).text();
            var ps = [];
            for(var i = 1;i<trs.length;i++){
                var tr = trs.eq(i);
                ps.push({
                    "k" : $.trim(tr.find("td").eq(0).find("span").text()),
                    "v" : $.trim(tr.find("input").val())
                });
            }
            paramJson.push({
                "group" : group,
                "params": ps
            });
        });
        paramJson = JSON.stringify(paramJson);

        $("#itemAddForm [name=itemParams]").val(paramJson);

        /*
        $.post("/rest/item/save",$("#itemAddForm").serialize(), function(data){
            if(data.status == 200){
                $.messager.alert('提示','新增商品成功!');
            }
        });
        */

        //提交到后台的RESTful
        $.ajax({
           type: "POST",
           url: "/rest/item",
           data: $("#itemAddForm").serialize(), //表单序列化,将所有的输入内容转化成K/V数据格式
           statusCode : {
               201 : function(){
                   $.messager.alert('提示','新增商品成功!');
               },
                400 : function(){
                   $.messager.alert('提示','提交的参数不合法!');
               },
               500 : function(){
                   $.messager.alert('提示','新增商品失败!');
               }
           }
        });
    }

The business id in the product description table

write picture description here

accomplish

POJO

package com.taotao.manage.pojo;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "tb_item")
public class Item extends BasePojo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;

    private String sellPoint;

    private Long price;

    private Integer num;

    private String barcode;

    private String image;

    private Long cid;

    private Integer status;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSellPoint() {
        return sellPoint;
    }

    public void setSellPoint(String sellPoint) {
        this.sellPoint = sellPoint;
    }

    public Long getPrice() {
        return price;
    }

    public void setPrice(Long price) {
        this.price = price;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public String getBarcode() {
        return barcode;
    }

    public void setBarcode(String barcode) {
        this.barcode = barcode;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Long getCid() {
        return cid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Item [id=" + id + ", title=" + title + ", sellPoint=" + sellPoint + ", price=" + price
                + ", num=" + num + ", barcode=" + barcode + ", image=" + image + ", cid=" + cid + ", status="
                + status + "]";
    }

}
package com.taotao.manage.pojo;

import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "tb_item_desc")
public class ItemDesc extends BasePojo{

    @Id//对应tb_item中的id
    private Long itemId;

    private String itemDesc;

    public Long getItemId() {
        return itemId;
    }

    public void setItemId(Long itemId) {
        this.itemId = itemId;
    }

    public String getItemDesc() {
        return itemDesc;
    }

    public void setItemDesc(String itemDesc) {
        this.itemDesc = itemDesc;
    }



}

Mapper

write picture description here

package com.taotao.manage.mapper;

import com.github.abel533.mapper.Mapper;
import com.taotao.manage.pojo.Item;

public interface ItemMapper extends Mapper<Item>{

}
package com.taotao.manage.mapper;

import com.github.abel533.mapper.Mapper;
import com.taotao.manage.pojo.ItemDesc;

public interface ItemDescMapper extends Mapper<ItemDesc> {

}

Controller

write picture description here

business problem

write picture description here

Solution:
Put the two saves in the same method of the same Service.
write picture description here

package com.taotao.manage.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.manage.pojo.Item;
import com.taotao.manage.pojo.ItemDesc;

@Service
public class ItemService extends BaseService<Item> {

    @Autowired
    private ItemDescService itemDescService;

    public Boolean saveItem(Item item, String desc) {
        // 初始值
        item.setStatus(1);
        item.setId(null); // 出于安全考虑,强制设置id为null,通过数据库自增长得到
        Integer count1 = super.save(item);

        // 保存商品描述数据
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        Integer count2 = this.itemDescService.save(itemDesc);

        return count1.intValue() == 1 && count2.intValue() == 1;

    }

}
package com.taotao.manage.service;

import org.springframework.stereotype.Service;

import com.taotao.manage.pojo.ItemDesc;

@Service
public class ItemDescService extends BaseService<ItemDesc>{

}

Controller:
write picture description here

package com.taotao.manage.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manage.pojo.Item;
import com.taotao.manage.service.ItemService;
@RequestMapping("item")
@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> saveItem(Item item, @RequestParam("desc") String desc) {
        try {
            if (StringUtils.isEmpty(item.getTitle())) { // TODO 未完成,待优化
                // 参数有误, 400
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }

            // 保存商品
            Boolean bool = this.itemService.saveItem(item, desc);
            if (!bool) {
                // 保存失败
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}

Test run to see the log:
write picture description here

write picture description here

write picture description here

write picture description here

It can be seen from the log that they belong to the same transaction.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325769925&siteId=291194637