springboot 可以很方便的接入redis,下面就来看看接入的步骤

准备:
1.idea创建springboot项目,可以参考 , https://blog.csdn.net/diaoling1990/article/details/85946388
2.安装redis,如果没安装可以参考 , https://blog.csdn.net/diaoling1990/article/details/86037089

一、pom.xml配置文件中引入redis依赖

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

二、配置redis的账号密码,地址等等相关信息,如下图
springboot redis 接入(idea)
我是新建了application.yml文件,在里面配置的,配置参数如下

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: 111111
    timeout: 1000
    database: 0
    pool:
      max-active: 10
      max-idle: 8
      min-idle: 2
      max-wait: 100

三、如何使用redis呢,新建TestRedis类

springboot redis 接入(idea)

可以看到,有个 RedisTemplate ; 这个类就是 springboot 提供的操作 redis 的工具类了
代码如下,就可以run 你的 test 方法了

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestRedis {

    @Resource
    private RedisTemplate<String,Object> template;

    @Test
    public void test(){
//        Fruit fruit = new Fruit();
//        fruit.name = "fruit haha";
        template.opsForValue().set("test","test redis haha");
        System.out.println("test values = " + template.opsForValue().get("test"));
    }
}

此时,就可以使用redis了,往redis里面存入值,但是还不能存入对象

想要能存取对象,需要如下配置
四、新建 RedisObjectSerializer 类,实现 RedisSerializer 接口,这就是序列化对象用的类,代码如下

public class RedisObjectSerializer implements RedisSerializer {

    private Converter<Object, byte[]> serializingConverter = new SerializingConverter();
    private Converter<byte[], Object> deserializingConverter = new DeserializingConverter();
    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];

    @Override
    public byte[] serialize(Object o) throws SerializationException {
        if (o == null) {    // 这个时候没有要序列化的对象出现,所以返回的字节数组应该就是一个空数组
            return EMPTY_BYTE_ARRAY ;
        }
        return this.serializingConverter.convert(o);    // 将对象变为字节数组
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length == 0) {    // 此时没有对象的内容信息
            return null ;
        }
        return this.deserializingConverter.convert(bytes);
    }
}

五、新建 RedisConfig 类,配置redis去使用刚刚创建的 RedisObjectSerializer 类去序列化对象
代码如下

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> getRedisTemplate(
            RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // key的序列化类型
        redisTemplate.setValueSerializer(new RedisObjectSerializer()); // value的序列化类型
        return redisTemplate;
    }
}

六、新家bean对象 ,共测试用

public class Fruit implements Serializable {

    public String name;

    @Override
    public String toString() {
        return "Fruit{" +
                "name='" + name + '\'' +
                '}';
    }
}

七、测试redis存取对象,TestRedis 代码修改如下

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestRedis {

    @Resource
    private RedisTemplate<String,Object> template;

    @Test
    public void test(){
        Fruit fruit = new Fruit();
        fruit.name = "fruit haha";
        template.opsForValue().set("test",fruit);
        System.out.println("test values = " + template.opsForValue().get("test"));
    }
}

run 起来,就能成功在控制台看到打印的对应内容了,看吧,使用起来还是很方便的

欢迎加微信 diaoling9090 共同学习Java
springboot redis 接入(idea)
欢迎关注公众号:程序员的日记(搜索chengxuyuanderiji ),持续分享程序员界的日常生活,职业历程,行业资讯
springboot redis 接入(idea)

相关文章:

  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2021-05-26
  • 2022-01-05
  • 2021-04-21
猜你喜欢
  • 2021-12-03
  • 2021-12-31
  • 2021-12-07
  • 2022-12-23
  • 2021-07-21
  • 2021-11-20
  • 2022-12-23
相关资源
相似解决方案