SpringBoot整合Redis 并 展示使用方法

步骤

  1. 引入依赖
  2. 配置数据库参数
  3. 编写配置类
  4. 构造RedisTemplate
  5. 创建测试类测试

1.引入依赖

不写版本号,也是可以的

在pom中引入

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

2.配置数据库参数

在SpringBoot的yaml配置文件中引入,如果你用的是properties格式,那么去转换一下就行了,把yaml转换成properties

spring:
  redis:
    # Redis数据库索引(默认为0)
    database: 11
    # Redis服务器地址
    host: localhost
    # Redis服务器连接端口
    port: 6379
    # Redis服务器连接密码(默认为空)
    password: ''
    # 连接超时时间(毫秒)
    timeout: 1000

3.编写配置类

package com.kyw.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 设置key的序列化方式
        template.setKeySerializer(RedisSerializer.string());

        // 设置value的序列化方式
        template.setValueSerializer(RedisSerializer.json());

        // 设置hash的key序列化方式
        template.setHashKeySerializer(RedisSerializer.string());

        // 设置hash的value序列化方式
        template.setHashValueSerializer(RedisSerializer.json());

        // 在设置完参数后生效
        template.afterPropertiesSet();

        return template;
    }

}

4.构造RedisTemplate

上面的配置类中就构造好了

5.测试

这里用的是junit测试,是常用的测试工具,没有导入的小伙伴导入一下,网上搜一下很简单。

编写测试类

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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
//下面括号里面:classes  = SpringBootDemoApplication.class,如果报错,那你转换成自己的SpringBoot启动类的类名就行
@ContextConfiguration(classes  = SpringBootDemoApplication.class)
public class RedisTest {
    @Autowired
    private RedisTemplate redisTemplate;

}

测试String

/**
 *String 测试
 */
@Test
public void testStrings(){
    String redisKey = "test:count";
    //设置一个 key value
    redisTemplate.opsForValue().set(redisKey,1);

    //获取value
    System.out.println(redisTemplate.opsForValue().get(redisKey));
    //value自增
    System.out.println(redisTemplate.opsForValue().increment(redisKey));
    //value自减
    System.out.println(redisTemplate.opsForValue().decrement(redisKey));
}

测试哈希

/**
 *哈希 测试
 */
@Test
public void testHashes(){
    String redisKey = "test:user";

    redisTemplate.opsForHash().put(redisKey,"id",1);
    redisTemplate.opsForHash().put(redisKey,"username","张三");

    System.out.println(redisTemplate.opsForHash().get(redisKey,"id"));
    System.out.println(redisTemplate.opsForHash().get(redisKey,"username"));
}

 

测试list

/**
 *list 类测试
 */
@Test
public void testLists(){
    String redisKey = "test:ids";

    redisTemplate.opsForList().leftPush(redisKey,101);
    redisTemplate.opsForList().leftPush(redisKey,102);
    redisTemplate.opsForList().leftPush(redisKey,103);

    System.out.println(redisTemplate.opsForList().size(redisKey));
    System.out.println(redisTemplate.opsForList().index(redisKey,0));
    System.out.println(redisTemplate.opsForList().range(redisKey,0,2));
    System.out.println(redisTemplate.opsForList().leftPop(redisKey));
    System.out.println(redisTemplate.opsForList().leftPop(redisKey));
    System.out.println(redisTemplate.opsForList().leftPop(redisKey));

}

 测试set

/**
 *set 测试
 */
@Test
public void testSets(){
    String redisKey = "test:teachers";
    redisTemplate.opsForSet().add(redisKey,"鸣人","路飞","韩立","炭治郎","祖国人");

    System.out.println(redisTemplate.opsForSet().size(redisKey));
    System.out.println(redisTemplate.opsForSet().pop(redisKey));
    System.out.println(redisTemplate.opsForSet().pop(redisKey));

}

 

 测试zset

@Test
public void testSortSets(){
    String redisKey = "test:students";
    redisTemplate.opsForZSet().add(redisKey,"少年鸣人",10);
    redisTemplate.opsForZSet().add(redisKey,"仙人鸣人",60);
    redisTemplate.opsForZSet().add(redisKey,"九尾鸣人",70);
    redisTemplate.opsForZSet().add(redisKey,"九喇嘛联结鸣人",80);
    redisTemplate.opsForZSet().add(redisKey,"六道鸣人",90);

    System.out.println(redisTemplate.opsForZSet().zCard(redisKey));
    System.out.println(redisTemplate.opsForZSet().score(redisKey,"六道鸣人"));
    System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey,"六道鸣人"));
    System.out.println(redisTemplate.opsForZSet().range(redisKey,0,4));
    System.out.println(redisTemplate.opsForZSet().removeRange(redisKey,0,2));

}

 测试通用操作


/**
 *测试通用操作
 */
@Test
public void testKeys(){
    redisTemplate.delete("test:user");
    System.out.println(redisTemplate.hasKey("test:user"));
    redisTemplate.expire("text:students",10, TimeUnit.SECONDS);
}

测试绑定key到变量的操作

/**
 *测试 绑定一个key 的操作
 */
@Test
public void testBondOperations(){
    String redisKey = "test:count";
    //用 BoundValueOperations 将一个key绑定到对象上,方便后面的操作
    //绑定的是 String 那就 BoundValueOperations,是set 那就BoundSetOperations,其余同理
    BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);
    System.out.println(operations.get());
    operations.increment();
    operations.increment();
    System.out.println(operations.get());

}

如果您觉得文章还不错,麻烦点个赞吧

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值