一、官方教程
官方教程: https://docs.spring.io/spring-boot/docs/current/reference/html/data.html#data.nosql.redis.
二、具体操作
1. 环境搭建
- 创建springboot项目
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 设置连接信息
spring.redis.host=192.168.227.131
spring.redis.port=6379
spring.redis.password=
2. 测试
- 实体类
public class User implements Serializable {
private String name;
private String password;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- 测试类
@RestController
public class RedisTestController {
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("/redisTest")
public String testRedis() {
User user = new User();
user.setName("dong");
user.setPassword("123");
ValueOperations ops = redisTemplate.opsForValue();
ops.set("u",user);
User u = (User)ops.get("u");
System.out.println(u);
return "aaaa";
}
}
- 启动springboot 访问 http://localhost:8080/redisTest