安装 windows版的Redis,打开即可,默认端口6379
导入两个jar包 commons-pool2-2.3.jar jedis-2.7.0.jar
一 写配置文件
redis.setMaxIdle=20redis.setMinIdle=10redis.setMaxTotal=50redis.url=localhostredis.port=6379
二 建立java redis 即 jedis连接池
1 package redistest; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Properties; 6 7 import redis.clients.jedis.Jedis; 8 import redis.clients.jedis.JedisPool; 9 import redis.clients.jedis.JedisPoolConfig;10 11 public class RedisConfigUtil {12 private static JedisPool pool;13 static{14 InputStream in = RedisConfigUtil.class.getClassLoader().getResourceAsStream("redis.properties");15 Properties pro=new Properties();16 try {17 pro.load(in);18 } catch (IOException e) {19 // TODO Auto-generated catch block20 e.printStackTrace();21 }22 JedisPoolConfig poolConfig=new JedisPoolConfig();23 poolConfig.setMaxIdle(Integer.valueOf(pro.getProperty("redis.setMaxIdle")));24 poolConfig.setMaxTotal(Integer.valueOf(pro.getProperty("redis.setMaxTotal")));25 poolConfig.setMinIdle(Integer.valueOf(pro.getProperty("redis.setMinIdle")));26 pool=new JedisPool(poolConfig,pro.getProperty("redis.url"), Integer.valueOf(pro.getProperty("redis.port")));27 }28 public static Jedis getJedis(){29 return pool.getResource();30 }31 public static void closeJedis(Jedis je){32 je.close();33 pool.close();34 }35 }
读取配置文件的redis数据库信息
三 redis的基本操作
setget 用于字符串,可以用json的思路去存数据
map set list 方法和字符串的不同
1 package redistest; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Map.Entry; 8 9 import org.junit.Test;10 11 import redis.clients.jedis.Jedis;12 import redis.clients.jedis.JedisPool;13 import redis.clients.jedis.JedisPoolConfig;14 15 public class RedisTest {16 /**17 * 18 */19 @Test20 public void main2() {21 /*JedisPoolConfig poolConfig=new JedisPoolConfig();22 poolConfig.setMaxIdle(20);23 poolConfig.setMinIdle(10);24 poolConfig.setMaxTotal(50);;25 JedisPool pool=new JedisPool(poolConfig, "localhost", 6379);26 //Jedis jedis=new Jedis("localhost", 6379);27 Jedis jedis = pool.getResource();*/28 Jedis jedis = RedisConfigUtil.getJedis();29 //添加数据30 jedis.set("xx", "wuchao22");31 //拼接字符串32 jedis.append("xx", "333");33 //删除数据34 jedis.del("xx");35 //设置多个键值对36 jedis.mset("name","wuchao","age","22");37 jedis.incr("age");//+138 //System.out.println(jedis.get("age"));39 40 /*41 * Map操作42 */43 Mapmap=new HashMap<>();44 map.put("name", "wu");45 map.put("age", "22");46 map.put("qq", "123");47 //map 集合遍历48 for (Map.Entry enrty:map.entrySet()) {49 //System.out.println(enrty.getValue());50 }51 //jedis.hmset("user", map);52 jedis.hdel("user", "qq");//删除一个键值53 List hmget = jedis.hmget("user","name", "age","qq");54 //System.out.println(hmget);55 //System.out.println(jedis.hkeys("user"));//获得所有键名56 //System.out.println(jedis.hvals("user"));//获得所以键值57 /*Iterator iter=jedis.hkeys("user").iterator();58 while(iter.hasNext()){59 String key=iter.next();60 System.out.println(jedis.hmget("user", key));61 }*/62 63 /*64 * list集合65 */66 jedis.del("list");67 jedis.rpush("list", "spring");68 jedis.rpush("list", "struts");69 jedis.rpush("list", "hibernate");70 System.out.println(jedis.lrange("list",0, -1));71 System.out.println(jedis.llen("list"));//获取长度72 73 /*74 * set集合75 */76 jedis.del("user1");77 jedis.sadd("user1", "a");78 jedis.sadd("user1", "b");79 jedis.sadd("user1", "c");80 jedis.sadd("user1", "d");81 jedis.srem("user1","d");//删除82 System.out.println(jedis.smembers("user1"));83 System.out.println(jedis.scard("user1"));//返回集合个数84 85 /*86 * 集合排序87 */88 jedis.rpush("sort","1");89 jedis.lpush("sort","6");90 jedis.rpush("sort","3");91 jedis.lpush("sort","7");92 jedis.rpush("sort","2");93 //System.out.println(jedis.lrange("sort", 0, -1));94 System.out.println(jedis.sort("sort"));//排序95 System.out.println(jedis.lrange("sort", 0, -1));96 }97 }
以上列举了些基本的操作