<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
Jedis jedis = new Jedis("主机名");
jedis.set("key", value);
jedis.get("key");
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();
}
}
}
网友评论