RedisTemplate配置及简单使用

4 篇文章 0 订阅

说明:本文章主要是一个配置类,一个控制类

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <version>2.1.7.RELEASE</version>
</dependency>
spring.redis.host=127.0.0.1
spring.redis.port=6379

配置类

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.StringRedisSerializer;

/**
 * @author 李庆伟
 * @date 2021/1/11 11:06
 */
@Configuration
public class RedisTemplateConfig {

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

        // 设置键(key)的序列化采用StringRedisSerializer。
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        //redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());//设置值(value)的序列化采用jdk
        // 设置值(value)的序列化采用FastJsonRedisSerializer。
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}

控制层

import com.example.dev.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 李庆伟
 * @date 2021/1/14 14:53
 */
@RestController
@RequestMapping("redisTemplateDemo")
public class RedisTemplateController {


    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("addRedisOne")
    public String addRedisOne(){
        List<String> listVal = Arrays.asList("a","b","c");
        redisTemplate.opsForValue().set("listVal",listVal,5, TimeUnit.MINUTES);
        List<String> list = (List<String>) redisTemplate.opsForValue().get("listVal");
        System.out.println(list.get(0));
        return "abc";
    }
    
    @GetMapping("addRedisTwo")
    public String addRedisTwo(){
        User u = new User();
        u.setId("a");
        u.setName("啊啊");
        redisTemplate.opsForValue().set("user",u,5, TimeUnit.MINUTES);
        Object object = redisTemplate.opsForValue().get("user");
        User user = (User) object;
        System.out.println(user.getId());
        return "abc";
    }
    
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中使用RedisTemplate需要进行配置,以下是一个简单的示例配置类: ```java 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.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } } ``` 在上述配置类中,我们创建了一个名为`redisTemplate`的Bean,它是`RedisTemplate<String, Object>`类型的实例。我们使用`RedisConnectionFactory`作为连接工厂,并设置了四个序列化器: - `KeySerializer`和`HashKeySerializer`使用了`StringRedisSerializer`,用于将键(key)和哈希键(hash key)序列化为字符串; - `ValueSerializer`和`HashValueSerializer`使用了`GenericJackson2JsonRedisSerializer`,用于将值(value)和哈希值(hash value)序列化为JSON格式。 这样配置后,我们就可以在其他组件中通过自动注入的方式使用`RedisTemplate<String, Object>`来操作Redis数据了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值