<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
spring: redis: port: 6379 host: localhost #端口6379 和主机地址 127.0.0.1
@SpringBootTest class J14SpringBootRedisApplicationTests { @Resource //使用AutoWired会爆红 private RedisTemplate redisTemplate; @Test void contextLoads() { ValueOperations ops = redisTemplate.opsForValue(); ops.set("name", "Terry"); ops.set("age", "18"); Object name = ops.get("name"); Object age = ops.get("age"); System.out.println(name); System.out.println(age); } }
@SpringBootTest class J14SpringBootRedisApplicationTests { @Resource private RedisTemplate redisTemplate; @Test void contextLoads() { HashOperations ops = redisTemplate.opsForHash(); ops.put("user","name","Terry"); ops.put("user","age","18"); ops.put("user","gender","male"); ops.put("user","hair","long"); Object name = ops.get("user", "name"); Object age = ops.get("user", "age"); Object gender = ops.get("user", "gender"); Object hair = ops.get("user", "hair"); System.out.println(name); System.out.println(age); System.out.println(gender); System.out.println(hair); } }