Spring 集成Redis

官网: http://projects.spring.io/spring-data-redis/

第一步:pom.xml中 导入依赖包

 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/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4
 5     <groupId>com.wisezone</groupId>
 6     <artifactId>spring-redis-demo</artifactId>
 7     <version>0.0.1-SNAPSHOT</version>
 8     <packaging>jar</packaging>
 9
10     <name>spring-redis-demo</name>
11     <url>http://maven.apache.org</url>
12
13     <properties>
14         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15     </properties>
16
17     <dependencies>
18         <dependency>
19             <groupId>org.springframework.data</groupId>
20             <artifactId>spring-data-redis</artifactId>
21             <version>1.7.4.RELEASE</version>
22         </dependency>
23
24         <dependency>
25             <groupId>redis.clients</groupId>
26             <artifactId>jedis</artifactId>
27             <version>2.9.0</version>
28         </dependency>
29
30         <dependency>
31             <groupId>junit</groupId>
32             <artifactId>junit</artifactId>
33             <version>4.12</version>
34             <scope>test</scope>
35         </dependency>
36
37     </dependencies>
38 </project>

第二步: 配置 Jedis, 在application.xml 里面

(注意:这里面需要注意配置redis序列化问题,不然对应的在redis客户端就是乱码是的问题)

 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     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5
 6     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
 7         <property name="maxTotal" value="1024" />
 8         <property name="maxIdle" value="200" />
 9         <property name="maxWaitMillis" value="10000" />
10         <property name="testOnBorrow" value="true" />
11     </bean>
12
13     <bean id="jedisConnFactory"
14         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
15         <property name="hostName" value="127.0.0.1" />
16         <property name="port" value="6379" />
17         <property name="password" value="123456" />
18         <property name="poolConfig" ref="jedisPoolConfig" />
19
20     </bean>
21
22     <!--配置redis序列化,解决redis乱码的问题-->
23     <!-- redis template definition -->
24     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
25         p:connection-factory-ref="jedisConnFactory" >
26         <!-- key的序列化 -->
27         <property name="keySerializer">
28             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
29         </property>
30
31          <!-- value的序列化 -->
32         <property name="valueSerializer">
33             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
34         </property>
35
36          <!-- hashKey的序列化 -->
37          <property name="hashKeySerializer">
38              <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
39          </property>
40
41          <!-- hashValue的序列化 -->
42          <property name="hashValueSerializer">
43              <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
44          </property>
45
46     </bean>
47 </beans>

第三步: 编写代码测试

 1 package com.wisezone.spring_redis_demo;
 2
 3 import org.springframework.context.support.AbstractApplicationContext;
 4 import org.springframework.context.support.FileSystemXmlApplicationContext;
 5 import org.springframework.data.redis.core.BoundHashOperations;
 6 import org.springframework.data.redis.core.BoundValueOperations;
 7 import org.springframework.data.redis.core.RedisTemplate;
 8
 9
10 public class Application {
11
12     public static void main(String[] args) {
13
14         AbstractApplicationContext aac = new
15                 FileSystemXmlApplicationContext("classpath:application.xml");
16         RedisTemplate<String, String> redisTemplate = aac.getBean(RedisTemplate.class);
17
18         //1、value做法
19         /*BoundValueOperations<String, String> valueOperations = redisTemplate.boundValueOps("上海天气");
20         valueOperations.set("今天高温,不适合出行");
21         //valueOperations.expire(1, TimeUnit.SECONDS);//设置1秒自动消失(设置过期)
22         System.out.println(valueOperations.get());*/
23
24
25         //2、hash做法
26         BoundHashOperations<String, Object, Object> hashOperations = redisTemplate.boundHashOps("各国家首都");
27         hashOperations.put("中国", "北京");
28         hashOperations.put("韩国", "首尔");
29         hashOperations.put("日本", "东京");
30         hashOperations.put("英国", "伦敦");
31         hashOperations.put("法国", "巴黎");
32         hashOperations.put("美国", "华盛顿");
33
34         System.out.println(hashOperations.get("中国"));
35         System.out.println(hashOperations.get("韩国"));
36         System.out.println(hashOperations.get("日本"));
37         System.out.println(hashOperations.get("英国"));
38         System.out.println(hashOperations.get("法国"));
39         System.out.println(hashOperations.get("美国"));
40     }
41 }

Console:

时间: 2024-10-04 12:28:15

Spring 集成Redis的相关文章

Spring 集成Redis哨兵模式

Spring 集成Redis哨兵模式 1.pom文件添加以下jar <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <!-- redis客户端jar -->

Spring集成redis集群

Spring集成redis集群 有密码 Maven <jedis.version>2.9.0</jedis.version> <spring-data-redis.version>1.7.1.RELEASE</spring-data-redis.version> <spring.version>3.2.17.RELEASE</spring.version> <dependency> <groupId>org.s

spring 集成redis客户端jedis(java)

jedis是redis的java客户端,spring将redis连接池作为一个bean配置. "redis.clients.jedis.JedisPool",这是单机环境适用的redis连接池. 1.maven导入相关包: <!-- redis依赖包 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> &l

Spring集成Redis使用注解

转载:http://blog.csdn.net/u013725455/article/details/52129283 使用Maven项目,添加jar文件依赖: 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.apac

升级spring&amp;集成Redis 二: spring4集成redis

1 添加Redis依赖 引入需要集成的redis版本 <!--spring redis 2.8.2 start--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.4.2</version> </dependency> <dependenc

spring集成redis cluster

客户端采用最新的jedis 2.7 1. maven依赖: <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency> 2. 增加spring 配置 <bean name="genericObjectPoolConfig&qu

spring集成redis

spring版本:4.1.6.RELEASE redis客户端:2.4.2 redis服务端:2.6.12 spring配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins

Spring 集成 Redis

pom.xml <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.0.4.RELEASE</version> <exclusions> <exclusion> <groupId>org.slf4j</groupI

Spring Cache集成redis

Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set –有序集合)和hash(哈希类型).这些数据类型都支持push/pop.add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的.在此基础上,redis支持各种不同方式的排序.与memcached一样,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据