转:http://blog.csdn.net/jiangtao_st/article/details/8256610

一、下载jar包

https://github.com/xetorthio/jedis/downloads

学习参考内容 :

http://blog.nosqlfan.com/html/3537.html

二、在spring 中的配置

  1. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  2. <property name="maxActive"  value="50" />
  3. <property name="maxIdle" value="10" />
  4. <property name="maxWait" value="1000" />
  5. <property name="testOnBorrow"  value="true"/>
  6. </bean>
  7. <bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">
  8. <constructor-arg index="0" value="这里填你的reids服务器ip" />
  9. <constructor-arg index="1" value="6379" />
  10. </bean>
  11. <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
  12. <constructor-arg index="0" ref="jedisPoolConfig" />
  13. <constructor-arg index="1">
  14. <list>
  15. <ref bean="jedis.shardInfo" />
  16. </list>
  17. </constructor-arg>
  18. </bean>

三、在Java代码中的使用

publicclassRedis{
    publicstaticvoidmain(Stringargs[]){
 
        //连接redis服务
        Jedisjedis=newJedis("192.168.0.234",6400);          
 
        //密码验证-如果你没有设置redis密码可不验证即可使用相关命令
        jedis.auth("abcdefg");
 
        //简单的key-value
存储
        jedis.set("tn","thisuc");
        System.out.println(jedis.get("tn"));
 
        //在原有值得基础上添加,如若之前没有该key,则导入该key
        //之前已经设定了redis对应"myredis",此句执行便会使redis对应"myredisyourredis"
        jedis.append("redis","yourredis");
        jedis.append("content","rabbit");
 
        //mset
是设置多个key-value值 参数(key1,value1,key2,value2,...,keyn,valuen)
        //mget
是获取多个key所对应的value值 参数(key1,key2,key3,...,keyn) 返回的是个
 
        jedis.mset("name1","thisuc","name2","gaoxc","name3","lemon");
        System.out.println(jedis.mget("name1","name2","name3"));
    }
}
05-28 09:36