springboot集成redis ————使用Spring Data Redis的方式

1.添加所需依赖

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

2.配置appplication.properties

没有密码就不用填写

#redis
spring.redis.host=127.0.0.1
spring.redis.password=
spring.redis.port=6379
spring.redis.pool.max-active=8

3.自动注入StringRedisTemplates

使用StringRedisTemplates可以操作redis

//使用StringRedisTemplate操作redis
@Autowired
private StringRedisTemplate stringRedisTemplate;

4. Redis的数据结构
在这里插入图片描述
5.操作这些结构

  • opsForValue,用来设置普通的Key-value
//设置一个键为testKey,值为testValue,并将其放入redis中
stringRedisTemplate.opsForValue().set("testKey","testValue");
//从redis中取出键为testKey的值
stringRedisTemplate.opsForValue().get("testKey");

其中很多方法,可以自己进行尝试
在这里插入图片描述

  • opsForList,用来操作LIst结构
//设置一个键为testKey,值为testValue,并将其放入redis中
stringRedisTemplate.opsForList().leftPsuh("testKey","testValue");
//从redis中取出键为testKey的值
stringRedisTemplate.opsForList().get("testKey");

其中很多方法,可以自己进行尝试
在这里插入图片描述

  • opsForHash:用来操作Hash数据结构
stringRedisTemplate.opsForHash.put("cacahe","1","2");
String str = (String)stringRedisTemplate.opsForHash().get("cache","1");

在这里插入图片描述

其中很多方法,可以自己进行尝试
在这里插入图片描述

基本的增删改查完整代码下载:https://download.csdn.net/download/m0_45025658/12363618

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值