zoukankan      html  css  js  c++  java
  • SpringBoot整合Redis

    SpringBoot整合Redis

    首先创建一个springboot项目,在创建时勾选spring data下的redis或者导入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    

    在使用之前,需要在application.properties中配置redis的参数

    # Redis数据库索引(默认为0)
    spring.redis.database=0
    
    # redis的地址
    spring.redis.host=127.0.0.1
    
    #redis连接端口
    spring.redis.port=6379
    
    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.jedis.pool.max-active=200
    
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.jedis.pool.max-wait=-1ms
    
    # 连接池中的最大空闲连接
    spring.redis.jedis.pool.max-idle=10 
    
    # 连接池中的最小空闲连接
    spring.redis.jedis.pool.min-idle=0  
    
    # 连接超时时间(毫秒)
    spring.redis.timeout=1000ms
    

    编写redis的配置类,主要是绑定连接

    @Configuration
    public class RedisConfig {
        @Bean
        @SuppressWarnings("all")
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
            // 为了开发方便,直接使用String -> Object
            // 创建绑定链接
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(factory);
            return template;
        }
    }
    

    然后就可以开始redis的使用了,在使用时装配一个RedisTemplate就可以对redis进行操作,spring对redis的功能进行了分类,在使用之前先选择方法,输入redisTemplate.ops之后,在idea的智能提示中可以看到所有的数据类型

    1613375287476

    下面是测试代码:

    @SpringBootTest
    class SpringsecuritytestApplicationTests {
    
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
        @Test
        void RedisTest() {
            String str = "Hello, World!";
            this.redisTemplate.opsForValue().set("str", str);
            System.out.println((redisTemplate.opsForValue().get("str")));
        }
    
    }
    

    输出Hello, World!,我们设置的redistemplate是String->Object键值对,那么我们直接传入一个对象又会怎么样呢?

    准备一个User类,实现了序列化的接口

    @Component
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public class User implements Serializable {
        private String name;
        private int age;
    }
    

    测试代码:

    @SpringBootTest
    class SpringsecuritytestApplicationTests {
    
        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
    
        @Test
        void RedisTest() {
            User user = new User("YJS", 19);
            this.redisTemplate.opsForValue().set("user", user);
            System.out.println((redisTemplate.opsForValue().get("user")));
        }
    
    }
    

    输出User(name=YJS, age=19)

    也可以实现对一个Java对象的存储,但是查看redis本地存储时,却不是我们想看到的:

    键:"xacxedx00x05tx00x04user"

    值:"xacxedx00x05srx00 com.pojo.User4xb3{xda{m,xc1x02x00x02Ix00x03ageLx00x04nametx00x12Ljava/lang/String;xpx00x00x00x13tx00x03YJS"

    这样的存储不便于调试,所以需要在配置类中配置自己的序列化器

    新配置类:

    @Configuration
    public class RedisConfig {
        @Bean
        @SuppressWarnings("all")
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
            // 为了开发方便,直接使用String -> Object
            // 创建绑定链接
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(factory);
    
            // 序列化配置
            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);
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    
            template.setKeySerializer(stringRedisSerializer);
            template.setHashKeySerializer(stringRedisSerializer);
            template.setValueSerializer(jackson2JsonRedisSerializer);
    
            template.setHashValueSerializer(jackson2JsonRedisSerializer);
            template.afterPropertiesSet();
    
            return template;
        }
    }
    

    重新运行测试代码,看到数据库中的存储

    键: "user"

    值:"["com.pojo.User",{"name":"YJS","age":19}]"

    这样就便于调试和查看存储对象了

    再接着需要可以根据业务需要编写redis的工具类和工具函数,更加便于使用

  • 相关阅读:
    Windows 代码实现关机(直接黑屏)
    Windows SEH学习 x86
    Smali 语法文档
    SIOCADDRT: No such process
    Windbg 常用命令整理
    ida GDB 远程调试
    IDA 使用技巧
    Windows X64 Patch Guard
    C 和C++ 名称修饰规则
    【转载】 硬盘主引导记录(MBR)及其结构详解
  • 原文地址:https://www.cnblogs.com/JoshuaYu/p/15054580.html
Copyright © 2011-2022 走看看