<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.8.0version>
dependency>
编写JedisTest类
import org.junit.Test;
import redis.clients.jedis.Jedis;
/**
* create by 刘鸿涛
* 2022/10/4 11:07
*/
@SuppressWarnings({"all"})
public class JedisTest {
@Test
public void testRedis(){
//1.获取连接
Jedis jedis = new Jedis("127.0.0.1",6379);
//2.执行具体操作
jedis.set("username","xiaoming");
//关闭连接
jedis.close();
}
}
<dependecy>
<groupId>org.springframework.bootgroup>
<artifactId>spring-boot-starter-data-redisartifactId>
dependecy>
导入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
配置yml
spring:
application:
name: springdataredis_demo
#Redis相关配置
redis:
host: 127.0.0.1
port: 6379
#password: 123456
database: 0 #操作的是0号数据库,默认配置文件中分配了16个数据库
jedis:
#Redis连接池配置
pool:
max-active: 8 #最大连接数
max-wait: 1ms #连接池最大阻塞等待时间
max-idle: 4 #连接池中最大空闲连接
min-idle: 0 #连接池中的最小空闲连接
使get city能查到
import org.springframework.cache.annotation.CachingConfigurationSelector;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* create by 刘鸿涛
* 2022/10/4 11:56
*/
@SuppressWarnings({"all"})
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object,Object> redisTemplate (RedisConnectionFactory connectionFactory){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
/**
* 操作String类型数据
*/
@Test
public void testString(){
// ValueOperations valueOperations = redisTemplate.opsForValue();
redisTemplate.opsForValue().set("name" , "taotao");
String name = (String)redisTemplate.opsForValue().get("name");
System.out.println(name);
}
/**
* 操作List类型的数据
*/
@Test
public void testList(){
ListOperations listOperations = redisTemplate.opsForList();
//存值
listOperations.leftPush("mylist","a");
listOperations.leftPushAll("mylist","b","c","d");
//取值
List mylist = listOperations.range("mylist", 0, -1);
for (Object value : mylist){
System.out.println(value);
}
//获取列表长度 llen
Long size = listOperations.size("mylist");
int lSize = size.intValue();
for (int i = 0; i < lSize; i++) {
//出队列
String element = (String)listOperations.rightPop("mylist");
System.out.println(element);
}
}
/**
* 操作Hash类型数据
*/
@Test
public void testHash(){
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("002","name","xiaoming");
hashOperations.put("002","age","20");
hashOperations.put("002","address","bj");
String age = (String)hashOperations.get("002", "age");
System.out.println(age);
//获得hash结构中的所有字段
Set keys = hashOperations.keys("002");
for (Object key:keys
) {
System.out.println(keys);
}
}
/**
* 操作set类型的数据
*/
@Test
public void testSet(){
SetOperations setOperations = redisTemplate.opsForSet();
//存值
setOperations.add("myset","a","b","c","d");
//取值
Set<String> myset = setOperations.members("myset");
for (String o : myset){
System.out.println(o);
}
//删除成员
setOperations.remove("myset","a","b");
//取值
myset = setOperations.members("myset");
for (String o : myset){
System.out.println(o);
}
}
/**
* 操作ZSet类型的数据
*/
@Test
public void testZset(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
//存值
zSetOperations.add("myZset","a",10.0);
zSetOperations.add("myZset","b",11.0);
zSetOperations.add("myZset","c",12.0);
zSetOperations.add("myZset","a",13.0);
//取值
Set<String> myZset = zSetOperations.range("myZset", 0, -1);
for (String s : myZset){
System.out.println(s);
}
//修改分数
zSetOperations.incrementScore("myZset","b",20.0);
//删除成员
zSetOperations.remove("myZset","a","b");
//取值
myZset = zSetOperations.range("myZset",0,-1);
for (String s : myZset){
System.out.println(s);
}
}
/**
* 通用操作,针对不同的数据类型都可以操作
*/
@Test
public void testCommon(){
//获取Redis种所有的key
Set<String> keys = redisTemplate.keys("*");
for (String key : keys){
System.out.println(key);
}
//判断某个key是否存在
Boolean taotao = redisTemplate.hasKey("taotao");
System.out.println(taotao);
//删除指定key
redisTemplate.delete("myZset");
//获取指定key对应的value的数据类型
DataType myset = redisTemplate.type("myset");
}
我们暂时使用StringRedisTeamplate类来处理,这样就不用为RedisTemplate配置config类了
目前用着没问题,3种Redis数据类型都支持类型