欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年10月14日 10:00:30   作者:雨田說碼  
這篇文章主要介紹了Redis在SpringBoot工程中的綜合應(yīng)用,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

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

準(zhǔn)備工作

初始化數(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(標(biāo)簽信息),代碼如下:

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;

/**
 * 標(biāo)簽類的設(shè)計
 */
@TableName("tb_tag")
public class Tag implements Serializable {
    private static final long serialVersionUID = 4504013456197711455L;
    /**標(biāo)簽id*/
    @TableId(type = IdType.AUTO)
    private Long id;
    /**標(biāo)簽名*/
    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(標(biāo)簽)業(yè)務(wù)邏輯。
第一步:定義TagService接口,代碼如下:

package com.jt.blog.service;
import com.jt.blog.domain.Tag;
import java.util.List;
public interface TagService {
    /**
     * 查詢所有的標(biāo)簽
     * @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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Redis基本數(shù)據(jù)類型哈希Hash常用操作命令

    Redis基本數(shù)據(jù)類型哈希Hash常用操作命令

    這篇文章主要為大家介紹了Redis基本數(shù)據(jù)類型哈希Hash常用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Redis搶單預(yù)熱的實現(xiàn)示例

    Redis搶單預(yù)熱的實現(xiàn)示例

    本文主要介紹了Redis搶單預(yù)熱的實現(xiàn)示例,以應(yīng)對搶單活動帶來的高并發(fā)訪問壓力,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • Redis底層數(shù)據(jù)結(jié)構(gòu)SkipList的實現(xiàn)

    Redis底層數(shù)據(jù)結(jié)構(gòu)SkipList的實現(xiàn)

    本文主要介紹了Redis底層數(shù)據(jù)結(jié)構(gòu)SkipList的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • redis 集群批量操作實現(xiàn)

    redis 集群批量操作實現(xiàn)

    這篇文章主要介紹了redis 集群批量操作,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • redis序列化及各種序列化情況劃分

    redis序列化及各種序列化情況劃分

    本文主要介紹了redis序列化及各種序列化情況劃分,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializer區(qū)別

    Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializ

    本文主要介紹了Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializer區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Redis哨兵監(jiān)控的使用

    Redis哨兵監(jiān)控的使用

    在Redis集群模式中,哨兵模式是一種常用的方案,本文主要介紹了Redis哨兵監(jiān)控的使用,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • redis客戶端實現(xiàn)高可用讀寫分離的方式詳解

    redis客戶端實現(xiàn)高可用讀寫分離的方式詳解

    基于sentienl 獲取和動態(tài)感知 master、slaves節(jié)點信息的變化,我們的讀寫分離客戶端就能具備高可用+動態(tài)擴容感知能力了,接下來通過本文給大家分享redis客戶端實現(xiàn)高可用讀寫分離的方式,感興趣的朋友一起看看吧
    2021-07-07
  • 使用Grafana監(jiān)控Redis的操作方法

    使用Grafana監(jiān)控Redis的操作方法

    這篇文章主要介紹了使用Grafana監(jiān)控Redis,號稱下一代可視化監(jiān)控系統(tǒng),結(jié)合SpringBoot使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 簡單聊一聊redis過期時間的問題

    簡單聊一聊redis過期時間的問題

    在使用redis的過期時間時不由想到設(shè)置了過期時間,下面這篇文章主要給大家介紹了關(guān)于redis過期時間問題的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04

最新評論