# 159. 商品详情页动态渲染系统:基于 Spring Cloud 开发商品服务(一)

TIP

本笔记练习项目,都在该 仓库 (opens new window)

# 搭建商品服务

基于 spring cloud 搭建一个商品服务,跑通基本架构

spring boot + spring cloud + spring mvc + spring + mybatis

创建新项目 eshop-product-service (基础搭建请参考上一章节的 mybatis 整合),端口号是 9100

# 进行业务代码开发说明

这里还是简化,不会带着你去做复查业务建模,上百张表那种,只会简化再简化,能驱动这里的场景即可。

要做的 crud 功能如下

  • 分类管理:增删改查
  • 品牌管理:增删改查
  • 商品基本信息管理:增删改查
  • 商品规格管理:增删改查
  • 商品属性管理:增删改查
  • 商品介绍管理:编辑

大概会在 170 讲之前都不会使用 cloud 中的其他生态服务,就是很简单的使用一个服务注册 + boot 来开发业务代码, 业务代码写完之后再来搞那一套的生态

创建 3 张表

public class Brand {
    private Long id;
    private String name;
    private String description;
}

public class Category {
    private Long id;
    private String name;
    private String description;
}

public class Product {
    private Long id;
    private String name;
    private Long categoryId;
    private Long brandId;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

实现上面的的功能,这里记录下全注解开发的(本人第一次接触),这里粘贴出来其中一个 mapper 的写法

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.mrcode.cache.eshop.productserver.model.Product;

@Mapper
public interface ProductMapper {

    @Insert("INSERT INTO product(name,category_id,brand_id) VALUES(#{name},#{categoryId},#{brandId})")
    void add(Product product);

    @Update("UPDATE product SET name=#{name},category_id=#{categoryId},brand_id=#{brandId} WHERE id=#{id}")
    void update(Product product);

    @Delete("DELETE FROM product WHERE id=#{id}")
    void delete(Long id);

    @Select("SELECT * FROM product WHERE id=#{id}")
    @Results({
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "brand_id", property = "brandId")
    })
    Product findById(Long id);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

service 就不记录了,特别简单的转调

controller 展示其中一个


@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/add")
    public String add(Product product) {
        try {
            productService.add(product);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    @RequestMapping("/update")
    public String update(Product product) {
        try {
            productService.update(product);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    @RequestMapping("/delete")
    public String delete(Long id) {
        try {
            productService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    @RequestMapping("/findById")
    public Product findById(Long id) {
        try {
            return productService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Product();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

写完来测试几个功能:

  • 插入分类:http://localhost:9100/category/add?name=手机&description=电子类
  • 查询分类:http://localhost:9100/category/findById?id=1