Java --- springboot3整合redis

目录​​​​​​​

一、整合redis

1.1、导入pom依赖

1.2、修改springboot配置文件

1.3、代码测试 

二、测试访问redis五大常用数据类型

三、自动配置原理

四、定制化

4.1、解决redis存储序列化乱码问题 

4.2、redis客户端使用jedis


一、整合redis

1.1、导入pom依赖


            org.springframework.boot
            spring-boot-starter-data-redis
        

1.2、修改springboot配置文件

Java --- springboot3整合redis_第1张图片

1.3、代码测试 

@RestController
public class RedisController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @GetMapping("/count")
    public String count(){
        Long count = stringRedisTemplate.opsForValue().increment("k1");
        return "访问了"+count+"次";
    }
}

二、测试访问redis五大常用数据类型

@SpringBootTest
class Boot3RedisApplicationTests {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 普通字符串
     */
    @Test
    void contextLoads() {
        stringRedisTemplate.opsForValue().set("test1", UUID.randomUUID().toString());
        String k = stringRedisTemplate.opsForValue().get("test1");
        System.out.println(k);
    }

    /**
     * list类型
     */
    @Test
    void testList(){
        stringRedisTemplate.opsForList().leftPush("a1","1");
        stringRedisTemplate.opsForList().leftPush("a1","2");
        stringRedisTemplate.opsForList().leftPush("a1","3");
        String a1 = stringRedisTemplate.opsForList().leftPop("a1");
        Assertions.assertEquals("3",a1);
    }

    /**
     * set类型
     */
    @Test
    void testSet(){
        stringRedisTemplate.opsForSet().add("set1","1","2","3");
        Boolean set1 = stringRedisTemplate.opsForSet().isMember("set1", "2");
        Assertions.assertTrue(set1);
    }

    /**
     * zset类型
     */
    @Test
    void testZSet(){
        stringRedisTemplate.opsForZSet().add("zset1","张三",12);
        stringRedisTemplate.opsForZSet().add("zset1","李四",13);
        stringRedisTemplate.opsForZSet().add("zset1","王五",14);
        ZSetOperations.TypedTuple zset1 = stringRedisTemplate.opsForZSet().popMax("zset1");
        System.out.println(zset1.getValue()+":"+zset1.getScore());
    }

    /**
     * hash类型
     */
    @Test
    void testHash(){
        stringRedisTemplate.opsForHash().put("hash1","name","tom");
        stringRedisTemplate.opsForHash().put("hash1","age","12");
    }
}

三、自动配置原理

Java --- springboot3整合redis_第2张图片

四、定制化

@Data
public class Person implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
}
 @GetMapping("/addPerson")
    public void addPerson(){
        Person person = new Person();
        person.setId(1);
        person.setName("tom");
        person.setAge(12);
        //序列化,将对象转为字符串
        redisTemplate.opsForValue().set("person",person);
    }
    @GetMapping("/getPerson")
    public Person getPerson(){
        Person person = (Person) redisTemplate.opsForValue().get("person");
        return person;
    }

4.1、解决redis存储序列化乱码问题 

发现存储在redis中的数据乱码,不可视

Java --- springboot3整合redis_第3张图片

 因为RedisTemplate使用的是默认序列化机制,为了方便其他系统交互应使用JOSN数据交互

解决代码如下:

@Configuration
public class AppRedisConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //把对象转为json字符串的序列化
        redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
}

Java --- springboot3整合redis_第4张图片

4.2、redis客户端使用jedis

 RedisTemplate底层默认使用lettuce


        
            org.springframework.boot
            spring-boot-starter-data-redis
            
                
                    io.lettuce
                    lettuce-core
                
            
        
        
            redis.clients
            jedis
        

Java --- springboot3整合redis_第5张图片

 

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