数据库
首页 > 数据库> > SpringBoot整合Redis

SpringBoot整合Redis

作者:互联网

SpringBoot整合Redis的pom坐标

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

Application.yml配置Redis

spring:
  redis:
    port: 6379
    host: localhost
#端口6379 和主机地址 127.0.0.1

在Idea使用Redis前,注意先从安装路径启动Redis,详细方法见上一篇博客

Redis操作,单一的Key-Value

@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);
    }

}

Redsi操作,hash存储结构

@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);
    }
}

 

标签:name,ops,age,Redis,System,user,整合,SpringBoot
来源: https://www.cnblogs.com/Sun-fish/p/16442052.html