1、建立spring boot项目,添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2、配置文件:
spring: redis: host: ubu port: 6379 password: 123456 #Redis数据库索引(默认为0) database: 0 #连接超时时间(毫秒) connect-timeout: 1800000 #客户端底层连接方式:lettuce(默认)、jedis二选一 client-type: lettuce lettuce: pool: #连接池最大连接数(使用负值表示没有限制) max-active: 20 #最大阻塞等待时间(负数表示没限制) max-wait: -1 #连接池中的最大空闲连接 max-idle: 5 #连接池中的最小空闲连接 min-idle: 0
3、将Lettuce切换为Jedis
在pom文件中添加jedis引用
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
在配置文件中设置:
client-type: jedis
4、建立测试类:
1 package com.example.demo02; 2 3 import org.junit.jupiter.api.Test; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.boot.test.context.SpringBootTest; 6 import org.springframework.data.redis.connection.RedisConnectionFactory; 7 import org.springframework.data.redis.core.*; 8 import redis.clients.jedis.BitOP; 9 10 @SpringBootTest 11 public class RedisTest { 12 13 @Autowired 14 StringRedisTemplate redisTemplate; 15 16 17 18 //读写String 19 @Test 20 void testRedis01(){ 21 //HashOperations<String,String,String>:操作hash类型 22 //ListOperations<String,String>:操作list类型 23 //SetOperations<String,String>:操作set类型 24 //ZSetOperations<String,String>:操作zset类型 25 26 //HyperLogLogOperations<String,String> 27 //GeoOperations 28 29 ValueOperations<String, String> operations = redisTemplate.opsForValue(); 30 operations.set("hello","world"); 31 final String hello = operations.get("hello"); 32 System.out.println(hello); 33 } 34 35 @Autowired 36 RedisConnectionFactory factory; 37 38 @Test 39 void testFactory(){ 40 System.out.println(factory.getClass()); 41 } 42 }