springboot整合redis超级详解

redis中文官网:http://www.redis.cn/
redis-windows安装下载:https://github.com/tporadowski/redis/releases
redis-linux下载:https://github.com/redis/redis/tags
接下来正式实现整合,在springboot中使用redis

1,创建springboot项目
2,需要的依赖
	jedis,同时需要使用下面的fastjson
	<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
	<dependency>
		<groupId>redis.clients</groupId>
		<artifactId>jedis</artifactId>
		<version>3.3.0</version>
	</dependency>
	fastjson
	<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>1.2.75</version>
	</dependency>

也可以去官网中下载其他版本

3,application.properties配置
#配置redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
4,在项目加载完之后,可以分析一波源码

在外部库中就能发现这个redis已经加载进来
在这里插入图片描述
点进去后可以发现,在每个对应的自动配置类里面,都有对应的配置文件
在这里插入图片描述
在点开配置文件类中就可以发现,redis基本操作就在这个对应的配置文件中了
如一些使用的默认数据库为第0个,url,主机等,不过实际开发中一般在linux下,并且这些命令一般不会用代码实现,而是使用配置文件,在配置文件中修改这些指令
在这里插入图片描述

5,进行测试,看是否可以使用redis
package com.example.bootredis;

import com.example.bootredis.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class BootredisApplicationTests {
    
    

    @Autowired
    //@Qualifier("redisTemplate")
    //重新编写的模板
    private RedisTemplate redisTemplate;
    long s1 = System.currentTimeMillis();
    @Test
    void contextLoads() {
    
    
        //opsForValue:操作字符串
        //opsForList:操作list
        //set,hash,geo,zset...等
        //redisTemplate可以操作我们的常用方法
        //获取redis的连接对象
        //RedisConnection rc = redisTemplate.getConnectionFactory().getConnection();
        redisTemplate.opsForValue().set("name","ZhengHuiSheng");
        String name = (String)redisTemplate.opsForValue().get("name");
        long s2 = System.currentTimeMillis();
        System.out.println(name);
        System.out.println(s2 - s1);

    }
//    @Test
//    public void test(){
    
    
//        //在开发中一般都使用json来传递对象
//        User user = new User("郑辉盛",18);
//        //向redis中添加数据
//        redisTemplate.opsForValue().set("user",user);
//        System.out.println(redisTemplate.opsForValue().get("user"));
//    }
}
6,测试发现并不能成功

原因:没有在我们安装在本地的redis下启动redis-server.exe以及redis-cli.exe,安装连接在一开始就已经说了,
这里再说一下:https://github.com/tporadowski/redis/releases
安装好之后也不用配置环境,主机和端口默认为127.0.0.1 6379
在这里插入图片描述

7,在windows中测试一下,依次打开redis-server.exe和redis-cli.exe,在redis-cli.exe中输入ping,如果能得到pong,name就连接成功,本地redis就可以正常使用了

在这里插入图片描述

8,重新进行测试,项目就能运行成功了,可以自己试着debug一下,这样才能知道程序干了什么,以及快速进行排错
9,再保存一个对象试试

新建一个pojo包,下面建一个User类,一般开发中实体类都是需要进行序列化的

package com.example.bootredis.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.Serializable;

@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
    
    
    private String name;
    private Integer age;
}
10,再进行测试,加入到上一段测试代码中
@Test
    public void test(){
    
    
        //在开发中一般都使用json来传递对象
        User user = new User("郑辉盛",18);
        //向redis中添加数据
        redisTemplate.opsForValue().set("user",user);
        System.out.println(redisTemplate.opsForValue().get("user"));
    }
11,然而实体类序列化只是在jdk中序列化,在我们进行对象保存时可能使用json进行序列化,因此在的查询设置的对象可以发现一串乱码,原因是在RedisTemplate类中并未进行序列化处理

在这里插入图片描述

在源码中可以发现并未进行序列化,序列化默认为jdk序列化
在这里插入图片描述
在这里插入图片描述

12,新建一个config包,下面新建一个RedisConfig配置类,相当于自定义一个模板配置类
package com.example.bootredis.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.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;

@Configuration
public class RedisConfig {
    
    
    //自定义了一个RedisTemplate
    @Bean
    @SuppressWarnings("all")    //告诉编译器忽略指定的警告,不用在编译完成后出现警告信息
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    
    
        //为了自己开发方便,使用String,Object类型
        RedisTemplate<String, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        //序列化配置,使用json解析任意的对象,将对象解析成可以序列化的对象
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        //使用Mapper对象进行转义
        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采用String序列化的方式
        template.setHashKeySerializer(stringRedisSerializer);
        //value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //hash的value序列化方式采用jackson
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
13,先在redis中flushdb(清空)一下数据库,在重新进行测试,那么使用的模板配置类肯定就要使用自定义配置的模板类了,终极测试
	@Autowired
    @Qualifier("redisTemplate")
    //重新编写的模板
    private RedisTemplate redisTemplate;
	@Test
    public void test(){
    
    
        //在开发中一般都使用json来传递对象
        User user = new User("郑辉盛",18);
        //向redis中添加数据
        redisTemplate.opsForValue().set("user",user);
        System.out.println(redisTemplate.opsForValue().get("user"));
    }
14,结果 user获取成功

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zhenghuishengq/article/details/111940358