詳解Redis在SpringBoot工程中的綜合應(yīng)用
業(yè)務(wù)描述
從一個(gè)博客數(shù)據(jù)庫(kù)中查詢所有的文章標(biāo)簽,然后存儲(chǔ)到緩存(Cache),后續(xù)查詢時(shí)可從緩存獲取。提高其查詢性能。
準(zhǔn)備工作
初始化數(shù)據(jù)
初始化數(shù)據(jù)庫(kù)中數(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");
添加項(xiàng)目依賴
在jt-template工程的原有依賴基礎(chǔ)上添加mysql數(shù)據(jù)庫(kù)訪問(wèn)依賴,例如:
<!--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ù)庫(kù)訪問(wèn)配置
在項(xiàng)目的配置文件(例如application.yml)中添加數(shù)據(jù)庫(kù)訪問(wèn)配置,例如:
spring: datasource: url: jdbc:mysql:///blog?serverTimezone=Asia/Shanghai&characterEncoding=utf8 username: root password: root
業(yè)務(wù)邏輯代碼設(shè)計(jì)及實(shí)現(xiàn)
Domain對(duì)象設(shè)計(jì)
創(chuàng)建一個(gè)Tag類,基于此類型的對(duì)象存儲(chǔ)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è)計(jì) */ @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 邏輯對(duì)象設(shè)計(jì)
創(chuàng)建Tag信息的數(shù)據(jù)訪問(wèn)接口,代碼如下:
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)建單元測(cè)試類,TagMapper中的相關(guān)方法進(jìn)行單元測(cè)試,例如:
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 邏輯對(duì)象設(shè)計(jì)
設(shè)計(jì)TagService接口及實(shí)現(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沒(méi)有獲取tag信息,查詢mysql tags = tagMapper.selectList(null); //3.將從mysql查詢到tag信息存儲(chǔ)到redis valueOperations.set("tags", tags); //4.返回查詢結(jié)果 return tags; } }
說(shuō)明,假如將List存儲(chǔ)到redis,此時(shí)Tag必須實(shí)現(xiàn)Serializable接口。
第三步:定義TagServiceTests單元測(cè)試類并進(jìn)行單元測(cè)試,代碼如下:
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邏輯對(duì)象設(shè)計(jì)
創(chuàng)建Tag控制邏輯對(duì)象,用于處理請(qǐng)求和響應(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 } }
啟動(dòng)服務(wù),打開(kāi)瀏覽器進(jìn)行訪問(wèn)測(cè)試。同時(shí)思考,我們是否可以在這個(gè)層加一個(gè)本地cache。
總結(jié)(Summary)
本章節(jié)重點(diǎn)是學(xué)習(xí)項(xiàng)目中緩存(Cache)的一種應(yīng)用思想。
到此這篇關(guān)于Redis在SpringBoot工程中的綜合應(yīng)用的文章就介紹到這了,更多相關(guān)Redis在SpringBoot綜合應(yīng)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis基本數(shù)據(jù)類型哈希Hash常用操作命令
這篇文章主要為大家介紹了Redis基本數(shù)據(jù)類型哈希Hash常用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Redis底層數(shù)據(jù)結(jié)構(gòu)SkipList的實(shí)現(xiàn)
本文主要介紹了Redis底層數(shù)據(jù)結(jié)構(gòu)SkipList的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializ
本文主要介紹了Jackson2JsonRedisSerializer和GenericJackson2JsonRedisSerializer區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04redis客戶端實(shí)現(xiàn)高可用讀寫(xiě)分離的方式詳解
基于sentienl 獲取和動(dòng)態(tài)感知 master、slaves節(jié)點(diǎn)信息的變化,我們的讀寫(xiě)分離客戶端就能具備高可用+動(dòng)態(tài)擴(kuò)容感知能力了,接下來(lái)通過(guò)本文給大家分享redis客戶端實(shí)現(xiàn)高可用讀寫(xiě)分離的方式,感興趣的朋友一起看看吧2021-07-07簡(jiǎn)單聊一聊redis過(guò)期時(shí)間的問(wèn)題
在使用redis的過(guò)期時(shí)間時(shí)不由想到設(shè)置了過(guò)期時(shí)間,下面這篇文章主要給大家介紹了關(guān)于redis過(guò)期時(shí)間問(wèn)題的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04