spring data Redis 集成jedis操作 redis中的数据。

13 篇文章 0 订阅

这篇文章主要说spring data Redis 集成jedis操作 redis中的数据。

还有一个和spring mvc 的小整合。实现了对用户的添加查询操作,后期有需要大家可以自己扩展。

代码目录结构如下


下面列出几个主要的文件吧,最后面有整个工程的下载

pom.xml

[html]  view plain  copy
  1. <project xmlns="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/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>cn.lzg</groupId>  
  5.     <artifactId>sdrdemo</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>sdrdemo Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.     <properties>  
  11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  12.         <org.springframework.version>4.1.6.RELEASE</org.springframework.version>  
  13.         <spring.version>4.1.6.RELEASE</spring.version>  
  14.     </properties>  
  15.     <dependencies>  
  16.         <!-- config junit jar -->  
  17.         <dependency>  
  18.             <groupId>junit</groupId>  
  19.             <artifactId>junit</artifactId>  
  20.             <version>4.11</version>  
  21.             <scope>test</scope>  
  22.         </dependency>  
  23.         <!-- config redis data and client jar -->  
  24.         <dependency>  
  25.             <groupId>org.springframework.data</groupId>  
  26.             <artifactId>spring-data-redis</artifactId>  
  27.             <version>1.5.0.RELEASE</version>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>org.apache.commons</groupId>  
  31.             <artifactId>commons-pool2</artifactId>  
  32.             <version>2.2</version>  
  33.         </dependency>  
  34.         <dependency>  
  35.             <groupId>redis.clients</groupId>  
  36.             <artifactId>jedis</artifactId>  
  37.             <version>2.6.2</version>  
  38.         </dependency>  
  39.   
  40.         <!-- config need jar -->  
  41.         <dependency>  
  42.             <groupId>commons-lang</groupId>  
  43.             <artifactId>commons-lang</artifactId>  
  44.             <version>2.6</version>  
  45.         </dependency>  
  46.         <dependency>  
  47.             <groupId>org.apache.geronimo.specs</groupId>  
  48.             <artifactId>geronimo-servlet_3.0_spec</artifactId>  
  49.             <version>1.0</version>  
  50.         </dependency>  
  51.   
  52.         <!-- cofig spring jar -->  
  53.         <dependency>  
  54.             <groupId>org.springframework</groupId>  
  55.             <artifactId>spring-core</artifactId>  
  56.             <version>${org.springframework.version}</version>  
  57.         </dependency>  
  58.         <dependency>  
  59.             <groupId>org.springframework</groupId>  
  60.             <artifactId>spring-expression</artifactId>  
  61.             <version>${org.springframework.version}</version>  
  62.         </dependency>  
  63.         <dependency>  
  64.             <groupId>org.springframework</groupId>  
  65.             <artifactId>spring-beans</artifactId>  
  66.             <version>${org.springframework.version}</version>  
  67.         </dependency>  
  68.         <dependency>  
  69.             <groupId>org.springframework</groupId>  
  70.             <artifactId>spring-aop</artifactId>  
  71.             <version>${org.springframework.version}</version>  
  72.         </dependency>  
  73.         <dependency>  
  74.             <groupId>org.springframework</groupId>  
  75.             <artifactId>spring-context</artifactId>  
  76.             <version>${org.springframework.version}</version>  
  77.         </dependency>  
  78.         <dependency>  
  79.             <groupId>org.springframework</groupId>  
  80.             <artifactId>spring-context-support</artifactId>  
  81.             <version>${org.springframework.version}</version>  
  82.         </dependency>  
  83.         <dependency>  
  84.             <groupId>org.springframework</groupId>  
  85.             <artifactId>spring-tx</artifactId>  
  86.             <version>${org.springframework.version}</version>  
  87.         </dependency>  
  88.         <dependency>  
  89.             <groupId>org.springframework</groupId>  
  90.             <artifactId>spring-jdbc</artifactId>  
  91.             <version>${org.springframework.version}</version>  
  92.         </dependency>  
  93.         <dependency>  
  94.             <groupId>org.springframework</groupId>  
  95.             <artifactId>spring-orm</artifactId>  
  96.             <version>${org.springframework.version}</version>  
  97.         </dependency>  
  98.         <dependency>  
  99.             <groupId>org.springframework</groupId>  
  100.             <artifactId>spring-oxm</artifactId>  
  101.             <version>${org.springframework.version}</version>  
  102.         </dependency>  
  103.         <dependency>  
  104.             <groupId>org.springframework</groupId>  
  105.             <artifactId>spring-web</artifactId>  
  106.             <version>${org.springframework.version}</version>  
  107.         </dependency>  
  108.         <dependency>  
  109.             <groupId>org.springframework</groupId>  
  110.             <artifactId>spring-webmvc</artifactId>  
  111.             <version>${org.springframework.version}</version>  
  112.         </dependency>  
  113.         <dependency>  
  114.             <groupId>org.springframework</groupId>  
  115.             <artifactId>spring-webmvc-portlet</artifactId>  
  116.             <version>${org.springframework.version}</version>  
  117.         </dependency>  
  118.         <dependency>  
  119.             <groupId>org.springframework</groupId>  
  120.             <artifactId>spring-test</artifactId>  
  121.             <version>${org.springframework.version}</version>  
  122.             <scope>test</scope>  
  123.         </dependency>  
  124.     </dependencies>  
  125.     <build>  
  126.         <finalName>sdrdemo</finalName>  
  127.         <plugins>  
  128.             <plugin>  
  129.                 <artifactId>maven-war-plugin</artifactId>  
  130.             </plugin>  
  131.             <plugin>  
  132.                 <artifactId>maven-compiler-plugin</artifactId>  
  133.                 <configuration>  
  134.                     <source>1.8</source>  
  135.                     <target>1.8</target>  
  136.                 </configuration>  
  137.             </plugin>  
  138.         </plugins>  
  139.     </build>  
  140. </project>  

