SpringBoot + Mybatis Plus 整合 Redis的詳細(xì)步驟
Redis 在用戶管理系統(tǒng)中的典型應(yīng)用場景
結(jié)合你的用戶增刪改查接口,以下是 Redis 的實(shí)用場景和具體實(shí)現(xiàn)方案:
| 場景 | 作用 | 實(shí)現(xiàn)方案 |
|---|---|---|
| 用戶信息緩存 | 減少數(shù)據(jù)庫壓力,加速查詢響應(yīng) | 使用 Spring Cache + Redis 注解緩存 |
| 登錄 Token 存儲(chǔ) | 分布式 Session 或 JWT Token 管理 | 將 Token 與用戶信息綁定,設(shè)置過期時(shí)間 |
| 接口限流 | 防止惡意刷接口 | 基于 Redis 計(jì)數(shù)器實(shí)現(xiàn)滑動(dòng)窗口限流 |
| 重復(fù)提交攔截 | 防止用戶重復(fù)提交表單 | 用 Redis 存儲(chǔ)請(qǐng)求唯一標(biāo)識(shí),設(shè)置短期過期 |
| 熱點(diǎn)數(shù)據(jù)預(yù)加載 | 提前緩存高頻訪問數(shù)據(jù) | 定時(shí)任務(wù) + Redis 存儲(chǔ) |
Mac M1 安裝 Redis 詳細(xì)步驟
1. 通過 Homebrew 安裝 Redis
# 安裝 Homebrew(如果尚未安裝) /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # 安裝 Redis brew install redis
2. 啟動(dòng) Redis 服務(wù)
# 前臺(tái)啟動(dòng)(測試用,Ctrl+C 退出) redis-server # 后臺(tái)啟動(dòng)(推薦) brew services start redis
3. 驗(yàn)證安裝
# 連接 Redis 客戶端 redis-cli ping # 應(yīng)返回 "PONG"
Spring Boot 3 整合 Redis
1. 添加依賴
在 pom.xml 中:
<!-- Spring Cache 核心依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Redis 驅(qū)動(dòng) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
2. 配置 Redis 連接
application.yml:
spring:
data:
redis:
host: localhost
port: 6379
# password: your-password # 如果設(shè)置了密碼
lettuce:
pool:
max-active: 8
max-idle: 83. 示例
配置類
package com.example.spring_demo01.config;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
import java.time.Duration;
@Configuration
public class RedisConfig {
// 配置 RedisTemplate
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// Key 序列化
template.setKeySerializer(new StringRedisSerializer());
// Value 序列化為 JSON
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// Hash 結(jié)構(gòu)序列化
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
// 配置緩存管理器
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
.entryTtl(Duration.ofMinutes(30)); // 設(shè)置默認(rèn)過期時(shí)間
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
}
}接口限流工具類
package com.example.spring_demo01.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Component
public class RateLimiter {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public boolean allowRequest(String userId) {
String key = "rate_limit:" + userId;
long now = System.currentTimeMillis();
long windowMs = 60_000; // 1 分鐘
// 移除窗口外的請(qǐng)求記錄
redisTemplate.opsForZSet().removeRangeByScore(key, 0, now - windowMs);
// 統(tǒng)計(jì)當(dāng)前窗口內(nèi)請(qǐng)求數(shù)
Long count = redisTemplate.opsForZSet().zCard(key);
if (count != null && count >= 10) {
return false; // 超過限制
}
// 記錄本次請(qǐng)求
redisTemplate.opsForZSet().add(key, UUID.randomUUID().toString(), now);
redisTemplate.expire(key, windowMs, TimeUnit.MILLISECONDS);
return true;
}
}實(shí)體類
package com.example.spring_demo01.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import java.io.Serializable;
@Data
@TableName("user")
@JsonIgnoreProperties(ignoreUnknown = true) // 防止 JSON 反序列化問題
public class User implements Serializable { // 實(shí)現(xiàn) Serializable
@TableId(type = IdType.AUTO) // 主鍵自增
private Long id;
private String name;
private Integer age;
private String email;
}service層
package com.example.spring_demo01.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spring_demo01.entity.User;
import com.example.spring_demo01.mapper.UserMapper;
import com.example.spring_demo01.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.io.Serializable;
@Service
public class UserServiceImpl
extends ServiceImpl<UserMapper, User>
implements UserService {
// 對(duì) MyBatis Plus 的 getById 方法添加緩存
@Cacheable(value = "user", key = "#id")
@Override
public User getById(Serializable id) {
return super.getById(id);
}
// 更新時(shí)清除緩存
@CacheEvict(value = "user", key = "#entity.id")
@Override
public boolean updateById(User entity) {
return super.updateById(entity);
}
// 刪除時(shí)清除緩存
@CacheEvict(value = "user", key = "#id")
@Override
public boolean removeById(Serializable id) {
return super.removeById(id);
}
}controller層
package com.example.spring_demo01.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spring_demo01.annotation.AdminOnly;
import com.example.spring_demo01.entity.User;
import com.example.spring_demo01.service.UserService;
import com.example.spring_demo01.utils.RateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.time.Duration;
import java.util.List;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private RateLimiter rateLimiter;
// ------------------------------ 增 ------------------------------
@PostMapping
public String addUser(@RequestBody User user, @RequestHeader String clientId) {
String key = "SUBMIT_LOCK:" + clientId + ":" + user.hashCode();
// 10秒內(nèi)不允許重復(fù)提交
Boolean success = redisTemplate.opsForValue()
.setIfAbsent(key, "", Duration.ofSeconds(10));
if (Boolean.FALSE.equals(success)) {
throw new RuntimeException("請(qǐng)勿重復(fù)提交");
}
userService.save(user);
return "新增成功";
}
// ------------------------------ 刪 ------------------------------
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable Long id) {
userService.removeById(id);
return "刪除成功";
}
@DeleteMapping("/batch")
public String deleteBatch(@RequestBody List<Long> ids) {
userService.removeByIds(ids);
return "批量刪除成功";
}
// ------------------------------ 改 ------------------------------
@PutMapping
public String updateUser(@RequestBody User user) {
userService.updateById(user);
return "更新成功";
}
// ------------------------------ 查 ------------------------------
@GetMapping("/{id}")
@AdminOnly
public User getUserById(@PathVariable Long id) {
return userService.getById(id);
}
@GetMapping("/list")
public List<User> listUsers(
@RequestParam(required = false) String name,
@RequestParam(required = false) Integer age) {
QueryWrapper<User> wrapper = new QueryWrapper<>();
if (name != null) {
wrapper.like("name", name); // 模糊查詢姓名
}
if (age != null) {
wrapper.eq("age", age); // 精確查詢年齡
}
return userService.list(wrapper);
}
@GetMapping("/page")
public Page<User> pageUsers(
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestHeader(value = "Authorization") String token) {
// 從 Token 中獲取用戶ID
log.info("token:{}", token);
log.info("User:{}", redisTemplate.opsForValue().get(token.split(" ")[1]));
User user = (User) redisTemplate.opsForValue().get(token.split(" ")[1]);
if (user == null) throw new RuntimeException("未登錄");
// 限流校驗(yàn)
if (!rateLimiter.allowRequest("PAGE_" + user.getId())) {
throw new RuntimeException("請(qǐng)求過于頻繁");
}
return userService.page(new Page<>(pageNum, pageSize));
}
// ------------------------------ other ------------------------------
@GetMapping("/error")
public String getError() {
throw new RuntimeException();
}
@PostMapping("/login")
public String login(@RequestBody User user) {
log.info("login user:{}", user);
// 驗(yàn)證用戶邏輯(示例簡化)
User dbUser = userService.getOne(new QueryWrapper<User>()
.eq("id", user.getId())
.eq("name", user.getName()));
if (dbUser == null) {
throw new RuntimeException("登錄失敗");
}
// 生成 Token 并存儲(chǔ)
String token = "TOKEN_" + UUID.randomUUID();
redisTemplate.opsForValue().set(
token,
dbUser,
Duration.ofMinutes(30)
);
return token;
}
}在啟動(dòng)類添加注解:
package com.example.spring_demo01;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@MapperScan("com.example.spring_demo01.mapper")
@ServletComponentScan // 啟用 Servlet 組件掃描(如 Filter、Servlet)
@EnableCaching // 啟動(dòng)緩存,Redis使用
public class SpringDemo01Application {
public static void main(String[] args) {
SpringApplication.run(SpringDemo01Application.class, args);
}
}常見問題排查
Q1: 連接 Redis 超時(shí)
- 檢查服務(wù)狀態(tài):運(yùn)行
redis-cli ping確認(rèn) Redis 是否正常運(yùn)行 - 查看端口占用:
lsof -i :6379 - 關(guān)閉防火墻:
sudo ufw allow 6379
Q2: Spring Boot 無法注入 RedisTemplate
- 確認(rèn)配置類:添加
@EnableCaching和@Configuration - 檢查序列化器:顯式配置序列化方式避免 ClassCastException
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}總結(jié)
通過 Redis 你可以為項(xiàng)目快速實(shí)現(xiàn):
- 高性能緩存層 - 降低數(shù)據(jù)庫負(fù)載
- 分布式會(huì)話管理 - 支持橫向擴(kuò)展
- 精細(xì)化流量控制 - 保障系統(tǒng)穩(wěn)定性
到此這篇關(guān)于SpringBoot + Mybatis Plus 整合 Redis的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)SpringBoot Mybatis Plus 整合 Redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決idea配置Tomcat Deployment沒有artifact選項(xiàng)的問題
今天在配置的時(shí)候tomcat deployment中卻找不到artifact,沒有artifact就不能打成war包上傳到服務(wù)器了,那么怎么解決沒有artifact選項(xiàng)的問題呢,今天通過本文給大家分享idea配置Tomcat Deployment沒有artifact選項(xiàng)的解決方案,一起看看吧2023-10-10
深入剖析Java中Map.Entry的方法與實(shí)戰(zhàn)應(yīng)用
在Java集合框架中,Map.Entry扮演著連接鍵值對(duì)的橋梁角色,作為Map接口的內(nèi)部接口,它封裝了鍵值對(duì)的本質(zhì),是高效處理映射數(shù)據(jù)的核心工具,下面我我們就來深入剖析Map.Entry的概念,方法及實(shí)戰(zhàn)應(yīng)用2025-06-06
使用MAT進(jìn)行JVM內(nèi)存分析實(shí)例
這篇文章主要介紹了使用MAT進(jìn)行JVM內(nèi)存分析實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
詳解Java?redis中緩存穿透?緩存擊穿?雪崩三種現(xiàn)象以及解決方法
緩存穿透是指緩存和數(shù)據(jù)庫中都沒有的數(shù)據(jù),而用戶不斷發(fā)起請(qǐng)求,如發(fā)起為id為“-1”的數(shù)據(jù)或id為特別大不存在的數(shù)據(jù)。這時(shí)的用戶很可能是攻擊者,攻擊會(huì)導(dǎo)致數(shù)據(jù)庫壓力過大2022-01-01
淺談SpringMVC中的session用法及細(xì)節(jié)記錄
下面小編就為大家?guī)硪黄獪\談SpringMVC中的session用法及細(xì)節(jié)記錄。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
學(xué)習(xí)不同 Java.net 語言中類似的函數(shù)結(jié)構(gòu)
這篇文章主要介紹了學(xué)習(xí)不同 Java.net 語言中類似的函數(shù)結(jié)構(gòu),函數(shù)式編程語言包含多個(gè)系列的常見函數(shù)。但開發(fā)人員有時(shí)很難在語言之間進(jìn)行切換,因?yàn)槭煜さ暮瘮?shù)具有不熟悉的名稱。函數(shù)式語言傾向于基于函數(shù)范例來命名這些常見函數(shù)。,需要的朋友可以參考下2019-06-06

