1.创建项目,项目名称redisdemo,目录结构如图所示
2.RedisJava文件的内容如下package com.demo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisJava {
public static void main(String[] args){
Jedis jedis = null;
//服务器IP
String add = "127.0.01";
//端口号
int port = 6379;
//最大可用数量
int max_active = 1024;
//空闲jedis实例数
int max_idle = 200;
//等待时间,毫秒数
int max_wait = 10000;
//超时时间
int timeout = 10000;
//验证操作,验证后的jedis都是可用的
boolean test_on_borrow = true;
//连接池对象
JedisPool jedisPool = null;
//配置连接池
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(max_active);
config.setMaxIdle(max_idle);
config.setMaxWait(max_wait);
config.setTestOnBorrow(test_on_borrow);
jedisPool = new JedisPool(config,add,port,timeout);
if(jedisPool != null){
jedis = jedisPool.getResource();
}
if(jedis != null){
/*
* 操作字符串
*/
//1.添加数据
jedis.set("name", "xiaoxiao");
//获取数据
System.out.println(jedis.get("name"));
//2.向已有的key中拼接
jedis.append("name", " i love you");
System.out.println(jedis.get("name"));
//3.删除key
jedis.del("name");
System.out.println(jedis.get("name"));
//4.设置多个键值对
jedis.msetnx("name","xiaoxiao","age","20","email","dengyunshuo@163.com");
System.out.println(jedis.get("name"));
System.out.println(jedis.get("age"));
System.out.println(jedis.get("email"));
//5.对key age进行加1操作
jedis.incr("age");
System.out.println(jedis.get("age"));
System.out.println("-----------------------------------我是分隔线---------------------------------");
/*
* 操作集合Map
*/
//1.添加数据
Map maps = new HashMap();
maps.put("name", "xiaoxiao");
maps.put("age", "22");
maps.put("email", "dengyunshuo@163.com");
jedis.hmset("mymap", maps);
//获取map中的数据
List lists = jedis.hmget("mymap", "name","age","email");
System.out.println(lists);
//2.删除map中的键值
jedis.hdel("mymap", "email");
System.out.println(jedis.hmget("mymap", "email"));
//3.获取mymap中元素个数
System.out.println(jedis.hlen("mymap"));
//4.判断mymap是否存在
System.out.println(jedis.exists("mymap"));
//5.获取mymap中的所有key
System.out.println(jedis.hkeys("mymap"));
//6.获取mymap中的所有值
System.out.println(jedis.hvals("mymap"));
//7.遍历mymap
Iterator it = jedis.hkeys("mymap").iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + "=>" + jedis.hget("mymap", key));
}
System.out.println("-----------------------------------我是分隔线---------------------------------");
/*
* 操作List
*/
//1.向List中添加数据
jedis.lpush("mylist", "redis");
jedis.lpush("mylist", "git");
jedis.lpush("mylist", "maven");
System.out.println(jedis.lrange("mylist", 0, -1));
//2.删除List
jedis.del("mylist");
//3.将一个或多个值插入到列表的尾部
jedis.rpush("mylist", "redis");
jedis.rpush("mylist", "git");
jedis.rpush("mylist", "gradle");
//4.输出mylist中所有的元素
System.out.println(jedis.lrange("mylist", 0, -1));
System.out.println("-----------------------------------我是分隔线---------------------------------");
/*
* 操作Set
*/
//1.添加数据
jedis.sadd("myset", "gaoneng");
jedis.sadd("myset", "banya");
jedis.sadd("myset", "houzi");
jedis.sadd("myset", "pangzi");
//2.获取所有的值
System.out.println(jedis.smembers("myset"));
//3.判断元素是否存在
System.out.println(jedis.sismember("myset", "gaoneng"));
//4.删除元素
jedis.srem("myset", "gaoneng");
System.out.println(jedis.smembers("myset"));
//5.返回集合中的一个随机元素
System.out.println(jedis.srandmember("myset"));
//6.返回集合中的元素个数
System.out.println(jedis.scard("myset"));
System.out.println("-----------------------------------我是分隔线---------------------------------");
/*
* List 排序
*/
jedis.del("orderlist");
jedis.rpush("orderlist", "1");
jedis.rpush("orderlist", "9");
jedis.rpush("orderlist", "3");
jedis.lpush("orderlist", "6");
jedis.lpush("orderlist", "4");
jedis.lpush("orderlist", "8");
System.out.println(jedis.lrange("orderlist", 0, -1));
//输出排序后的结果
System.out.println(jedis.sort("orderlist"));
System.out.println(jedis.lrange("orderlist", 0, -1));
}
}
}
3.运行程序执行结果如图所示