人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

詳解Redis在SpringBoot工程中的綜合應(yīng)用

發(fā)布日期:2022-01-29 20:06 | 文章來源:CSDN

業(yè)務(wù)描述

從一個博客數(shù)據(jù)庫中查詢所有的文章標簽,然后存儲到緩存(Cache),后續(xù)查詢時可從緩存獲取。提高其查詢性能。

準備工作

初始化數(shù)據(jù)

初始化數(shù)據(jù)庫中數(shù)據(jù),SQL腳本如下:

DROP DATABASE IF EXISTS `blog`;
CREATE DATABASE `blog` DEFAULT character set utf8mb4;
SET names utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
USE `blog`;
CREATE TABLE `tb_tag` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `name` varchar(255) NOT NULL COMMENT 'data_id',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tb_tag';
insert into `tb_tag` values (null,"mysql"),(null,"redis");

添加項目依賴

在jt-template工程的原有依賴基礎(chǔ)上添加mysql數(shù)據(jù)庫訪問依賴,例如:

<!--mysql-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

添加數(shù)據(jù)庫訪問配置

在項目的配置文件(例如application.yml)中添加數(shù)據(jù)庫訪問配置,例如:

spring:
  datasource:
    url: jdbc:mysql:///blog?serverTimezone=Asia/Shanghai&characterEncoding=utf8
    username: root
    password: root

業(yè)務(wù)邏輯代碼設(shè)計及實現(xiàn)

Domain對象設(shè)計

創(chuàng)建一個Tag類,基于此類型的對象存儲Tag(標簽信息),代碼如下:

package com.jt.blog.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
/**
 * 標簽類的設(shè)計
 */
@TableName("tb_tag")
public class Tag implements Serializable {
    private static final long serialVersionUID = 4504013456197711455L;
    /**標簽id*/
    @TableId(type = IdType.AUTO)
    private Long id;
    /**標簽名*/
    private String name;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Tag{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Dao 邏輯對象設(shè)計

創(chuàng)建Tag信息的數(shù)據(jù)訪問接口,代碼如下:

package com.jt.blog.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jt.blog.domain.Tag;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TagMapper
        extends BaseMapper<Tag> {
}

創(chuàng)建單元測試類,TagMapper中的相關(guān)方法進行單元測試,例如:

package com.jt.blog.dao;
import com.jt.blog.domain.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class TagMapperTests {
    @Autowired
    private TagMapper tagMapper;
    @Test
    void testSelectList(){
        List<Tag> tags =
        tagMapper.selectList(null);
        for(Tag t:tags){
            System.out.println(t);
            //System.out.println(t.getId()+"/"+t.getName());
        }
    }
}

Service 邏輯對象設(shè)計

設(shè)計TagService接口及實現(xiàn)類,定義Tag(標簽)業(yè)務(wù)邏輯。
第一步:定義TagService接口,代碼如下:

package com.jt.blog.service;
import com.jt.blog.domain.Tag;
import java.util.List;
public interface TagService {
    /**
     * 查詢所有的標簽
     * @return
     */
    List<Tag> selectTags();
}

第二步:定義TagServiceImpl類,代碼如下:

package com.jt.blog.service.impl;
import com.jt.blog.dao.TagMapper;
import com.jt.blog.domain.Tag;
import com.jt.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TagServiceImpl implements TagService {
    //RedisAutoConfiguration 類中做的RedisTemplate的配置
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TagMapper tagMapper;
    @Override
    public List<Tag> selectTags() {
        //1.從redis查詢Tag信息,redis有則直接返回
        ValueOperations<String,List<Tag>> valueOperations =
        redisTemplate.opsForValue();
        List<Tag> tags=valueOperations.get("tags");
        if(tags!=null&&!tags.isEmpty())return tags;
        //2.從redis沒有獲取tag信息,查詢mysql
        tags = tagMapper.selectList(null);
        //3.將從mysql查詢到tag信息存儲到redis
        valueOperations.set("tags", tags);
        //4.返回查詢結(jié)果
        return tags;
    }
}

說明,假如將List存儲到redis,此時Tag必須實現(xiàn)Serializable接口。

第三步:定義TagServiceTests單元測試類并進行單元測試,代碼如下:

package com.jt.blog.service;
import com.jt.blog.domain.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class TagServiceTests {
    @Autowired
    private TagService tagService;
    
    @Test
    void testSelectTags(){
        List<Tag> tags=
        tagService.selectTags();
        System.out.println(tags);
    }
}

Controller邏輯對象設(shè)計

創(chuàng)建Tag控制邏輯對象,用于處理請求和響應(yīng)邏輯,代碼如下:

package com.jt.blog.controller;
import com.jt.blog.domain.Tag;
import com.jt.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    private TagService tagService;
    
    @GetMapping
    public  List<Tag> doSelectTags(){
      return  tagService.selectTags());//1.redis,2.mysql
    }
}

啟動服務(wù),打開瀏覽器進行訪問測試。同時思考,我們是否可以在這個層加一個本地cache。

總結(jié)(Summary)

本章節(jié)重點是學(xué)習(xí)項目中緩存(Cache)的一種應(yīng)用思想。

到此這篇關(guān)于Redis在SpringBoot工程中的綜合應(yīng)用的文章就介紹到這了,更多相關(guān)Redis在SpringBoot綜合應(yīng)用內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

香港服務(wù)器租用

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部