[转帖]spring 结合 Redis 例子,简单入门例子_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3995 | 回复: 0   主题: [转帖]spring 结合 Redis 例子,简单入门例子        下一篇 
Leon
注册用户
等级:少校
经验:1436
发帖:116
精华:7
注册:2013-1-4
状态:离线
发送短消息息给Leon 加好友    发送短消息息给Leon 发消息
发表于: IP:您无权察看 2013-1-7 12:59:39 | [全部帖] [楼主帖] 楼主

oyhk 学习笔记

好了费话不多说了,介绍下spring 结合redis是怎么操作数据的 这里我用了maven管理,由于简单嘛,依赖下包就行了..不用单独去依赖包,成了我的习惯


好了,下面是pom的代码

pom.xml


Xml代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>redis</groupId>
  5. <artifactId>redis</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <build>
  8. </build>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework.data</groupId>
  12. <artifactId>spring-data-redis</artifactId>
  13. <version>1.0.2.RELEASE</version>
  14. </dependency>
  15. </dependencies>
  16. </project>


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>redis</groupId>
<artifactId>redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>

    在pom里添加了redis spring客户端的依赖,那么所有的jar包都会帮你自动下载下来,是不是很方便啊,哈


下面是spring-context.xml代码

Xml代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:util="http://www.springframework.org/schema/util"xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd   
  7. http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd   
  9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
  10. <!--注解说明 -->  
  11. <context:annotation-config/>
  12. <!-- 把标记了@Controller注解的类转换为bean -->  
  13. <context:component-scanbase-package="com.mkfree.**"/>
  14. <!-- redis工厂 -->  
  15. <beanid="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  16. p:host-name="192.168.9.140"p:port="6379"p:password="87980879"/>
  17. <!-- redis服务封装 -->  
  18. <beanid="redisService"class="com.mkfree.redis.test.RedisService">
  19. </bean>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<!--注解说明 -->
<context:annotation-config />
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.mkfree.**" />
<!-- redis工厂 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="192.168.9.140" p:port="6379" p:password="87980879" />
<!-- redis服务封装 -->
<bean id="redisService" class="com.mkfree.redis.test.RedisService">
</bean>

    这里配置了一个跟spring 集成的redis客户端,ip port password自己定哦,这里我在redis配置文件了定义了需求密码认证,你们先不要用吧,为了简单起见


