Spring Boot整合Redis注解,实战Redis注解使用

以下是一个基于Spring Boot整合Redis注解实现增删改查功能的实战教程,内容分为配置、实体类、Repository、Service及测试示例:


配置Redis连接

在application.properties或application.yml中添加Redis配置:

properties 复制代码
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0

添加Spring Boot Redis依赖(Maven):

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

定义实体类

使用@RedisHash注解标记实体类,并定义主键:

java 复制代码
@RedisHash("User")
public class User {
    @Id
    private String id;
    private String name;
    private int age;
    // 省略getter/setter
}

创建Repository接口

继承CrudRepository并指定泛型类型:

java 复制代码
public interface UserRepository extends CrudRepository<User, String> {
}

增删改查操作示例

插入数据

通过save方法插入或更新数据:

java 复制代码
@Autowired
private UserRepository userRepository;

public void addUser(User user) {
    userRepository.save(user);
}
查询数据

使用findById或自定义查询方法:

java 复制代码
public User getUserById(String id) {
    return userRepository.findById(id).orElse(null);
}

// 自定义查询(需在Repository中声明方法)
@Indexed
public List<User> findByAge(int age);
删除数据

通过deleteById或对象删除:

java 复制代码
public void deleteUser(String id) {
    userRepository.deleteById(id);
}
更新数据

更新操作与插入相同(自动覆盖):

java 复制代码
public void updateUser(User user) {
    userRepository.save(user);
}

缓存注解应用

在Service层使用@Cacheable等注解实现缓存:

查询缓存
java 复制代码
@Service
public class UserService {
    @Cacheable(value = "users", key = "#id")
    public User getUser(String id) {
        // 模拟数据库查询
        return userRepository.findById(id).orElse(null);
    }
}
更新缓存
java 复制代码
@CachePut(value = "users", key = "#user.id")
public User updateUser(User user) {
    return userRepository.save(user);
}
删除缓存
java 复制代码
@CacheEvict(value = "users", key = "#id")
public void deleteUser(String id) {
    userRepository.deleteById(id);
}

测试验证

编写测试类验证功能:

java 复制代码
@SpringBootTest
public class RedisTest {
    @Autowired
    private UserService userService;

    @Test
    public void testCrud() {
        User user = new User();
        user.setId("1");
        user.setName("Alice");
        userService.addUser(user);

        User cachedUser = userService.getUser("1");
        Assertions.assertNotNull(cachedUser);
    }
}

关键注解说明

  • @RedisHash:定义Redis中的存储哈希名称
  • @Id:标记主键字段
  • @Indexed:为字段创建二级索引
  • @Cacheable:查询时优先读取缓存
  • @CachePut:更新数据时同步更新缓存
  • @CacheEvict:删除数据时清除缓存

通过以上步骤即可实现Spring Boot与Redis注解的整合操作。注意缓存注解需在启动类添加@EnableCaching启用缓存功能。

相关推荐
谢亮_vipxieliang3 小时前
Spring Cloud 服务治理入门:注册发现、配置中心、网关限流、熔断降级与分布式一致性方案
分布式·spring·spring cloud
卷无止境5 小时前
独立开发者的"富矿地带":哪些垂直领域值得你押注一辈子?
后端·python
谢亮_vipxieliang5 小时前
Spring 事务失效的常见场景
java·开发语言·数据库·spring boot
狼爷5 小时前
从零用 Java 构建 AI Agent 框架:JavaManus 设计与实现深度解析
后端·langchain·aigc
郑州光合科技余经理5 小时前
海外版外卖加盟:总站与分站配送规则怎么分开管
java·开发语言·前端·后端·uni-app·php·ai编程
专业程序开发源6 小时前
springboot简历管理系统81389-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·spring·php·课程设计
专业程序开发源6 小时前
springboot社区养老系统44071-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·php·课程设计
小呆呆6666 小时前
副业搞起来,小说,漫画,漫剧的成本优化思路
前端·后端·面试
周杰伦fans6 小时前
8GB显存下模型量化实战指南
人工智能·后端·c#
hweiyu007 小时前
Redis命令:HTTL
redis·缓存