栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Redis快速入门(整合Springboot+redis基础操作)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Redis快速入门(整合Springboot+redis基础操作)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录
  • 一、idea新建springboot项目,勾选4个依赖项
  • 二、项目结构
  • 三、String操作
  • 四、Set操作
  • 五、 Hash操作
  • 六、List操作
  • 七、ZSet操作
  • 八、 BitMap操作
  • 查看终端工具:![在这里插入图片描述](https://img-blog.csdnimg.cn/1795c6ec453c433789c64572a5ec90dd.png)


一、idea新建springboot项目,勾选4个依赖项

注意:在applicaiton.properties配置文件中配置


Redis服务器地址
spring.redis.host=192.168.12.59
二、项目结构

三、String操作
package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

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


@Component
public class StringDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final String key1 = "tea#01";
    private final String key2 = "tea#02";

    public void test() {
        redisTemplate.opsForValue().set(key1, "张三");
        String s = redisTemplate.opsForValue().get(key1);
        System.out.println(s);
    }


    public void test2() throws InterruptedException {
        redisTemplate.opsForValue().set(key2, "李四", 30, TimeUnit.SECONDS);
        String s = redisTemplate.opsForValue().get(key2);
        System.out.println("第一次获取值:" + s);
        Thread.sleep(31 * 1000);
        String s2 = redisTemplate.opsForValue().get(key2);
        System.out.println("第二次获取值:" + s2);
    }

}

单元测试类:

package cn.hncj.springbootredis.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;


@SpringBootTest
class StringDemoTest {
    @Autowired
    private StringDemo stringDemo;
    @Test
    void test1() {
        stringDemo.test();
    }

    @Test
    void test2() throws InterruptedException {
        stringDemo.test2();
    }

}

四、Set操作
package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

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


@Component
public class SetDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final String key5 = "tea#04";
    private final String key6 = "tea#05";


    public void test() {
        redisTemplate.opsForSet().add(key5,"语文");
        redisTemplate.opsForSet().add(key5,"数学");
        redisTemplate.opsForSet().add(key5,"英语");


        redisTemplate.opsForSet().add(key6,"语文");
        redisTemplate.opsForSet().add(key6,"英语");

        Set intersect = redisTemplate.opsForSet().intersect(key5, key6);
        System.out.println(intersect);

    }


}

单元测试类:

package cn.hncj.springbootredis.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;


@SpringBootTest
class SetDemoTest {
    @Autowired
    private SetDemo setDemo;
    @Test
    void test1() {
        setDemo.test();
    }
}

五、 Hash操作
package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

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


@Component
public class HashDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private  final  String key3 ="tea#03";

    public void test(){
        redisTemplate.opsForHash().put(key3,"家庭住址","开封");
        redisTemplate.opsForHash().put(key3,"大学","开封大学");
        List values = redisTemplate.opsForHash().values(key3);

        for (Object value:values){
            System.out.println(value);
        }
    }
}

 
package cn.hncj.springbootredis.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class HashDemoTest {
    @Autowired
    private HashDemo hashDemo;

    @Test
    void test1() {

        hashDemo.test();
    }
}

六、List操作
package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;


@Component
public class ListDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final String key1="tea#07";
    public void test(){
    redisTemplate.opsForList().leftPush(key1,"张三");
    redisTemplate.opsForList().leftPush(key1,"李四");
    redisTemplate.opsForList().leftPush(key1,"王五");
        Object o = redisTemplate.opsForList().rightPop(key1);
        System.out.println(o);
    }
}

package cn.hncj.springbootredis.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class ListDemoTest {
    @Autowired
    private ListDemo listDemo;

    @Test
    void test1() {

        listDemo.test();
    }
}

七、ZSet操作
package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.Set;


@Component
public class ZSetDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final String key6 = "tea#06";


    public void test() {
        redisTemplate.opsForZSet().add(key6, "语文", 88);
        redisTemplate.opsForZSet().add(key6, "数学", 99);
        redisTemplate.opsForZSet().add(key6, "英语", 66);

        ZSetOperations.TypedTuple stringTypedTuple = redisTemplate.opsForZSet().popMax(key6);
        System.out.println("成绩最高的科目:"+stringTypedTuple.getValue());
        System.out.println("成绩最高的科目:"+stringTypedTuple.getScore());
    }


}

package cn.hncj.springbootredis.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class ZSetDemoTest {
    @Autowired
    private ZSetDemo zsetDemo;
    @Test
    void test1() {
        zsetDemo.test();
    }
}

八、 BitMap操作
package cn.hncj.springbootredis.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.List;


@Component
public class BitDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private  final  String key3 ="tea#08";

    public void test(){
        redisTemplate.opsForValue().setBit(key3,2,true);
        redisTemplate.opsForValue().setBit(key3,144,true);

        RedisCallback callback=connection -> {
            return connection.bitCount(key3.getBytes(),0,19);
        };
        Long totalCount = redisTemplate.execute(callback);
        System.out.println(totalCount);

    }
}

package cn.hncj.springbootredis.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest
class BitMapDemoTest {
    @Autowired
    private BitDemo bitDemo;

    @Test
    void test1() {

        bitDemo.test();
    }
}

查看终端工具:
转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号