首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >SpringBoot+SpringData-Redis 操作 Redis(CRUD) 模板代码

SpringBoot+SpringData-Redis 操作 Redis(CRUD) 模板代码

作者头像
用户9006224
发布2022-12-21 09:28:03
发布2022-12-21 09:28:03
4260
举报
文章被收录于专栏:cjz的专栏cjz的专栏

Spring Data Redis

官网:http://projects.spring.io/spring-data-redis/ 这是spring整合的Redis框架十分简洁好用

导入依赖

导入SpringDataRedis依赖也会传递导入了SpringBoot基本依赖,为了演示基本操作我在测试类里操作,我引入了 text包

代码语言:javascript
复制
<!--SpringDataRedis依赖我没有加版本号,这个一定要加上,除非你有springboot依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencies>
<!--        SpringDataRedis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
<!--测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

启动类

代码语言:javascript
复制
package com.lianxi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringDataRedisApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringDataRedisApplication.class,args);
    }
}

配合文件

application.yml 中添加如下配置,端口不修改默认是 6379

代码语言:javascript
复制
spring:
  redis:
    host: 127.0.0.1  # 自己的Redis地址
    port: 6379

注入StringRedisTemplate

在使用SpringData-Redis的类里注入StringRedisTemplate,好处就是在查看是不会有看不懂的编码的问题。

熟悉常见的一些语法

里面封装了对于Redis的五种数据结构的各种操作,包括:

  • redisTemplate.opsForValue() :操作字符串
  • redisTemplate.opsForHash() :操作hash
  • redisTemplate.opsForList():操作list
  • redisTemplate.opsForSet():操作set
  • redisTemplate.opsForZSet():操作zset

例如我们对字符串操作比较熟悉的有:getset等命令,这些方法都在 opsForValue()返回的对象中有:

操作字符串

代码语言:javascript
复制
package com.lianxi.text;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SDRDeomStr {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Test //如果有就修改,没有就添加
    public void testAdd(){
        //为设置过期时间
        redisTemplate.boundValueOps("name1").set("未设置过期时间");
        //设置过期时间,10秒后销毁,TimeUnit:是告诉Redis时间单位
        redisTemplate.boundValueOps("name2").set("设置过期时间",10, TimeUnit.SECONDS);
    }
    
    @Test //获取
    public void testGet(){
        String name = redisTemplate.boundValueOps("name").get();
    }
    
    @Test //删除
    public void testDel(){
        redisTemplate.delete("name");
    }

}

操作list

代码语言:javascript
复制
package com.lianxi.text;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SDRDeomList {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Test //添加,右压栈
    public void testAddList(){
        redisTemplate.boundListOps("listKey").rightPush("迪丽热巴");
        redisTemplate.boundListOps("listKey").rightPush("哈妮克孜");
        redisTemplate.boundListOps("listKey").rightPush("古力娜扎");
        redisTemplate.boundListOps("listKey").rightPush("王五赵四");
    }
    
    @Test //获取,range相当于分页,从0开始取10个
    public void testGetList(){
        List<String> stringList = redisTemplate.boundListOps("listKey").range(0, 10);
        //打印结果
        System.out.println(stringList);
    }
    
    @Test //删除,用右弹出或者直接删除 都可以
    public void testDelList(){
        //弹出方式,谈一个没一个
        String listKey = redisTemplate.boundListOps("listKey").rightPop();
        System.out.println(listKey);
        
//        redisTemplate.delete("listKey");
    }

}

操作set

代码语言:javascript
复制
package com.lianxi.text;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SDRDeomSet {

    @Autowired
    private StringRedisTemplate redisTemplate;
    
    @Test //添加,直接add添加
    public void testAddSet(){
        redisTemplate.boundSetOps("setKey").add("杨幂");
        redisTemplate.boundSetOps("setKey").add("唐嫣");
        redisTemplate.boundSetOps("setKey").add("刘诗诗");
        redisTemplate.boundSetOps("setKey").add("张萌");
        redisTemplate.boundSetOps("setKey").add("准备删除");
    }
    
    @Test //获取 members:所有成员
    public void testGetSet(){
        //获取所有成员
        Set<String> stringList = redisTemplate.boundSetOps("setKey").members();
        //打印
        System.out.println(stringList);
    }
    
    @Test //删除
    public void testDelSet(){
        //根据大key删除,这个是全部删除
//        redisTemplate.delete("setKey");

        //根据小key删除
         redisTemplate.boundSetOps("setKey").remove("准备删除");
    }
}

操作hash

代码语言:javascript
复制
package com.lianxi.text;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SDRDeomHash {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Test //添加
    public void testAddHash(){
        redisTemplate.boundHashOps("hashKey").put("a","AAA");
        redisTemplate.boundHashOps("hashKey").put("b","BBB");
        redisTemplate.boundHashOps("hashKey").put("c","CCC");
        redisTemplate.boundHashOps("hashKey").put("d","DDD");

    }
    
    @Test //获取
    public void testGetHash(){
        //先获取key集合
        Set<Object> stringList = redisTemplate.boundHashOps("hashKey").keys();
        //打印key集合
        System.out.println("所有key:"+stringList);
        //遍历打印
        for (Object oj : stringList) {
            Object hashKey = redisTemplate.boundHashOps("hashKey").get(oj);
            //打印value值
            System.out.println(oj+":"+hashKey);
        }
    }
    
    @Test //删除弹出和直接删除都可以
    public void testDelHash(){
        //删除全部
//        redisTemplate.delete("hashKey");
        //根据key删除
        redisTemplate.boundHashOps("hashKey").delete("a");
    }
}

添加和修改是一样的,有修改,没有添加

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring Data Redis
  • 导入依赖
  • 启动类
  • 配合文件
  • 注入StringRedisTemplate
    • 熟悉常见的一些语法
    • 操作字符串
  • 操作list
  • 操作set
  • 操作hash
  • 添加和修改是一样的,有修改,没有添加
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档