詳解Redis在SpringBoot工程中的綜合應用
業(yè)務描述
從一個博客數(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工程的原有依賴基礎上添加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è)務邏輯代碼設計及實現(xiàn)
Domain對象設計
創(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;
/**
* 標簽類的設計
*/
@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 邏輯對象設計
創(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中的相關方法進行單元測試,例如:
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 邏輯對象設計
設計TagService接口及實現(xiàn)類,定義Tag(標簽)業(yè)務邏輯。
第一步:定義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邏輯對象設計
創(chuàng)建Tag控制邏輯對象,用于處理請求和響應邏輯,代碼如下:
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
}
}
啟動服務,打開瀏覽器進行訪問測試。同時思考,我們是否可以在這個層加一個本地cache。
總結(jié)(Summary)
本章節(jié)重點是學習項目中緩存(Cache)的一種應用思想。
到此這篇關于Redis在SpringBoot工程中的綜合應用的文章就介紹到這了,更多相關Redis在SpringBoot綜合應用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Redis在計數(shù)器和人員記錄的事務操作應用小結(jié)
Redis是一個高性能的鍵值存儲系統(tǒng),專于處理計數(shù)器和事務操作,它提供了INCR、DECR等命令來進行原子遞增或遞減操作,并通過MULTI、EXEC等命令實現(xiàn)事務操作,此外,Redis的Pipeline功能可減少網(wǎng)絡往返次數(shù),提高性能2024-10-10
Redis使用bloom-filter過濾器實現(xiàn)推薦去重
這篇文章主要介紹了Redis使用bloom-filter過濾器實現(xiàn)推薦去重,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11
Redis高級數(shù)據(jù)類型Hyperloglog、Bitmap的使用
很多小伙伴在面試中都會被問道 Redis的常用數(shù)據(jù)結(jié)構有哪些?可能很大一部分回答都是 string、hash、list、set、zset,但其實還有Hyperloglog和Bitmap,本文就來介紹一下2021-05-05
Redis報錯UnrecognizedPropertyException: Unrecognized 
在使用SpringBoot訪問Redis時,報錯提示識別不了屬性headPart,經(jīng)過排查,發(fā)現(xiàn)并非Serializable或getset方法問題,而是存在一個方法getHeadPart,但無headPart屬性,解決方案是將getHeadPart改為makeHeadPart2024-10-10

