Jedis

作者: 荒天帝886 | 来源:发表于2019-06-27 11:21 被阅读0次
  • 安装依赖
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
  • 连接redis服务
Jedis jedis = new Jedis("主机名");
  • set, get方法
jedis.set("key", value);
jedis.get("key");
  • jedis关闭服务
jedis.shutdown();

完整示例

package com.fzy.javastudy.java.day_0626;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import redis.clients.jedis.*;

import java.io.IOException;

public class RedisTest {
    //新建jedis对象
    private final static Jedis jedis = new Jedis("47.93.35.143");

    //初始化密码
    static {
        jedis.auth("redis123456");
    }

    public static void main(String[] args) {
        jedis.set("username", "fdsfdsfdsfsdfdsf分行第三");
        System.out.println(jedis.get("username"));
    }

    @Test
    public void test() {
        Animal animal = new Animal();
        animal.setName("dog");
        animal.setColor("black");
        animal.setAge(3);
        animal.setLeg(4);
        try {
            String animalJon = new ObjectMapper().writeValueAsString(animal);
            String s = jedis.set("animal", animalJon);
            System.out.println(s);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        String animalJson = jedis.get("animal");
        System.out.println(animalJson);
        try {
            Animal animal = new ObjectMapper().readValue(animalJson, Animal.class);
            System.out.println(animal);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

相关文章

网友评论

      本文标题:Jedis

      本文链接:https://www.haomeiwen.com/subject/vxmlcctx.html