开始向导
添加 dependency spring-boot-starter-redis
,它默认使用 jedis
(还支持 JRedis,SRP 和 Lettuce)
一个简单的 Hash 示例:
@SpringBootApplication
public class RedisDemo implements CommandLineRunner {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public static void main(String[] args) {
SpringApplication.run(RedisDemo.class).close();
}
public void run(String... args) throws Exception {
BoundHashOperations<String, String, String> ops = stringRedisTemplate.boundHashOps("com.foo");
ops.put("name", "bar");
ops.put("sex", "man");
System.out.println(ops.entries()); // 打印结果:{name=bar, sex=man}
}
}