1. 程式人生 > 其它 >第十章 新版SpringBoot2.X+MybatisPlus+SpringCache框架專案實戰

第十章 新版SpringBoot2.X+MybatisPlus+SpringCache框架專案實戰

第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;
   }