目录
1.pom文件
2.yml文件
3.创建实体类(要实现序列化接口,否则无法存入redis)
4.config配置文件,直接引入即可,根据情况可修改
5.创建启动类
6.具体操作
6.1 set操作
6.2 get操作
6.3 delete操作
?6.4 列表类型
??6.5?集合类型
?6.6?有序集合类型
6.7 hash类型?
?
1.pom文件
<dependencies>
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.yml文件
spring:
redis:
host: 192.168.16.106
database: 1
port: 6379
3.创建实体类(要实现序列化接口,否则无法存入redis)
@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class User implements Serializable {
private String name;
private int age;
}
4.config配置文件,直接引入即可,根据情况可修改
存入redis的乱码问题配置文件中解决了(起初我用默认的redisTemplate,出现了乱码问题)
@EnableCaching
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL , JsonAutoDetect.Visibility.ANY );
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL );
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setConnectionFactory(factory);
//key 序列化方式
template.setKeySerializer(redisSerializer);
//value 序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap 序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer);
return template;
}
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
Jackson2JsonRedisSerializer(Object. class);
//解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor. ALL , JsonAutoDetect.Visibility. ANY );
om.enableDefaultTyping(ObjectMapper.DefaultTyping. NON_FINAL );
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间 600 秒
RedisCacheConfiguration config =
RedisCacheConfiguration.defaultCacheConfig ()
.entryTtl(Duration.ofSeconds (600))
.serializeKeysWith(RedisSerializationContext.SerializationPair
.fromSerializer (redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer (jackson2JsonRedisSerializer))
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder (factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
5.创建启动类
@SpringBootApplication
public class RedisSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(RedisSpringbootApplication.class, args);
}
}
6.具体操作
6.1 set操作
@RestController
public class UserController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("/set")
public void set(){
User user = new User("张三", 12);
redisTemplate.opsForValue().set("user",user);
}
}
6.2 get操作
@GetMapping("/get/{key}")
public String get(@PathVariable String key){
Object value = redisTemplate.opsForValue().get(key);
return value.toString();
}
6.3 delete操作
@GetMapping("delete/{key}")
public void delete(@PathVariable String key){
Boolean hasKey = redisTemplate.hasKey(key);
if(hasKey){
redisTemplate.delete(key);
}
}
?6.4 列表类型
@GetMapping("list")
public String list(){
ListOperations listOperations = redisTemplate.opsForList();
//从左边添加
listOperations.leftPush("list","left1");
listOperations.leftPush("list","left2");
//从右边添加
listOperations.rightPush("list", "right1");
listOperations.rightPush("list", "right2");
//利用下标来取数据,这里取left和left1
List<String> list = listOperations.range("list", 0, 3);
return list.toString();
}
?
??6.5?集合类型
@GetMapping("/set")
public void setTest(){
SetOperations<String, String> setOperations = redisTemplate.opsForSet();
setOperations.add("k1", "v1");
setOperations.add("k1", "v1");
setOperations.add("k2", "v1");
setOperations.add("k2", "v2");
Set<String> k1 = setOperations.members("k1");
Set<String> k2 = setOperations.members("k2");
System.out.println(k1);
System.out.println(k2);
}
?
?6.6?有序集合类型
@GetMapping("/zset")
public void zsetTest(){
ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("k2", "v1", 3);
zSetOperations.add("k2", "v2", 2);
zSetOperations.add("k2", "v3", 1);
Set<String> zset = zSetOperations.range("k2", 0, 2);
System.out.println(zset);
}
?
6.7 hash类型?
@GetMapping("/hash")
public void hashTest(){
HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
hashOperations.put("k3","hashKey1","v1");
hashOperations.put("k3","hashKey2","v2");
String s1 = hashOperations.get("k3", "hashKey1");
String s2 = hashOperations.get("k3", "hashKey2");
System.out.println(s1);
System.out.println(s2);
}
????????
?
?
?
?
|