1.创建一个springboot项目
2.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.添加application.properties
spring.redis.host=127.0.0.1
spring.redis.database=0
spring.redis.port=6379
spring.redis.password=123456
4.添加Book类
public class Book implements Serializable {
private Integer id;
private String name;
private String author;
}
5.编写controller
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("/test1")
public void test1(){
ValueOperations<String,String> ops1=stringRedisTemplate.opsForValue();
ops1.set("name","三国演义");
String name=ops1.get("name");
System.out.println(name);
ValueOperations ops2=redisTemplate.opsForValue();
Book b1=new Book();
b1.setId(1);
b1.setName("红楼梦");
b1.setAuthor("曹雪芹");
ops2.set("b1",b1);
Book book=(Book) ops2.get("b1");
System.out.println(book);
}