微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