SpringBoot——整合redis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010837612/article/details/80363819

添加依赖:

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

application.yml配置:

spring
  redis:
    database: 1
    host: 172.16.3.99
    port: 6379
    password:
    timeout: 5000 # 超时时间
    jedis:
      pool:
        max-active: 1000 #连接池最大连接数,负值表示不限制
        max-idle: 10  # 连接池中最大空闲连接
        min-idle: 2  # 连接池中最小空闲连接
        max-wait: -1 # 连接池最大阻塞等待时间,负值表示不限制

你要先安装redis

然后就可以使用了,先autowired StringRedisTemplate ,这个类提供了对redis的基本操作

@Autowired
private StringRedisTemplate redis;

使用:

redis.opsForValue().set("name","spring boot");
String value = redis.opsForValue().get("name");

System.out.println(value);

使用redis manager查看一下效果:
这里写图片描述

redis支持五种数据存储:String(字符串)、List(列表)、Set(集合)、Hash(散列)和 Zset(有序集合),StringRedisTemplate 也有提供对应的api操作

redis.opsForValue();//操作字符串
redis.opsForHash();//操作hash
redis.opsForList();//操作list
redis.opsForSet();//操作set
redis.opsForZSet();//操作有序set

例如操作Hash:

redis.opsForHash().put("userlist","name","tom");

其他api大家自己尝试一下就明白了。建议把常用方法封装成工具类使用。

猜你喜欢

转载自blog.csdn.net/u010837612/article/details/80363819