Redis快速入门(SpringBoot整合 三)

SpringBoot整合

SpringBoot 操作数据:spring-data jpa jdbc mongodb redis
SpringData 也是和 SpringBoot 齐名的项目。

整合测试一下

  • 配置 (application.properties)
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
  • 在SpringBoot的测试类里测试
    @SpringBootTest
    class Redis02SpringbootApplicationTests {
    
        @Autowired
        private RedisTemplate redisTemplate;
        
        @Test
        void contextLoads() {
    
            /**
             * redisTemplate.opsForValue()    
             * redisTemplate.opsForList() : 操作List
             * redisTemplate.opsForHash() : 操作hash
             * redisTemplate.opsForHyperLogLog() : 操作HyperLogLog
             */
            redisTemplate.opsForValue().set("study","Java");
            System.out.println(redisTemplate.opsForValue().get("study"));
    
        }
    
    }

关于对象的保存

需要将对象进行序列化,一种方式是使用下边这种方式进行序列化,另一种方式我们可以让User这个类来实现序列化接口

//User这个类来实现序列化接口
public class User implements Serializable{}
*******************************************************
@Test
public void test() throws JsonProcessingException {
    // 真实的开发一般都使用json来传递对象
    User user = new User("翟千祎", 20);
    /**
     * 序列化是什么呢?
     *  序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程。
     *  在序列化期间,对象将其当前状态写入到临时或持久性存储区。
     *  以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。
     */
    // 使用 new ObjectMapper().writeValueAsString(user) 来序列化我们的user对象,将user序列化为json数据
    // 如果不适用下边这一行代码将User对象序列化,直接在使用redis来设置值的时候将user传入,就会报错
    String jsonUser = new ObjectMapper().writeValueAsString(user);
    redisTemplate.opsForValue().set("user",jsonUser);
    System.out.println(redisTemplate.opsForValue().get("user"));

}

编写自己的redis序列化配置

我们为什么要自己来写redis 的序列化呢?
默认的序列化是jdk序列化,在使用key set “user” value命令的时候,key是作为String类型存入的,当我们用命令行来查询key的时候,会发现,key值已经不是我们认识的key值了("\xac\xed\x00\x05t\x00\x05study"),当我们自己序列化过key值之后,我们再次查询key就可以查到了(“study”)。

固定模板

package com.qy.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;

@Configuration
public class RedisConfig {

    /**
     *               固定模板!
     *            可直接使用 。
     */

    // 编写我们自己的 RedisTemplate
    	@Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){
        // 我们为了自己开发方便,一般直接使用 <String,Object>
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // Json 的序列化配置
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        // String 的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // key 采用 String 的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash 的key也采用 String 的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value 序列化方式采用 jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash 的 value 也采用 jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }

}

Redis 一般不适用原生态的来写命令,一般会封装一个 RedisUtils 来对命令进行封装
使用更加方便

猜你喜欢

转载自blog.csdn.net/qq_45260619/article/details/105716439