第一步:pom.xml文件导入坐标
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.5.4</version> </dependency
第二步:application.yml文件添加配置
spring: redis: host: localhost port: 6379
第三步:RedisTemplate 提供操作各种数据存储类型的接口API
@SpringBootTest public class RedisTest { @Autowired private RedisTemplate redisTemplate; @Test public void testsetKeys(){ ValueOperations ops = redisTemplate.opsForValue(); ops.set("name","minkl1"); ops.set("country","china"); } @Test public void testgetKeys(){ ValueOperations ops = redisTemplate.opsForValue(); System.out.println(ops.get("name")); System.out.println(ops.get("country")); }
@Test public void testHashsetKeys() { HashOperations hashOperations = redisTemplate.opsForHash(); hashOperations.put("keya", "local1", "china"); hashOperations.put("keya", "local2", "chinaeasdf"); hashOperations.put("keya", "local3", "chifdsaf"); } @Test public void testHashgetKeys(){ HashOperations hashOperations = redisTemplate.opsForHash(); System.out.println(hashOperations.get("keya", "local1")); System.out.println(hashOperations.get("keya", "local2")); System.out.println(hashOperations.get("keya", "local3")); }
}