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 存儲 | 分布式 Session 或 JWT Token 管理 | 將 Token 與用戶信息綁定,設(shè)置過期時間 |
| 接口限流 | 防止惡意刷接口 | 基于 Redis 計數(shù)器實(shí)現(xiàn)滑動窗口限流 |
| 重復(fù)提交攔截 | 防止用戶重復(fù)提交表單 | 用 Redis 存儲請求唯一標(biāo)識,設(shè)置短期過期 |
| 熱點(diǎn)數(shù)據(jù)預(yù)加載 | 提前緩存高頻訪問數(shù)據(jù) | 定時任務(wù) + Redis 存儲 |
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. 啟動 Redis 服務(wù)
# 前臺啟動(測試用,Ctrl+C 退出) redis-server # 后臺啟動(推薦) 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ū)動 -->
<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)過期時間
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 分鐘
// 移除窗口外的請求記錄
redisTemplate.opsForZSet().removeRangeByScore(key, 0, now - windowMs);
// 統(tǒng)計當(dāng)前窗口內(nèi)請求數(shù)
Long count = redisTemplate.opsForZSet().zCard(key);
if (count != null && count >= 10) {
return false; // 超過限制
}
// 記錄本次請求
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 {
// 對 MyBatis Plus 的 getById 方法添加緩存
@Cacheable(value = "user", key = "#id")
@Override
public User getById(Serializable id) {
return super.getById(id);
}
// 更新時清除緩存
@CacheEvict(value = "user", key = "#entity.id")
@Override
public boolean updateById(User entity) {
return super.updateById(entity);
}
// 刪除時清除緩存
@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("請勿重復(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("請求過于頻繁");
}
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 并存儲
String token = "TOKEN_" + UUID.randomUUID();
redisTemplate.opsForValue().set(
token,
dbUser,
Duration.ofMinutes(30)
);
return token;
}
}在啟動類添加注解:
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 // 啟動緩存,Redis使用
public class SpringDemo01Application {
public static void main(String[] args) {
SpringApplication.run(SpringDemo01Application.class, args);
}
}常見問題排查
Q1: 連接 Redis 超時
- 檢查服務(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ù)載
- 分布式會話管理 - 支持橫向擴(kuò)展
- 精細(xì)化流量控制 - 保障系統(tǒng)穩(wěn)定性
到此這篇關(guān)于SpringBoot + Mybatis Plus 整合 Redis的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)SpringBoot Mybatis Plus 整合 Redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Netty進(jìn)階之EventExecutorGroup源碼詳解
這篇文章主要介紹了Netty進(jìn)階之EventExecutorGroup源碼詳解,EventExecutorGroup繼承了JDK的ScheduledExecutroService,那么它就擁有了執(zhí)行定時任務(wù),執(zhí)行提交的普通任務(wù),需要的朋友可以參考下2023-11-11
Java對象和Json文本轉(zhuǎn)換工具類的實(shí)現(xiàn)
Json?是一個用于Java對象和Json文本相互轉(zhuǎn)換的工具類,本文主要介紹了Java對象和Json文本轉(zhuǎn)換工具類,具有一定的參考價值,感興趣的可以了解一下2022-03-03
Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程
這篇文章主要介紹了Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Springboot獲取前端反饋信息并存入數(shù)據(jù)庫的實(shí)現(xiàn)代碼
這篇文章主要介紹了Springboot獲取前端反饋信息并存入數(shù)據(jù)庫的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
利用Spring Social輕松搞定微信授權(quán)登錄的方法示例
這篇文章主要介紹了利用Spring Social輕松搞定微信授權(quán)登錄的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