spring-context.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:util="http://www.springframework.org/schema/util"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  7.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  8.             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd                
  9.             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
  10.       
  11.     <!-- 激活@Controller模式 -->  
  12.     <mvc:annotation-driven />  
  13.       
  14.     <!-- 启动注解 -->  
  15.     <context:annotation-config />    
  16.       
  17.     <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->  
  18.     <context:component-scan base-package="cn.lzg" />  
  19.       
  20.     <!-- 引入redis属性配置文件 -->  
  21.     <import resource="redis-context.xml"/>  
  22.   
  23. </beans>  

redis-context.xml

[html]  view plain  copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  3.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="  
  5.             http://www.springframework.org/schema/beans   
  6.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.             http://www.springframework.org/schema/tx   
  8.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  9.             http://www.springframework.org/schema/context  
  10.             http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  11.                ">  
  12.   
  13.     <!-- 扫描 redis properties -->  
  14.     <context:property-placeholder location="classpath:property/redis.properties" />  
  15.   
  16.  <!--  <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
  17.       p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:use-pool="true"/>  
  18.   redis template definition  
  19.   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/> -->  
  20.       
  21.     <bean id="jedisConnectionFactory"  
  22.         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
  23.         p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"  
  24.         p:use-pool="true" />  
  25.   
  26.     <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
  27.         <property name="connectionFactory" ref="jedisConnectionFactory" />  
  28.         <!-- 如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can't cast to String!!! -->  
  29. <!--         <property name="keySerializer">  
  30.             <bean  
  31.                 class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
  32.         </property>  
  33.         <property name="valueSerializer">  
  34.             <bean  
  35.                 class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
  36.         </property> -->  
  37.     </bean>  
  38.   
  39. </beans>  

JedisApiTest.java 这个类主要包含了对redis 几种数据类型的常见API操作(String,List,Set,SortSet,Hash)

