引入了web和redis依赖
<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> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
/**
* @author hrui
* @date 2024/8/5 14:17
*/
@RestController
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/setStr")
public String setStr() {
bookService.setStr();
return "ok";
}
@GetMapping("/setBook")
public String setBook() {
bookService.setBook();
return "ok";
}
@GetMapping("/getBook")
public Book getBook() {
return bookService.getBook();
}
@GetMapping("/setBookList")
public String setBookList() {
bookService.setBookList();
return "ok";
}
@GetMapping("/getBookList")
public List<Book> getBookList() {
return bookService.getBookList();
}
}
/**
* @author hrui
* @date 2024/8/5 14:17
*/
@Service
public class BookService {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setStr() {
String key = "str";
redisTemplate.opsForValue().set(key, "hrui");
}
public void setBook() {
Book value = new Book("hrui", "springboot");
String key = "book1";
redisTemplate.opsForValue().set(key, value);
}
public Book getBook() {
String key = "book1";
Book book = (Book) redisTemplate.opsForValue().get(key);
return book;
}
public void setBookList() {
List<Book> value = new ArrayList<>();
value.add(new Book("hrui", "springboot"));
value.add(new Book("hrui2", "springboot2"));
String key = "bookList";
redisTemplate.opsForValue().set(key, value);
}
public List<Book> getBookList() {
String key = "bookList";
List<Book> bookList = (List<Book>) redisTemplate.opsForValue().get(key);
return bookList;
}
}
/**
* @author hrui
* @date 2024/8/5 14:16
*/
@Data
@AllArgsConstructor
@NoArgsConstructor//反序列化时候需要无参构造器 在没有配置RestTemplate序列化方式时候必须要有无参构造 且实现Serializable
//配置了RedisTemplate的序列化方式之后 无参构造和实现Serializable可以去掉 但是不建议
public class Book implements Serializable {
private static final long serialVersionUID = 5040794497723191392L;
private String name;
private String author;
}
application.properties中关于Redis的配置
Redis默认用的是JDK序列化
写个配置类
@Configuration
public class RedisConfig {
/**
* 配置 RedisTemplate 序列化方式
*
* @param connectionFactory Redis 连接工厂
* @return 配置好的 RedisTemplate 对象
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// 使用 Jackson2JsonRedisSerializer 进行序列化和反序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
// 配置 ObjectMapper 以便 Jackson 可以处理所有类型的序列化和反序列化
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置 RedisTemplate 的 key 和 value 的序列化规则
// 使用 StringRedisSerializer 进行 key 的序列化和反序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 使用 Jackson2JsonRedisSerializer 进行 value 的序列化和反序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// 设置 RedisTemplate 的 hash key 和 hash value 的序列化规则
// 使用 StringRedisSerializer 进行 hash key 的序列化和反序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// 使用 Jackson2JsonRedisSerializer 进行 hash value 的序列化和反序列化
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
再使用redisTemplate