【linux系统之redis6】redisTemplate的使用方法

新版本的application.yml配置文件

xml 复制代码
spring:
  data:
    redis:
      host: 192.168.1.102
      port: 6379
      lettuce:
        pool:
          max-active: 8
          min-idle: 1
          max-idle: 0
          max-wait: 100

redisTemplate使用方法

xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>
</dependencies>
java 复制代码
package com.gaofeng;

import lombok.extern.slf4j.Slf4j;
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;

@Slf4j
@SpringBootTest
class Springboot07RedisTemplateApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;


    @Test
    void testString() {
        // 写入一条数据
       redisTemplate.opsForValue().set("name","胡歌");
        Object name = redisTemplate.opsForValue().get("name");
        log.info("name is {}",name);
    }

}
  • 中文字符的处理

  • 依赖添加
xml 复制代码
 <dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
 </dependency>
  • redisTemplate配置类
java 复制代码
package com.gaofeng.redis.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;

/**
 * @author gaofeng
 * @date 2025-01-08 - 20:42
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        // 创建redisTemplate对象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 设置连接工厂
        template.setConnectionFactory(redisConnectionFactory);
        // 创建JSON序列化工具
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置key的序列化
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());
        // 设置value的序列化
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashKeySerializer(jsonRedisSerializer);
        // 返回
        return template;
    }
}
java 复制代码
package com.gaofeng.redis.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author gaofeng
 * @date 2025-01-08 - 20:56
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private Integer age;
}
  • 测试类
java 复制代码
package com.gaofeng;

import com.gaofeng.redis.pojo.User;
import lombok.extern.slf4j.Slf4j;
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;

@Slf4j
@SpringBootTest
class Springboot07RedisTemplateApplicationTests {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    @Test
    void testString() {
        // 写入一条数据
       redisTemplate.opsForValue().set("name","胡歌");
        Object name = redisTemplate.opsForValue().get("name");
        log.info("name is {}",name);
    }

    @Test
    void testSaveUser(){
        redisTemplate.opsForValue().set("user:1",new User("gaofeng",30));
        User user = (User) redisTemplate.opsForValue().get("user:1");
        log.info("user is {} ",user);
    }

}


这样,显示就正常了

  • 手动序列化
java 复制代码
@Autowired
private StringRedisTemplate stringRedisTemplate;


@Test
void testString(){
    stringRedisTemplate.opsForValue().set("name","胡歌111");
    String name = stringRedisTemplate.opsForValue().get("name");
    System.out.println("name = " + name);

}
java 复制代码
@Test
void testSaveUser() throws JsonProcessingException {
    User user = new User("胡歌222", 40);
    String json = mapper.writeValueAsString(user);

    stringRedisTemplate.opsForValue().set("user:200",json);

    String jsonUser = stringRedisTemplate.opsForValue().get("user:200");

    //
    User user1 = mapper.readValue(jsonUser, User.class);

    System.out.println("user1 = " + user1);
    
}

这样我们就去掉了自动序列化多余的@class字段

  • hash数据
java 复制代码
@Test
void testHash(){
    stringRedisTemplate.opsForHash().put("user:400","name","gaofeng");
    stringRedisTemplate.opsForHash().put("user:400","age","30");
    Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("user:400");
    System.out.println(entries);
}


总结

相关推荐
风月歌1 小时前
基于Spring Boot的海滨体育馆管理系统的设计与实现
java·spring boot·后端
小青柑-6 小时前
Go语言中的接收器(Receiver)详解
开发语言·后端·golang
C++小厨神8 小时前
Bash语言的计算机基础
开发语言·后端·golang
BinaryBardC8 小时前
Bash语言的软件工程
开发语言·后端·golang
凡人的AI工具箱8 小时前
每天40分玩转Django:Django DevOps实践指南
运维·后端·python·django·devops
土豆凌凌七8 小时前
GO:sync.Map
开发语言·后端·golang
蟹黄堡在逃员工9 小时前
消息队列MQ(一)
java·后端
小兵张健9 小时前
记一个 IDEA 关于 Git 的神坑
git·后端·intellij idea
@_@哆啦A梦9 小时前
Django中自定义模板字符串
后端·python·django
栗豆包9 小时前
w148基于spring boot的文档管理系统的设计与实现
java·spring boot·后端·spring·tornado