articleList

10-新版 SpringBoot2.X+MybatisPlus+SpringCache 框架项目实战

2025/03/16 posted in  Redis
Tags: 

第 1 集 项目缓存提效神器-SpringCache 缓存框架介绍

简介:SpringCache 缓存框架介绍

  • SpringCache 简介

    • 文档:https://spring.io/guides/gs/caching/

    • 自 Spring 3.1 起,提供了类似于@Transactional 注解事务的注解 Cache 支持,且提供了 Cache 抽象

    • 提供基本的 Cache 抽象,方便切换各种底层 Cache

    • 只需要更少的代码就可以完成业务数据的缓存

    • 提供事务回滚时也自动回滚缓存,支持比较复杂的缓存逻辑

    • 核心

      • 一个是 Cache 接口,缓存操作的 API;
      • 一个是 CacheManager 管理各类缓存,有多个缓存框架的实现
  • 讲课方式

    • 很多其他地方的教程,使用单元测试的方式教 SpringCache 使用
    • 多个同学反馈看了也不懂在 SpringBoot 或者 Cloud 微服务项目中使用
    • 本章内容采用案例实战的方式,教大家怎么使用,工作中开发项目直接采用即可
    • 学会触类旁通,举一反三
  • 使用

    • 项目中引入 starter

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-cache</artifactId>
      </dependency>
      
    • 配置文件指定缓存类型

      spring:
        cache:
          type: redis
      
    • 启动类开启缓存注解

      @EnableCaching
      

第 2 集 SpringBoot2.x 整合 MybatisPlus 连接 Mysql 数据库

简介:整合 MybatisPlus 连接 Mysql 数据库

  • 备注:

  • 添加依赖

    <!--mybatis plus和springboot整合-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.0</version>
      </dependency>
    ​
    <!--数据库驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.15</version>
    </dependency>
    
  • 新增配置

    #==============================数据库相关配置========================================
    #数据库配置
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/xdclass_user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
        username: root
        password: xdclass.net
    ​
    #配置plus打印sql日志
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    
  • 数据库和表建立

    CREATE TABLE `product` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `title` varchar(128) DEFAULT NULL COMMENT '标题',
      `cover_img` varchar(128) DEFAULT NULL COMMENT '封面图',
      `detail` varchar(256) DEFAULT '' COMMENT '详情',
      `amount` int(10) DEFAULT NULL COMMENT '新价格',
      `stock` int(11) DEFAULT NULL COMMENT '库存',
      `create_time` datetime DEFAULT NULL COMMENT '创建时间',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
    

  • 插入数据

    INSERT INTO `product` (`id`, `title`, `cover_img`, `detail`, `amount`, `stock`, `create_time`)
    VALUES
      (1, '老王-小滴课堂抱枕', 'https://file.xdclass.net/video/2020/alibabacloud/zt-alibabacloud.png', 'https://file.xdclass.net/video/2021/60-MLS/summary.jpeg', 213, 100, '2021-09-12 00:00:00'),
      (2, '老王-技术人的杯子Linux', 'https://file.xdclass.net/video/2020/alibabacloud/zt-alibabacloud.png', 'https://file.xdclass.net/video/2021/59-Postman/summary.jpeg', 42, 100, '2021-03-12 00:00:00'),
      (3, '技术人的杯子docker', 'https://file.xdclass.net/video/2020/alibabacloud/zt-alibabacloud.png', 'https://file.xdclass.net/video/2021/60-MLS/summary.jpeg', 12, 20, '2022-09-22 00:00:00'),
      (4, '技术人的杯子git', 'https://file.xdclass.net/video/2020/alibabacloud/zt-alibabacloud.png', 'https://file.xdclass.net/video/2021/60-MLS/summary.jpeg', 14, 20, '2022-11-12 00:00:00');
    
  • DO 类编写

    @TableName("product")
    public class ProductDO  {
    ​
        @TableId(value = "id", type = IdType.AUTO)
        private Long id;
    ​
        /**
        * 标题
        */
        private String title;
    ​
        /**
        * 封面图
        */
        private String coverImg;
    ​
        /**
        * 详情
        */
        private String detail;
    ​
        /**
        * 新价格
        */
        private Integer amount;
    ​
        /**
        * 库存
        */
        private Integer stock;
    ​
        /**
        * 创建时间
        */
        private Date createTime;
    ​
    }
    

第 3 集 SpringBoot+MybatisPlus 开发商品的 CRUD 接口

简介: SpringBoot+MybatisPlus 开发商品列表的 CRUD 接口

  • 编写 Mapper

    public interface ProductMapper extends BaseMapper<ProductDO> {
    }
    
  • 编写 Service

    @Override
    public int save(ProductDO productDO) {
        return productMapper.insert(productDO);
    }
    ​
    @Override
    public int delById(int id) {
        return productMapper.deleteById(id);
    }
    ​
    @Override
    public int updateById(ProductDO productDO) {
        return productMapper.updateById(productDO);
    }
    ​
    @Override
    public ProductDO findById(int id) {
        return productMapper.selectById(id);
    }
    
  • 编写 controller

第 4 集 SpringBoot+MybatisPlus 开发商品分页接口

简介: SpringBoot+MybatisPlus 开发商品分页接口

  • 为啥要开发分页接口?

    • 很多同学不知道分页怎么缓存
  • MybatisPlus 分页接口

    @Override
    public Map<String, Object> page(int page, int size) {
    ​
        Page<ProductDO> pageInfo = new Page<>(page, size);
    ​
        IPage<ProductDO> productDOIPage = productMapper.selectPage(pageInfo, null);
    ​
        Map<String, Object> pageMap = new HashMap<>(3);
    ​
        pageMap.put("total_record", productDOIPage.getTotal());
        pageMap.put("total_page", productDOIPage.getPages());
        pageMap.put("current_data", productDOIPage.getRecords());
    ​
        return pageMap;
    }
    
  • controller 编写

  • 分页插件配置

    /**
     * 新的分页插件
      */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }