一、项目克隆
克隆此项目到本地
https://github.com/Xiamu-ssr/MultiCache
来到start
目录下,分别有以下文件夹
docker
:docker相关文件
item-service
:springboot项目
二、数据库准备
在docker/docker-compose.yml
中已经定义好如下mysql
块
language-yaml1 2 3 4 5 6 7 8 9 10 11 12 13 14
| mysql: container_name: mysql image: mysql:8 volumes: - ./mysql/conf/my.cnf:/etc/mysql/conf.d/my.cnf - ./mysql/data:/var/lib/mysql - ./mysql/logs:/logs ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD=1009 networks: multi-cache: ipv4_address: 172.30.3.2
|
my.cnf
如下
language-bash1 2 3 4 5
| [mysqld] bind-address=0.0.0.0 skip-name-resolve character_set_server=utf8 datadir=/var/lib/mysql
|
运行以下命令启动docker-compose
language-bash1
| docker-compose -p multi-cache up -d
|
之后使用数据库连接工具连接mysql
容器,创建heima
数据库,并对其执行docker/mysql/item.sql
脚本。
三、项目工程准备
用idea
打开item-service
文件夹,等待idea
加载本springboot
项目。
如果在docker-compose
中服务ip改动,请注意一些可能关联的地方也需要做同样改动,比如item-service
的application.yml
language-yaml1 2 3 4 5 6 7 8
| spring: application: name: itemservice datasource: url: jdbc:mysql://172.30.3.2:3306/heima?useSSL=false&allowPublicKeyRetrieval=true username: root password: 1009 driver-class-name: com.mysql.cj.jdbc.Driver
|
观察controller
language-java1 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| package com.heima.item.web;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.heima.item.pojo.Item; import com.heima.item.pojo.ItemStock; import com.heima.item.pojo.PageDTO; import com.heima.item.service.IItemService; import com.heima.item.service.IItemStockService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.stream.Collectors;
@RestController @RequestMapping("item") public class ItemController {
@Autowired private IItemService itemService; @Autowired private IItemStockService stockService;
@GetMapping("list") public PageDTO queryItemPage( @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "5") Integer size){ // 分页查询商品 Page<Item> result = itemService.query() .ne("status", 3) .page(new Page<>(page, size));
// 查询库存 List<Item> list = result.getRecords().stream().peek(item -> { ItemStock stock = stockService.getById(item.getId()); item.setStock(stock.getStock()); item.setSold(stock.getSold()); }).collect(Collectors.toList());
// 封装返回 return new PageDTO(result.getTotal(), list); }
@PostMapping public void saveItem(@RequestBody Item item){ itemService.saveItem(item); }
@PutMapping public void updateItem(@RequestBody Item item) { itemService.updateById(item); }
@PutMapping("stock") public void updateStock(@RequestBody ItemStock itemStock){ stockService.updateById(itemStock); }
@DeleteMapping("/{id}") public void deleteById(@PathVariable("id") Long id){ itemService.update().set("status", 3).eq("id", id).update(); }
@GetMapping("/{id}") public Item findById(@PathVariable("id") Long id){ return itemService.query() .ne("status", 3).eq("id", id) .one(); }
@GetMapping("/stock/{id}") public ItemStock findStockById(@PathVariable("id") Long id){ return stockService.getById(id); } }
|