SpringBoot整合Redis
1.新建springboot工程
【注:springboot在整合redis时会自动封装了两个类:RedisTemplate和StringRedisTemplate. StringRedisTemplate它是RedisTemplate的子类,StringRedisTemplate它里面存储的key和value都是字符串类型】
新建springboot工程,其中勾选依赖包,web,以及NOSQL中的第一个依赖【Redis】
或者跳过直接复制以下依赖到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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.修改application配置文件
# 连接单机redis
spring.redis.host=192.168.223.158
spring.redis.port=6379
3.使用StringRedisTemplate
打开测试文件中:
我们注入
@Autowired
RedisTemplate redisTemplate;
【注:springboot整合了redis,命令被封装成了一个个方法,但是这些方法名与命令有很大不同】
【注:springboot不再提供直接操作数据库的命令,我们知道,在Redis中有着select、flushdb、flushall,这些直接操作数据库的命令】
【注:springboot间接的为我们提供了各个类型的操作方式,如string类型有操作string类型的类】
4.匹配Redis命令
key-Redis命令 | key-springboot | |
1 | keys * | keys(“*”) |
exists key | hsskey(“key”) | |
string-Redis命令 | string-springboot | |
1 | set key,value | set(key,value) |
2 |
get key mget key key key ...n |
get(key) get(key,key,key...n) |
3 |
setnx key |
setIfABsent(key,value) |
4 | expire key seconds |
size(key,value,timeunit) |
5 | incr key |
increment(“key”) |
6 | decr key |
decrease(“key”) |
hash-Redis命令 |
hash-springboot |
|
1 | hset field key value |
put(field,key,value) |
2 | hset field key value key value ...n |
putAll(field,MAp) |
3 | hget field key |
get(field,key) |
4 | hgetall field |
entries(field) |
5 | hkeys key |
keys(“key”) |
6 | hvalus key |
values(“key”) |
List-Redis命令 |
List-springboot |
|
1 | lpush key value value value....n |
leftPushAll(key,value1,value2,value3...n) |
2 | lpop key |
leftPop(“key”) |
3 | lrange key start end |
range(key,start,end) |
set-Redis命令 |
set-springboot |
|
1 | sadd key value value value...n |
add(key,value,value,value...n) |
2 | smembers key |
members(“key”) |
3 |
size(“key”) |
|
4 | srandmember key |
randomMember(“key”) |
5 | spop key |
pop(“key”) |
6 | sinter key1 key2 |
intersect(“key1”,“key2”) |
sorted set-Redis命令 |
sorted set-springboot |
|
1 | zadd keyname key score key score...n |
add(keyname,key1 score) add(keyname,set) |
2 | zrange key start end |
range(“key”,start,end) |
3 | zrevrange key start end |
reverseRange(key,start,end) |
【注:其上部分命令已经补充完成,更多详细信息请看以下链接:】
【key,String,List,Hash,Set Redis详细命令】
5.
原因是因为,其在存入时它默认以JDK序列化方式,但是这种方式我们难以看懂,所以需要认为指定序列化方式:
新建config包,在其中新建RedisTemplateConfig文件
复制以下代码:
@Configuration
public class RedisTemplerConfig {
@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;
}
}
到RedisPlus软件中清空数据,到测试类中,随便输入类型数据存入到数据库中
@SpringBootTest
class RedisSpringBootApplicationTests {
@Autowired
RedisTemplate redisTemplate;
@Test
void contextLoads() {
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add("ty","English",50);
zSetOperations.add("ty","math",120);
zSetOperations.add("ty","china",80);
zSetOperations.add("ty","music",48);
System.out.println(zSetOperations.reverseRange("ty", 0, -1));
}
}
运行测试:
指定序列化成功!!!
6.
application文件配置集群连接:
# 集群redis
spring.redis.cluster.nodes=192.168.235.135:7001,\
192.168.235.135:7002,\
192.168.235.135:7003,\
192.168.235.135:7004,\
192.168.235.135:7005,\
192.168.235.135:7006
在RedisPlus软件上进行连接
以上便是SpringBoot整合Redis中的内容,如有漏缺请在下方留言告知,我会及时补充