下面是RedisService,里面包含了对redis的方法,还有更多的方法没有去使用,这里当一个入门的小例子吧

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. package com.mkfree.redis.test; 
  2. import java.util.Set; 
  3. import org.springframework.beans.factory.annotation.Autowired; 
  4. import org.springframework.beans.factory.annotation.Qualifier; 
  5. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 
  6. import redis.clients.jedis.Jedis; 
  7. /** 
  8.  * 封装redis 缓存服务器服务接口 
  9.  * @author hk 
  10.  * 
  11.  * 2012-12-16 上午3:09:18 
  12.  */ 
  13. public class RedisService { 
  14.       
  15.        /** 
  16.        * 通过key删除(字节) 
  17.        * @param key 
  18.        */ 
  19.        public void del(byte [] key){ 
  20.              this.getJedis().del(key); 
  21.        } 
  22.        /** 
  23.        * 通过key删除 
  24.        * @param key 
  25.        */ 
  26.        public void del(String key){ 
  27.              this.getJedis().del(key); 
  28.        } 
  29.       
  30.        /** 
  31.        * 添加key value 并且设置存活时间(byte) 
  32.        * @param key 
  33.        * @param value 
  34.        * @param liveTime 
  35.        */ 
  36.        public void set(byte [] key,byte [] value,int liveTime){ 
  37.              this.set(key, value); 
  38.              this.getJedis().expire(key, liveTime); 
  39.        } 
  40.        /** 
  41.        * 添加key value 并且设置存活时间 
  42.        * @param key 
  43.        * @param value 
  44.        * @param liveTime 
  45.        */ 
  46.        public void set(String key,String value,int liveTime){ 
  47.              this.set(key, value); 
  48.              this.getJedis().expire(key, liveTime); 
  49.        } 
  50.        /** 
  51.        * 添加key value 
  52.        * @param key 
  53.        * @param value 
  54.        */ 
  55.        public void set(String key,String value){ 
  56.              this.getJedis().set(key, value); 
  57.        } 
  58.        /**添加key value (字节)(序列化) 
  59.        * @param key 
  60.        * @param value 
  61.        */ 
  62.        public void set(byte [] key,byte [] value){ 
  63.              this.getJedis().set(key, value); 
  64.        } 
  65.        /** 
  66.        * 获取redis value (String) 
  67.        * @param key 
  68.        * @return 
  69.        */ 
  70.        public String get(String key){ 
  71.              String value = this.getJedis().get(key); 
  72.              return value; 
  73.        } 
  74.        /** 
  75.        * 获取redis value (byte [] )(反序列化) 
  76.        * @param key 
  77.        * @return 
  78.        */ 
  79.        public byte[] get(byte [] key){ 
  80.              return this.getJedis().get(key); 
  81.        } 
  82.       
  83.        /** 
  84.        * 通过正则匹配keys 
  85.        * @param pattern 
  86.        * @return 
  87.        */ 
  88.        public Set<String> keys(String pattern){ 
  89.              return this.getJedis().keys(pattern); 
  90.        } 
  91.       
  92.        /** 
  93.        * 检查key是否已经存在 
  94.        * @param key 
  95.        * @return 
  96.        */ 
  97.        public boolean exists(String key){ 
  98.              return this.getJedis().exists(key); 
  99.        } 
  100.        /** 
  101.        * 清空redis 所有数据 
  102.        * @return 
  103.        */ 
  104.        public String flushDB(){ 
  105.              return this.getJedis().flushDB(); 
  106.        } 
  107.        /** 
  108.        * 查看redis里有多少数据 
  109.        */ 
  110.        public long dbSize(){ 
  111.              return this.getJedis().dbSize(); 
  112.        } 
  113.        /** 
  114.        * 检查是否连接成功 
  115.        * @return 
  116.        */ 
  117.        public String ping(){ 
  118.              return this.getJedis().ping(); 
  119.        } 
  120.        /** 
  121.        * 获取一个jedis 客户端 
  122.        * @return 
  123.        */ 
  124.        private Jedis getJedis(){ 
  125.              if(jedis == null){ 
  126.                    return jedisConnectionFactory.getShardInfo().createResource(); 
  127.              } 
  128.              return jedis; 
  129.        } 
  130.        private RedisService (){ 
  131.             
  132.        } 
  133.        //操作redis客户端 
  134.        private static Jedis jedis; 
  135.       @Autowired
  136.       @Qualifier("jedisConnectionFactory") 
  137.        private JedisConnectionFactory jedisConnectionFactory; 


package com.mkfree.redis.test;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import redis.clients.jedis.Jedis;

/**
* 封装redis 缓存服务器服务接口
* @author hk
*
* 2012-12-16 上午3:09:18
*/
public class RedisService {

       /**
       * 通过key删除(字节)
       * @param key
       */
       public void del(byte [] key){
             this.getJedis().del(key);
       }
       /**
       * 通过key删除
       * @param key
       */
       public void del(String key){
             this.getJedis().del(key);
       }

       /**
       * 添加key value 并且设置存活时间(byte)
       * @param key
       * @param value
       * @param liveTime
       */
       public void set(byte [] key,byte [] value,int liveTime){
             this.set(key, value);
             this.getJedis().expire(key, liveTime);
       }
       /**
       * 添加key value 并且设置存活时间
       * @param key
       * @param value
       * @param liveTime
       */
       public void set(String key,String value,int liveTime){
             this.set(key, value);
             this.getJedis().expire(key, liveTime);
       }
       /**
       * 添加key value
       * @param key
       * @param value
       */
       public void set(String key,String value){
             this.getJedis().set(key, value);
       }
       /**添加key value (字节)(序列化)
       * @param key
       * @param value
       */
       public void set(byte [] key,byte [] value){
             this.getJedis().set(key, value);
       }
       /**
       * 获取redis value (String)
       * @param key
       * @return
       */
       public String get(String key){
             String value = this.getJedis().get(key);
             return value;
       }
       /**
       * 获取redis value (byte [] )(反序列化)
       * @param key
       * @return
       */
       public byte[] get(byte [] key){
             return this.getJedis().get(key);
       }

       /**
       * 通过正则匹配keys
       * @param pattern
       * @return
       */
       public Set<String> keys(String pattern){
             return this.getJedis().keys(pattern);
       }

       /**
       * 检查key是否已经存在
       * @param key
       * @return
       */
       public boolean exists(String key){
             return this.getJedis().exists(key);
       }
       /**
       * 清空redis 所有数据
       * @return
       */
       public String flushDB(){
             return this.getJedis().flushDB();
       }
       /**
       * 查看redis里有多少数据
       */
       public long dbSize(){
             return this.getJedis().dbSize();
       }
       /**
       * 检查是否连接成功
       * @return
       */
       public String ping(){
             return this.getJedis().ping();
       }
       /**
       * 获取一个jedis 客户端
       * @return
       */
       private Jedis getJedis(){
             if(jedis == null){
                   return jedisConnectionFactory.getShardInfo().createResource();
             }
             return jedis;
       }
       private RedisService (){

       }
       //操作redis客户端
       private static Jedis jedis;
       @Autowired
       @Qualifier("jedisConnectionFactory")
       private JedisConnectionFactory jedisConnectionFactory;
}


    下面是测试代码TestRedis.java

Java代码
北京联动北方科技有限公司北京联动北方科技有限公司北京联动北方科技有限公司

  1. package com.mkfree.redis.test; 
  2. import java.util.Set; 
  3. import org.springframework.context.ApplicationContext; 
  4. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  5. /** 
  6.  * redis spring 简单例子 
  7.  * @author hk 
  8.  * 
  9.  * 2012-12-22 上午10:40:15 
  10.  */ 
  11. public class TestRedis { 
  12.       
  13.        public static void main(String[] args) throws InterruptedException { 
  14.              ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml"); 
  15.              //这里已经配置好,属于一个redis的服务接口 
  16.              RedisService redisService = (RedisService) app.getBean("redisService"); 
  17.             
  18.              String ping = redisService.ping();//测试是否连接成功,连接成功输出PONG 
  19.              System.out.println(ping); 
  20.             
  21.              //首先,我们看下redis服务里是否有数据 
  22.              long dbSizeStart = redisService.dbSize(); 
  23.              System.out.println(dbSizeStart); 
  24.             
  25.              redisService.set("username", "oyhk");//设值(查看了源代码,默认存活时间30分钟) 
  26.              String username = redisService.get("username");//取值 
  27.              System.out.println(username); 
  28.              redisService.set("username1", "oyhk1", 1);//设值,并且设置数据的存活时间(这里以秒为单位) 
  29.              String username1 = redisService.get("username1"); 
  30.              System.out.println(username1); 
  31.              Thread.sleep(2000);//我睡眠一会,再去取,这个时间超过了,他的存活时间 
  32.              String liveUsername1 = redisService.get("username1"); 
  33.              System.out.println(liveUsername1);//输出null 
  34.             
  35.              //是否存在 
  36.              boolean exist = redisService.exists("username"); 
  37.              System.out.println(exist); 
  38.             
  39.              //查看keys 
  40.              Set<String> keys = redisService.keys("*");//这里查看所有的keys 
  41.              System.out.println(keys);//只有username username1(已经清空了) 
  42.             
  43.              //删除 
  44.              redisService.set("username2", "oyhk2"); 
  45.              String username2 = redisService.get("username2"); 
  46.              System.out.println(username2); 
  47.              redisService.del("username2"); 
  48.              String username2_2 = redisService.get("username2"); 
  49.              System.out.println(username2_2);//如果为null,那么就是删除数据了 
  50.             
  51.              //dbsize 
  52.              long dbSizeEnd = redisService.dbSize(); 
  53.              System.out.println(dbSizeEnd); 
  54.             
  55.              //清空reids所有数据 
  56.              //redisService.flushDB(); 
  57.        } 


package com.mkfree.redis.test;

import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* redis spring 简单例子
* @author hk
*
* 2012-12-22 上午10:40:15
*/
public class TestRedis {

       public static void main(String[] args) throws InterruptedException {
             ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
             //这里已经配置好,属于一个redis的服务接口
             RedisService redisService = (RedisService) app.getBean("redisService");

             String ping = redisService.ping();//测试是否连接成功,连接成功输出PONG
             System.out.println(ping);

             //首先,我们看下redis服务里是否有数据
             long dbSizeStart = redisService.dbSize();
             System.out.println(dbSizeStart);

             redisService.set("username", "oyhk");//设值(查看了源代码,默认存活时间30分钟)
             String username = redisService.get("username");//取值
             System.out.println(username);
             redisService.set("username1", "oyhk1", 1);//设值,并且设置数据的存活时间(这里以秒为单位)
             String username1 = redisService.get("username1");
             System.out.println(username1);
             Thread.sleep(2000);//我睡眠一会,再去取,这个时间超过了,他的存活时间
             String liveUsername1 = redisService.get("username1");
             System.out.println(liveUsername1);//输出null

             //是否存在
             boolean exist = redisService.exists("username");
             System.out.println(exist);

             //查看keys
             Set<String> keys = redisService.keys("*");//这里查看所有的keys
             System.out.println(keys);//只有username username1(已经清空了)

             //删除
             redisService.set("username2", "oyhk2");
             String username2 = redisService.get("username2");
             System.out.println(username2);
             redisService.del("username2");
             String username2_2 = redisService.get("username2");
             System.out.println(username2_2);//如果为null,那么就是删除数据了

             //dbsize
             long dbSizeEnd = redisService.dbSize();
             System.out.println(dbSizeEnd);

             //清空reids所有数据
             //redisService.flushDB();
       }
}

    好了入门例子就先这么多了...如果有什么疑问,可以加我QQ 1109349806 联系我,交流学习方法

项目源代码下载: http://blog.mkfree.com/posts/12




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论