JAVA操作Redis

1.环境装备

1.已经安装上了redis服务器

2.redis服务器已经配置了protected-mode为no

在启动的时候启动

 ./redis-server ../redis.conf   --protected-mode no

一、jedis

1.创建boot工程testredis

2.引入jar包

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
       <dependency>
           <groupId>redis.clients</groupId>
           <artifactId>jedis</artifactId>
           <version>3.6.0</version>
       </dependency>

       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
       </dependency>

3.使用redis存储数据

@Test
   public void set(){
       //1.连接redis
       Jedis jedis = new Jedis("192.168.75.10", 6379);
       //2.操作redis
       jedis.set("hello","helloworld");
       //3.关闭连接
       jedis.close();
  }

4.使用redis获取数据

@Test
   public void get(){
       //1.连接redis
       Jedis jedis = new Jedis("192.168.75.10", 6379);
       //2.操作redis
       String value = jedis.get("hello");
       System.out.println(value);
       //3.关闭连接
       jedis.close();
  }

5.操作list类型数据

@Test
   public void testList(){
       //1.连接redis
       Jedis jedis = new Jedis("192.168.75.10", 6379);
       //2.操作集合
       jedis.lpush("list","a","b","c");
       jedis.rpush("list","x");
       //获取集合长度
       Long size = jedis.llen("list");
       System.out.println(size);
       List<String> list = jedis.lrange("list", 0, -1);
       for (String value:list){
           System.out.println(value);
      }
       //3.关闭连接
       jedis.close();
  }

6.操作hash类型数据

@Test
   public void testHash(){
       //1.连接redis
       Jedis jedis = new Jedis("192.168.75.10", 6379);
       //2.操作hash数据
       //a.存储数据
       jedis.hset("hash","a1","a1value");
       jedis.hset("hash","a2","a2value");
       jedis.hset("hash","a3","a3value");
       //b.获取数据
       String value = jedis.hget("hash", "a1");
       System.out.println("a1的值为:"+value);
       //c.获取数据长度
       Long size = jedis.hlen("hash");
       System.out.println("数据长度为:"+size);
       //d.获取所有数据
       Map<String, String> map = jedis.hgetAll("hash");
       Set<Map.Entry<String, String>> entries = map.entrySet();
       for (Map.Entry<String,String> en:entries){
           System.out.println(en.getKey()+"="+en.getValue());
      }
       //3.关闭redis
       jedis.close();
  }

二、redis启动器

1.创建一个springboot工程

2.加入redis启动器和fast-json依赖

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-data-redis</artifactId>
       </dependency>


       <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
           <version>1.2.79</version>
       </dependency>

3.书写application.yml

spring:
redis:
  host: 192.168.72.111
  port: 6379

4.书写配置类(不然会乱码)


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author Administrator
* @date 2022/1/10
*/

@Configuration
public class RedisConfig {
   @Autowired
   private RedisTemplate redisTemplate;

   /**
    * 解决Redis乱码
    */
   @Bean
   public RedisTemplate<String, Object> stringSerializerRedisTemplate() {
       RedisSerializer<String> stringSerializer = new StringRedisSerializer();
       redisTemplate.setKeySerializer(stringSerializer);
       redisTemplate.setValueSerializer(stringSerializer);
       redisTemplate.setHashKeySerializer(stringSerializer);
       redisTemplate.setHashValueSerializer(stringSerializer);
       return redisTemplate;
  }
}

5.测试使用redis操作字符串

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

@SpringBootTest
class DemoredisApplicationTests {
   @Autowired
   private RedisTemplate redisTemplate;//注入redis操作模板
   @Test
   public void setkeyvalue(){//增加数据
       ValueOperations valueOperations = redisTemplate.opsForValue();
       valueOperations.set("hello","worldhello");
  }
   @Test
   public void getkeyvalue(){//获取数据
       ValueOperations valueOperations = redisTemplate.opsForValue();
       String value = valueOperations.get("hello").toString();
       System.out.println(value);
  }
}