spring boot整合redis

进行spring boot 整合redis操作的前提是了解redis和spring boot的基本操作
若不清楚这两项基本前提,请参考以下文章
redis学习
spring MVC入门
spring入门
首先新建一个maven工程,然后向pom.xml中添加一些基本依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

<!--        spring boot版本在1.5以上就必须加-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.6.1</version>
        </dependency>

等包下载完之后,进行redis的配置,由于我在本项目中用的是application.yaml,所以格式会和application.properties有些出入

#redis配置
spring:
  redis:
    #redis数据库索引
    database: 0
    #redis服务器地址
    host: localhost
    #redis服务器端口
    port: 6379
    #redis数据库密码,默认为空
    password:
    lettuce:
      pool:
        #连接池中最大连接数(使用负数表示没有限制。默认8)
        max-active: 8
        #连接池中最大阻塞等待时间(是用负值表示没有时间限制,此处用-1)
        max-wait: -1
        #连接池中最大空闲连接,默认8
        max-idle: 8
        #连接池中最小空闲连接,默认0
        min-idle: 0
    #连接超时时间
    timeout: 5000

配置完毕后就可以对redis数据库进行操作了,本文采取Junit进行操作,代码如下:

package com.redis.demo;

import com.redis.demo.controller.Student;
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.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;

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

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void redisStr() throws InterruptedException{
        ValueOperations operations = redisTemplate.opsForValue();
        operations.set("name","wcc1");
        System.out.println("检索key:"+operations.get("name"));
    }

    @Test
    public void redisObj()throws InterruptedException{
        ValueOperations operations = redisTemplate.opsForValue();
        Student student = new Student();
        student.setName("wcc");
        student.setAge(16);
        student.setClas("123");
        operations.set("student",student);
        System.out.println("获取"+operations.get("student"));
    }

}

在这里插入图片描述
在这里插入图片描述
值得一提的是,进行对象存储的时候,必须将对象进行序列化,否则会无法存取
对redis数据进行delete操作

    @Test
    public void redisDel(){
        ValueOperations operations = redisTemplate.opsForValue();
        redisTemplate.delete("name");
        System.out.println("name为:"+operations.get("name"));

    }

在这里插入图片描述

    @Test
    public void redisHash(){
        HashOperations operations = redisTemplate.opsForHash();
        operations.put("hash","name","wcc");
        System.out.println(operations.get("hash","name"));
    }

在这里插入图片描述

    @Test
    public void redisList(){
        ListOperations<String,String> operations = redisTemplate.opsForList();
        operations.leftPush("name","张三") ;
        operations.leftPush("name","李四");
        operations.leftPush("name","王二");
        operations.leftPush("name","麻子");
        for (int i = 0; i < 4; i++) {
            System.out.println(operations.leftPop("name"));
        }
    }

在这里插入图片描述

    @Test
    public void redisSet(){
        SetOperations<String,String> operations = redisTemplate.opsForSet();
        operations.add("name1","wcc") ;
        operations.add("name1","wcc1");
        operations.add("name1","wcc2");
        operations.add("name1", "wcc3");
        System.out.println(operations.members("name1"));
    }

在这里插入图片描述

    @Test
    public void redis_Z_Set(){
        ZSetOperations operations = redisTemplate.opsForZSet();
        operations.add("name2","wcc",1);
        operations.add("name2","wcc1",2);
        operations.add("name2","wcc2",3);
        operations.add("name2","wcc3",4);
        System.out.println(operations.rangeByScore("name2",1,3));
    }

在这里插入图片描述
值得一提的是,当向redis中存放数据的时候,不同类型的数据不能取相同的key,假设List和Set类型的数据全都用name作为key,当读取的时候就会出现不同类型的数据无法读取的错误。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃小巴掌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值