5.3.2 使用方式
5.3.2.1 环境搭建
第一步:创建maven项目springdataredis_demo,配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> </parent> <groupId>com.itheima</groupId> <artifactId>springdataredis_demo</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.5</version> </plugin> </plugins> </build> </project>
第二步:编写启动类
package com.itheima; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } }
第三步:配置application.yml
spring: application: name: springdataredis_demo #Redis相关配置 redis: host: localhost port: 6379 #password: 123456 database: 0 #操作的是0号数据库 jedis: #Redis连接池配置 pool: max-active: 8 #最大连接数 max-wait: 1ms #连接池最大阻塞等待时间 max-idle: 4 #连接池中的最大空闲连接 min-idle: 0 #连接池中的最小空闲连接
解释说明:
spring.redis.database:指定使用Redis的哪个数据库,Redis服务启动后默认有16个数据库,编号分别是从0到15。
可以通过修改Redis配置文件来指定数据库的数量。
第四步:提供配置类
package com.itheima.config; 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; /** * Redis配置类 */ @Configuration public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); //默认的Key序列化器为:JdkSerializationRedisSerializer redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setConnectionFactory(connectionFactory); return redisTemplate; } }
解释说明:
当前配置类不是必须的,因为 Spring Boot 框架会自动装配 RedisTemplate 对象,但是默认的key序列化器为JdkSerializationRedisSerializer,导致我们存到Redis中后的数据和原始数据有差别
第五步:提供测试类
package com.itheima.test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @RunWith(SpringRunner.class) public class SpringDataRedisTest { @Autowired private RedisTemplate redisTemplate; }
5.3.2.2 操作字符串类型数据
/** * 操作String类型数据 */ @Test public void testString(){ //存值 redisTemplate.opsForValue().set("city123","beijing"); //取值 String value = (String) redisTemplate.opsForValue().get("city123"); System.out.println(value); //存值,同时设置过期时间 redisTemplate.opsForValue().set("key1","value1",10l, TimeUnit.SECONDS); //存值,如果存在则不执行任何操作 Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("city1234", "nanjing"); System.out.println(aBoolean); }
5.3.2.3 操作哈希类型数据
/** * 操作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(key); } //获得hash结构中的所有值 List values = hashOperations.values("002"); for (Object value : values) { System.out.println(value); } }
5.3.2.4 操作列表类型数据
/** * 操作List类型的数据 */ @Test public void testList(){ ListOperations listOperations = redisTemplate.opsForList(); //存值 listOperations.leftPush("mylist","a"); listOperations.leftPushAll("mylist","b","c","d"); //取值 List<String> mylist = listOperations.range("mylist", 0, -1); for (String 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); } }
5.3.2.5 操作集合类型数据
/** * 操作Set类型的数据 */ @Test public void testSet(){ SetOperations setOperations = redisTemplate.opsForSet(); //存值 setOperations.add("myset","a","b","c","a"); //取值 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); } }
5.3.2.6 操作有序集合类型数据
/** * 操作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); //取值 myZset = zSetOperations.range("myZset", 0, -1); for (String s : myZset) { System.out.println(s); } //删除成员 zSetOperations.remove("myZset","a","b"); //取值 myZset = zSetOperations.range("myZset", 0, -1); for (String s : myZset) { System.out.println(s); }
5.3.2.7 通用操作
/** * 通用操作,针对不同的数据类型都可以操作 */ @Test public void testCommon(){ //获取Redis中所有的key Set<String> keys = redisTemplate.keys("*"); for (String key : keys) { System.out.println(key); } //判断某个key是否存在 Boolean itcast = redisTemplate.hasKey("itcast"); System.out.println(itcast); //删除指定key redisTemplate.delete("myZset"); //获取指定key对应的value的数据类型 DataType dataType = redisTemplate.type("myset"); System.out.println(dataType.name()); }