使用Spring Boot整合Redis与Jedis一样需要先引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.5.2</version>
</dependency>
配置数据库连接配置,在application.properties中配置信息,若不配置默认连接本地Redis数据库
# IP地址
spring.redis.host=127.0.0.1
# 端口
spring.redis.port=6379
与Jedis不同,Spring Boot整合Redis后,不能直接使用与Redis数据库中一样的命令进行操作数据库,Spring对其操作方法进行了二次封装,操作数据库,需要注入 Redistemplate
或StringRedistemplate
对象进行操作
// 使用Spring Boot操作redis
@Test
public void testspring(){
User user = new User();
user.setId(99).setName("test").setAge(22);
// 使用Redistemplate进行操作,其通过序列化将对象存储到redis中,存储对象为Java Bean时,需要实现实体类的序列化接口
redistemplate.opsForValue().set("user",user);
User u = (User) redistemplate.opsForValue().get("user");
System.out.println(u);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。