欢迎投稿

今日深度:

springboot整合redis,(因为

springboot整合redis,(因为


创建springboot整合redis工程:

一、springboot整合redis步骤

首先我们要知道什么是redis:

三步骤完成springboot对redis数据库的整合:

1、导入springboot整合redis坐标(上面勾选的那个就是)

2、在yml配置文件中配置redis端口号连接信息

3、自动装配RedisTemplate对象

补充:RedisTemplate对象提供了各种往redis数据库中存储数据的类型:

 代码演示如下所示:

注意:下面所有的步骤,要保证Redis数据库是开启的状态,要不然肯定连接不上Redis数据库

第一步:

 第二步:

 第三步:

package com.Bivin;

import org.junit.jupiter.api.Test;
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;

@SpringBootTest
class Springboot09RedisApplicationTests {

    /**
     *   第三步:自动装配RedisTemplate对象
     */
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     *  首先我们知道Redis是一款key,value存储结构的数据库
     *
     *   因此我们假定这个set()测试方法以key,value的形式往Redis数据库中存储数据
     *   
     *   key,value形式的存储命令:set 
     *   key,value形式的取数据命令:get
     *   
     */

    @Test
    void set() {
        ValueOperations ops = redisTemplate.opsForValue();    // 首先redisTemplate.opsForValue的目的就是表明是以key,value形式储存到Redis数据库中数据的
        ops.set("address1","zhengzhou");// 到这里就表明Redis数据库中存储了key为address1,value为zhengzhou的数据了(取的时候通过key取数据)
    }

    /**
     *  取数据
     */
    @Test
    void get() {
        ValueOperations ops = redisTemplate.opsForValue();  // 表明取的是key,value型的数据
        Object o = ops.get("address1");  // 获取Redis数据库中key为address1对应的value数据
        System.out.println(o);
    }

}

又因为我们知道Redis数据库支持多种格式的存储数据形式,因此还可以往Redis数据库中存储哈希类型的数据(哈希类型:就类似于一个key里面又放入一个key,value),因此Redis数据库哈希类型的储存/取数据演示如下所示:

package com.Bivin;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

@SpringBootTest
class Springboot09RedisApplicationTests {

    /**
     *   第三步:自动装配RedisTemplate对象
     */
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     *  向Redis数据库中储存哈希类型的数据(一个key里面放着一个key和value)
     *
     *  哈希类型的储存命令:put
     */
    @Test
    void hset() {
        HashOperations ops = redisTemplate.opsForHash();// 表明数据是以哈希类型的格式进行储存到Redis数据库的
        ops.put("info","a","aa");
        // 通过put命令,向Redis数据库中储存一个哈希类型的数据(一个为info的key里面放着一个key为a,value为aa的数据)
    }

    /**
     *  取数据
     *
     *  哈希类型的取值命令:get
     */
    @Test
    void hget() {
        HashOperations ops = redisTemplate.opsForHash();  // 表明取的是哈希类型的数据
        Object o = ops.get("info","a");  // 获取Redis数据库中哈希类型的数据(获取第一个key里面key为a的value数据)
        System.out.println(o);
    }

}

二、springboot读写redis的客户端(必须看)

在上面的演示中我们知道,我们已经把Redis数据库整合到springboot了,上面我们也通过RedisTemplate对象往Redis数据库中储存了一些数据,也进行了在Redis数据库中取数据的操作,

我们思考:我们通过该对象往Redis数据库中储存的那些数据,到底真的储存到Redis数据库中了吗? 

我们就拿上面往Redis数据库中储存的key为address1的数据,我们用cmd判断一下Redis数据库中到底储存了该数据没有: 答案就是没有

那么到底怎么才能把数据真正的储存到Redis数据库中呢:

因此我们需要把第三步中的自动装配的RedisTemplate对象换成StringRedisTemplate对象即可储存成功了。(因为Redis数据库都是字符串形式的数据,所以用StringRedisTemplate对象)

 

三、springboot操作Redis客户端实现技术切换(jedis)

第一步:导入坐标

第二步:配置客户端

 

总结:(面试必备)

www.htsjk.Com true http://www.htsjk.com/redis/45915.html NewsArticle springboot整合redis,因为 创建springboot整合redis工程 一、springboot整合redis步骤 首先我们要知道什么是redis 三步骤完成springboot对redis数据库的整合 1、导入springboot整合redis坐标上面勾选的那...
评论暂时关闭