当前位置: 首页 >  互联网技术 >  springboot中操作redis

springboot中操作redis

导读:1.maven引入相关依赖.<dependencies>.<!-- spring-boot-starter-data-redis -->.<dependency>.<groupId>org.springframework.boot</groupId>.<artifactId>sp

1.maven引入相关依赖

<dependencies>
 	<!-- spring-boot-starter-data-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- commons-pool2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.11.1</version>
        </dependency>
        <!--jackjson-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>

2.配置redis

application.yml

spring:
  # 配置redis
  redis:
    host: 192.168.***.***
    port: 6379
    password: ******
    lettuce:
      pool:
        max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
        max-idle: 8 # 连接池中的最大空闲连接
        max-wait: 100 # 连接池最大阻塞等待时间(使用负值表示没有限制)
        min-idle: 0 # 连接池中的最小空闲连接
    database: 0 # redis数据库索引(默认为0)

3.配置序列化

package com.example.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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * Redis  序列化方式配置
 *
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        // 创建RedisTemplate<String, Object>对象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(factory);
        // json方式序列化对象
        GenericJackson2JsonRedisSerializer jsonRedisSerializer =
                new GenericJackson2JsonRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        // value序列化方式采用jackson
        template.setValueSerializer(jsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jsonRedisSerializer);
        return template;
    }

}

4.测试类中进行测试

package com.example;

import com.example.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.StringRedisTemplate;

@SpringBootTest
class SpringDataRedisTestApplicationTests {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 测试redis的String类型
     */
    @Test
    void testString() {
        redisTemplate.opsForValue().set("name", "minqiliang");
        System.out.println(redisTemplate.opsForValue().get("name"));
    }

    /**
     * 使用StringRedisTemplate操作redis(需要手动进行序列化和反序列化)
     * @throws JsonProcessingException
     */
    @Test
    void testString2() throws JsonProcessingException {
        // 创建一个对象
        User user = new User("001", "minqiliang", 18);
        // 由于StringRedisTemplate默认使用的是String的序列化器,所以这里需要将对象转换成json字符串
        String json = objectMapper.writeValueAsString(user);
        // 存入redis
        stringRedisTemplate.opsForValue().set("user:001", json);
        // 从redis中取出数据
        String s = stringRedisTemplate.opsForValue().get("user:001");
        // 将json字符串转换成对象
        User u = objectMapper.readValue(s, User.class);
        System.out.println(u);
    }

    @Test
    void testHash() {
        // 存入redis
        redisTemplate.opsForHash().put("user:002", "id", "002");
        redisTemplate.opsForHash().put("user:002", "name", "张三");
        redisTemplate.opsForHash().put("user:002", "age", 18);
        // 获取对应的key的所有值
        System.out.println(redisTemplate.opsForHash().entries("user:002"));
        System.out.println("====================================");
        // 获取对应的key的某个值
        System.out.println(redisTemplate.opsForHash().get("user:002", "id"));
        System.out.println(redisTemplate.opsForHash().get("user:002", "name"));
        System.out.println(redisTemplate.opsForHash().get("user:002", "age"));
    }
}
内容
  • UE构建基础和实践:一、概述
    UE构建基础和实践:一、概述
    2023-12-04
    序言.构建这个概念是和应用程序包及资源等相关的,它的特点是使用脚本控制一系列的任务的处理,以减少人力操作带来的成本和操作
  • 园林绿化养护服务
    园林绿化养护服务
    2024-01-10
    园林绿化养护服务.产品功能.园林绿化养护服务是一项专业的服务,旨在为客户提供全方位的园林绿化管理和养护服务。我们团队的专
  • 园林景观设计
    园林景观设计
    2023-12-11
    园林景观设计产品介绍.产品功能.园林景观设计是一项专业的设计服务,主要用于规划和设计公共和私人的园林空间。其功能包括根据
  • 绿化工程材料供应
    绿化工程材料供应
    2023-12-16
    绿化工程材料供应.产品功能.我们的绿化工程材料供应主要用于城市绿化、园林景观建设、庭院绿化等相关项目。产品种类丰富,覆盖
  • 城市绿化规划设计
    城市绿化规划设计
    2024-01-15
    城市绿化规划设计.随着城市化进程的不断加快,城市绿化规划设计成为了一个备受关注的问题。如何在城市中保护和增加绿地,促进城
  • 喷泉景观设计
    喷泉景观设计
    2024-01-05
    喷泉景观设计.喷泉是一种极具观赏性和装饰性的景观设计元素,它不仅可以为周围的环境增添一份生气与动感,更可以为人们带来一份
  • 公园景观规划设计
    公园景观规划设计
    2024-01-10
    公园景观规划设计.产品功能.我们的公园景观规划设计产品旨在为城市和乡村地区提供高质量的公共休闲空间。我们致力于通过规划和
  • 园林休闲座椅制作
    园林休闲座椅制作
    2024-01-20
    园林休闲座椅制作.产品功能.园林休闲座椅是专门为户外休闲空间设计制作的座椅产品。产品具有耐候性强、外观美观、舒适度高等特
  • 喷泉设计与安装
    喷泉设计与安装
    2023-12-21
    喷泉设计与安装.喷泉是园林景观中不可或缺的元素之一,无论是在公园、**还是私人花园中,喷泉都能为环境增添灵动的气息,成为
  • 室外园林景观配套设施制作
    室外园林景观配套设施制作
    2024-01-15
    室外园林景观配套设施制作.产品功能.我们的室外园林景观配套设施制作主要提供定制化的户外景观配套设施,包括花池、凉亭、栏杆
  • 景观照明工程
    景观照明工程
    2023-12-16
    景观照明工程.产品功能.景观照明工程是一种专门为户外景观设计的照明方案。它既可以美化城市风景,提升城市形象,也可以为人们