二:springboot整合redis

二:springboot整合redis

  1. maven依赖导入

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
        <version>2.1.15.RELEASE</version>
    </dependency>
    
  2. yml配置

    spring
    	redis:
    	    host: 127.0.0.1
    	    port: 6379
    	    password: 123456
    	    # Redis数据库索引(默认为0)
    	    database: 0
    	    # 连接超时时间(毫秒)0为禁用
    	    timeout: 1000
    
  3. 自定义redisTemplate替换spring boot的redisTemplate

    package com.lmy.config;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.context.annotation.Bean;
    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 org.springframework.stereotype.Component;
    
    /**
     * @author : liu ming yong
     * @date : 2023/3/1 下午 2:44
     * @description : 自定义redisTemplate,解决序列化及强转RedisTemplate => RedisTemplate问题
     */
    @Component
    public class RedisConfig {
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            // 泛型改成 String Object,方便我们的使用
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            // Json序列化配置
            // 使用 json解析对象
            Jackson2JsonRedisSerializer objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
            ObjectMapper om = new ObjectMapper();
            // 通过 ObjectMapper进行转义
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            objectJackson2JsonRedisSerializer.setObjectMapper(om);
            // String的序列化
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
            // key和 hashKey采用 string序列化
            template.setKeySerializer(stringRedisSerializer);
            template.setHashKeySerializer(stringRedisSerializer);
            // value和 hashValue采用 JSON序列化
            template.setValueSerializer(objectJackson2JsonRedisSerializer);
            template.setHashValueSerializer(objectJackson2JsonRedisSerializer);
    
            template.setConnectionFactory(redisConnectionFactory);
            template.afterPropertiesSet();
            return template;
        }
    }
    
  4. 单元测试

    package com.lmy.redis;
    
    import com.lmy.UtilsDemoApplication;
    import com.lmy.dto.rsp.User;
    import com.lmy.service.TestService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.test.context.ActiveProfiles;
    import org.springframework.test.context.junit4.SpringRunner;
    
    /**
     * @author : liu ming yong
     * @date : 2023/2/28 下午 2:34
     * @description : redis测试
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = UtilsDemoApplication.class)
    @ActiveProfiles(value = {"dev"})
    public class RedisTest {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void testRedis() {
    //        Jedis jedis = new Jedis("101.35.117.72", 6379);
    //        jedis.auth("lmy19960215");
    //        jedis.set("userName","风范");
    //        jedis.set("sex","女");
    //        jedis.set("age","1");
    //        System.out.println(jedis.get("userName")+"是个"+jedis.get("age")+"岁的"+jedis.get("sex")+"童鞋");
    
    //        Boolean userName = redisTemplate.delete("userName");
    //        Boolean age = redisTemplate.delete("age");
    //        Boolean sex = redisTemplate.delete("sex");
    //
    //        redisTemplate.opsForValue().set("userName","啊hi");
    //        redisTemplate.opsForValue().set("age","2");
    //        redisTemplate.opsForValue().set("sex","女");
    
            User user = new User();
            user.setUserName("回啊");
            user.setAge("3");
            user.setSex("女");
    
    //        redisTemplate.opsForValue().set("user",user);
            System.out.println(redisTemplate.opsForValue().get("user"));
    
           Object o =  redisTemplate.opsForValue().get("user");
           if (o!=null) {
               User myUser = (User)o;
               System.out.println(myUser.getUserName()+"是个"+myUser.getAge()+"岁的"+myUser.getSex()+"童鞋");
           }
    
        }
    }
    

你可能感兴趣的:(redis,redis,spring,boot,java)