Java连接redis两种方式

一、原生方法连接redis

第一步:加依赖

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
</dependency>

第二步:

启动redis的服务。
在这里插入图片描述

第三步:

获得连接对象,连接对象分装操作redis的方法:

Jedis jedis=new Jedis("127.0.0.1");
jedis.set("JEDSI JAVA","hello jedis");
String value=jedis.get("JEDSI JAVA");

弊端:每次连接都会创建jedis对象,严重降低效率。在互联网的背景下》使用springboot添加第二种工厂模式创建连接对象,会大大提高redis的性能

二、通过springboot的jar包

第一步:添加依赖

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

第二步:

1、在application.properties里添加配置:

#端口
spring.redis.port=6379
#redis服务地址
spring.redis.host=127.0.0.1
#redis密码,默认为空
spring.redis.password=

2、在工具包里面写配置类

package com.util;

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;

import java.io.Serializable;
/**
 * @Auth朱定欢
 * 作用:创建连接对象
 * */
@Configuration
public class redisconfig {
    @Bean
    //通过配置类对连接对象配置
    public RedisTemplate<String , Serializable> getRedis(RedisConnectionFactory factory){
        //获得连接对象
        RedisTemplate<String, Serializable> redisTemplate=new RedisTemplate<String, Serializable>();
      //设置连接工厂
       redisTemplate.setConnectionFactory(factory);
       //key进行序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //规定value的序列化格式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
        //value进行序列化
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        //返回连接对象
        return redisTemplate;
    }
}

3、通过控制器进行调用

package com.Controller;

import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Rediscontroller {
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/redis")
    public Object helloRedis(){

        User he=new User();
        he.setAge(11);
        he.setId("yihao");
        he.setName("nihao");
        //字符类型的数据
        ValueOperations  valueOperations=redisTemplate.opsForValue();
        //通过对象在redis对象进行存储
        valueOperations.set("USER_INFO",he);
        //获取刚刚存储的对象
        Object value=valueOperations.get("USER_INFO");
        System.out.println(value);
        //操作hash类型的数据
        HashOperations hashOperations=redisTemplate.opsForHash();
        //操作列表的数据
        ListOperations listOperations=redisTemplate.opsForList();
        //操作有序集合类型的数据
        ZSetOperations zSetOperations=redisTemplate.opsForZSet();
        //操作无序集合类型的数据
        SetOperations setOperations=redisTemplate.opsForSet();
        return value;
    }
}

4、简单类对象:

package com.pojo;

import java.io.Serializable;

public class User implements Serializable {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    private String id;
}

注意:
简单类对象需要进行序列化,否则会报错如下的错。

java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.pojo.User]
 at org.springframework.core.serializer.DefaultSerializer.serialize(DefaultSerializer.java:43) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
 at org.springframework.core.serializer.Serializer.serializeToByteArray(Serializer.java:56) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
 at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:60) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
 at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:33) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
 at org.springframework.da

启动服务就可以调用了。
在redis就可以看到以字符串形式存储的序列化数据
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值