安裝Redis以后,如何使用在Java中呢浊仆?
1、下載 jedis.jar包豫领,這是驅動(Jedis就是redis支持java的第三方類庫抡柿,使用Jedis類庫,才可以操作redis數(shù)據庫)
PS:Jedis2.7以上的版本才支持集群操作等恐。
或者利用Maven引入依賴:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
2洲劣、檢測連接Redis是否成功,代碼如下:
package com.redis;
import redis.clients.jedis.Jedis;
public class ConnectRedis {
public static void main(String[] args) {
//連接本地的 Redis 服務
Jedis jedis = new Jedis("localhost",6379);//默認端口6379 可以省略
// jedis.auth("redis"); //有密碼此步需加上
System.out.println("連接成功");
//查看服務是否運行
System.out.println("服務正在運行: "+jedis.ping());
}
}
由于服務沒啟课蔬,故而打哟鸦:連接成功,然后報錯二跋,啟動Redis战惊,則打印:
連接成功
服務正在運行: PONG
Redis--String:
package com.redis;
import redis.clients.jedis.Jedis;
public class RedisStringJava {
public static void main(String[] args) {
//連接本地的 Redis 服務
Jedis jedis = new Jedis("localhost");
System.out.println("連接成功");
//設置 redis 字符串數(shù)據
jedis.set("hello", "What are you fuck doing?");
// 獲取存儲的數(shù)據并輸出
System.out.println("redis存儲的字符串為: "+ jedis.get("hello"));
}
}
Redis--List:
import java.util.List;
import redis.clients.jedis.Jedis;
public class RedisListJava {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost",6379);
// jedis.auth("redis"); ?
//存儲數(shù)據到列表中
jedis.lpush("site-list", "Java");
jedis.lpush("site-list", "Html5");
jedis.lpush("site-list", "Python");
System.out.println("連接成功");
// 獲取存儲的數(shù)據并輸出
List<String> list = jedis.lrange("site-list", 0 ,2);
for(int i=0; i<list.size(); i++) {
System.out.println("列表項為: "+list.get(i));
}
}
}
Redis--Keys:
import java.util.Iterator;
import java.util.Set;
import redis.clients.jedis.Jedis;
public class RedisKeyJava {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost",6379);
// jedis.auth("redis"); ?
System.out.println("連接成功");
// 獲取數(shù)據并輸出
Set<String> keys = jedis.keys("*");
Iterator<String> it=keys.iterator() ;
while(it.hasNext()){
String key = it.next();
System.out.println(key);
}
}
}
以上就是Redis支持的數(shù)據類型簡單演示扎即,下面我們放入一個對象:
package com.redis;
public class User {
private String id;
private String name;
private int age;
private String sex;
public User() {
super();
}
public User(String id, String name, int age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
//getter和setter方法略
}
package com.redis;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
public class RedisUser {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost",6379);
// jedis.auth("redis"); ?
User u1 = new User(UUID.randomUUID().toString(),"jack1",21,"m");
User u2 = new User(UUID.randomUUID().toString(),"jack2",22,"m");
User u3 = new User(UUID.randomUUID().toString(),"jack3",23,"m");
User u4 = new User(UUID.randomUUID().toString(),"jack4",24,"m");
User u5 = new User(UUID.randomUUID().toString(),"jack5",25,"m");
Map<String,String> userMap = new HashMap<String,String>();
userMap.put("u1",JSON.toJSONString(u1));
userMap.put("u2",JSON.toJSONString(u2));
userMap.put("u3",JSON.toJSONString(u3));
userMap.put("u4",JSON.toJSONString(u4));
userMap.put("u5",JSON.toJSONString(u5));
jedis.hmset("t_user",userMap);
}
}
參考:https://blog.csdn.net/chensizheng/article/details/78309888#commentBox