標籤:getbean val pool 國家 9.png xmla 匯入 到期 東京
官網: 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:
Spring 整合Redis