多级缓存架构(一)项目初始化

文章目录

一、项目克隆

克隆此项目到本地
https://github.com/Xiamu-ssr/MultiCache

来到start目录下,分别有以下文件夹

  • docker:docker相关文件
  • item-service:springboot项目

二、数据库准备

docker/docker-compose.yml中已经定义好如下mysql

yaml 复制代码
  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如下

bash 复制代码
[mysqld]
bind-address=0.0.0.0
skip-name-resolve
character_set_server=utf8
datadir=/var/lib/mysql

运行以下命令启动docker-compose

bash 复制代码
docker-compose -p multi-cache up -d

之后使用数据库连接工具连接mysql容器,创建heima数据库,并对其执行docker/mysql/item.sql脚本。

三、项目工程准备

idea打开item-service文件夹,等待idea加载本springboot项目。

如果在docker-compose中服务ip改动,请注意一些可能关联的地方也需要做同样改动,比如item-serviceapplication.yml

yaml 复制代码
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

java 复制代码
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);
    }
}
相关推荐
Lill_bin19 分钟前
报表生成---JFreeChart
docker·信息可视化·zookeeper·云原生·容器·jenkins
warrah3 小时前
mysql主从同步
mysql·主从
JSON_L4 小时前
MySQL 事务处理
数据库·mysql
别挡6 小时前
CentOS Stream 8中安装和使用 Docker
linux·docker·centos
飞翔沫沫情6 小时前
mysql快速定位cpu 占比过高的sql语句
mysql
huaqianzkh7 小时前
使用 Parallel 类进行多线程编码(上)
开发语言·架构
King.6247 小时前
SQLynx如何提高企业数据库安全?
数据库·sql·mysql·postgresql·oracle
愤怒的代码7 小时前
Centos使用阿里云镜像安装docker
linux·docker·centos
汪公子4927 小时前
使用docker部署考试系统
docker
ahauedu7 小时前
Spring Boot3项目的常见通用整体架构
spring boot·后端·架构