[java]  view plain  copy
  1. package cn.lzg.sdrdemo.service.impl;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.annotation.Resource;  
  7.   
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.data.redis.core.HashOperations;  
  10. import org.springframework.data.redis.core.ListOperations;  
  11. import org.springframework.data.redis.core.RedisTemplate;  
  12. import org.springframework.data.redis.core.SetOperations;  
  13. import org.springframework.data.redis.core.StringRedisTemplate;  
  14. import org.springframework.data.redis.core.ValueOperations;  
  15. import org.springframework.data.redis.core.ZSetOperations;  
  16. import org.springframework.stereotype.Component;  
  17.   
  18. @Component  
  19. public class JedisApiTest {  
  20.     //这里别写泛型,因为StringRedisTemplate的源码是默 extends RedisTemplate<String, User> 你写泛型了就无法正确按照类型匹配了  
  21.     @Autowired  
  22.     private RedisTemplate stringRedisTemplate;    
  23.     //这里为什么能这样注入,是利用了spring 的Editor特性,具体见下面博客  
  24.     //http://www.cnblogs.com/chanedi/p/4135303.html  
  25.     //当然spring data redis 的官网也推荐了这个用法,注意这个特性是在 spring 3.2.8以上才有的,以后写demo要注意官方文档的requrement的提示  
  26.     //http://docs.spring.io/spring-data/redis/docs/1.4.0.RELEASE/reference/html/#redis:template  
  27.     @Resource(name = "stringRedisTemplate")  
  28.     private ListOperations<String, String> listOps;  
  29.   
  30.     @Resource(name = "stringRedisTemplate")  
  31.     private ValueOperations<String, String> valueOps;  
  32.   
  33.     @Resource(name = "stringRedisTemplate")  
  34.     private HashOperations<String, String, Object> hashOps;  
  35.   
  36.     @Resource(name = "stringRedisTemplate")  
  37.     private SetOperations<String, String> setOps;  
  38.       
  39.     @Resource(name = "stringRedisTemplate")  
  40.     private ZSetOperations<String, String> ZSetOps;  
  41.   
  42.     //redis命令速查表 http://doc.redisfans.com/  
  43.       
  44.     //List的操作  
  45.     public void testListOps(){  
  46.         //List 其实是一个双向链表,从下面的代码就能看出来可以从两边进行push/pop操作  
  47.         stringRedisTemplate.delete("listTest"); //删除这个List  
  48.         //添加操作  
  49.         listOps.rightPush("listTest""1");  
  50.         listOps.rightPush("listTest""2");  
  51.         listOps.leftPush("listTest""3");  
  52.         listOps.leftPush("listTest""4");  
  53.           
  54.         // 查看listTest这个List的值  
  55.         System.out.println(listOps.range("listTest"0, -1));   //[4, 3, 1, 2]  
  56.           
  57.         // 取左边第一个值  
  58.         System.out.println(listOps.leftPop("listTest"));    //4  
  59.           
  60.         // 查看listTest这个List的值  
  61.         System.out.println(listOps.range("listTest"0, -1));   //[3, 1, 2]  
  62.           
  63.         //修改操作  将下标为1的值修改为 "set_1",(下标从0开始)  
  64.         listOps.set("listTest"1"set_1");  
  65.         System.out.println(listOps.range("listTest"0, -1));   //[3, set_1, 2]  
  66.           
  67.         //删除操作  将下标为1,值为 set_1的元素删除  
  68.         listOps.remove("listTest"1"set_1");   
  69.         System.out.println(listOps.range("listTest"0, -1));   //[3, 2]  
  70.     }  
  71.       
  72.     //set的操作  
  73.     public void testSetOps(){  
  74.         //添加  
  75.         setOps.add("setTest1""1","2","3","4");  
  76.         setOps.add("setTest2""a","b","1","2");  
  77.           
  78.         //查询 set集合是无序的  
  79.         System.out.println(setOps.members("setTest1"));//[2, 1, 3, 4]  
  80.           
  81.         //判断是否有这个值  
  82.         System.out.println(setOps.isMember("setTest1""4"));// true  
  83.           
  84.         //删除  可以删除多个值  
  85.         setOps.remove("setTest1""1","4");  
  86.         System.out.println(setOps.members("setTest1"));//[3, 2]  
  87.           
  88.         //交集  
  89.         System.out.println(setOps.intersect("setTest1""setTest2"));//[2]  
  90.           
  91.         //并集  
  92.         System.out.println(setOps.union("setTest1""setTest2"));//[d, 2, c, 1, a, 3, b]  
  93.           
  94.         //差集  
  95.         System.out.println(setOps.difference("setTest1""setTest2"));//[3]  
  96.     }  
  97.   
  98.     // ZSetOps操作  
  99.     public void testZSetOps(){  
  100.         //添加  
  101.         ZSetOps.add("ZSetTest""google.com"10);  
  102.         ZSetOps.add("ZSetTest""baidu.com"8);  
  103.         ZSetOps.add("ZSetTest""soso.com"5);  
  104.         ZSetOps.add("ZSetTest""360.com"2);  
  105.           
  106.         //默认按照分数的降序排列  
  107.         System.out.println(ZSetOps.range("ZSetTest"0, -1));  
  108.         //[360.com, soso.com, baidu.com, google.com]  
  109.           
  110.         //按照分数范围查询  
  111.         System.out.println(ZSetOps.rangeByScore("ZSetTest"510));  
  112.         //[soso.com, baidu.com, google.com]  
  113.           
  114.         //查看排名  
  115.         System.out.println(ZSetOps.rank("ZSetTest""baidu.com"));// 2  说明第一名是0  
  116.           
  117.         // 按分数从大到小排列  
  118.         System.out.println(ZSetOps.reverseRange("ZSetTest"0, -1));  
  119.         //[google.com, baidu.com, soso.com, 360.com]  
  120.           
  121.         //看这个对象的 socre  
  122.         System.out.println(ZSetOps.score("ZSetTest""google.com")); //10.0  
  123.           
  124.         // 删除,按照对象  
  125.         ZSetOps.remove("ZSetTest""360.com");  
  126.         // 删除,根据下标  
  127.         ZSetOps.removeRange("ZSetTest"01);  
  128.         // 删除,根据score  
  129.         ZSetOps.removeRangeByScore("ZSetTest"25);  
  130.     }  
  131.       
  132.     // hash表操作 和Map差不多  
  133.     public void testHashOps(){  
  134.         // 添加  
  135.         hashOps.put("hashTest""key1","aa");  
  136.         Map<String,String> map = new HashMap<String, String>();  
  137.         map.put("key2""bb");  
  138.         map.put("key3""cc");  
  139.         map.put("key4""dd");  
  140.         hashOps.putAll("hashTest", map);  
  141.           
  142.         // 查询所有的key  
  143.         System.out.println(hashOps.keys("hashTest")); //[key3, key4, key2, key1]  
  144.           
  145.         //查询 所有的value  
  146.         System.out.println(hashOps.values("hashTest"));//[aa, bb, cc, dd]  
  147.           
  148.         //删除 key  
  149.         hashOps.delete("hashTest""key1");  
  150.         System.out.println(hashOps.keys("hashTest")); //[key2, key4, key3]  
  151.     }  
  152.       
  153.     // String操作  
  154.     public void testValueOps(){  
  155.         valueOps.set("valueTest""1");  
  156.         System.out.println(valueOps.get("valueTest"));  
  157.     }  
  158. }  

上面代码直接跑junit的那个测试类就OK了。

集成spring mvc 的简单操作,大家可以下载工程后,直接部署去看下。

下载地址:http://download.csdn.NET/detail/hunanlzg/9353113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值