Official website: http://projects.spring.io/spring-data-redis/First step: Import dependent packages in Pom.xml
1<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"2xsi: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 Ten<name>spring-redis-demo</name> One<url>http://maven.apache.org</url> A -<properties> -<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> the</properties> - -<dependencies> -<dependency> +<groupId>org.springframework.data</groupId> -<artifactId>spring-data-redis</artifactId> +<version>1.7.4.RELEASE</version> A</dependency> at -<dependency> -<groupId>redis.clients</groupId> -<artifactId>jedis</artifactId> -<version>2.9.0</version> -</dependency> in -<dependency> to<groupId>junit</groupId> +<artifactId>junit</artifactId> -<version>4.12</version> the<scope>test</scope> *</dependency> $ Panax Notoginseng</dependencies> -</project>
Step Two: Configure Jedis, inside the Application.xml(Note: There is a need to pay attention to configuring the Redis serialization problem, otherwise the corresponding problem is garbled in the Redis client)
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"4xsi: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= "1024x768"/>8<property name= "Maxidle" value= "/>"9<property name= "Maxwaitmillis" value= "10000"/>Ten<property name= "Testonborrow" value= "true"/> One</bean> A -<bean id= "Jedisconnfactory" - class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > the<property name= "HostName" value= "127.0.0.1"/> -<property name= "Port" value= "6379"/> -<property name= "Password" value= "123456"/> -<property name= "Poolconfig" ref= "Jedispoolconfig"/> + -</bean> + A<!--configuring Redis serialization to troubleshoot Redis garbled problems-- at<!--redis template definition-- -<bean id= "Redistemplate"class= "Org.springframework.data.redis.core.RedisTemplate" -p:connection-factory-ref= "Jedisconnfactory" > -<!--key Serialization-- -<property name= "Keyserializer" > -<beanclass= "Org.springframework.data.redis.serializer.StringRedisSerializer"/> in</property> - to<!--serialization of value-- +<property name= "ValueSerializer" > -<beanclass= "Org.springframework.data.redis.serializer.StringRedisSerializer"/> the</property> * $<!--HashKey Serialization--Panax Notoginseng<property name= "Hashkeyserializer" > -<beanclass= "Org.springframework.data.redis.serializer.StringRedisSerializer"/> the</property> + A<!--HashValue Serialization-- the<property name= "Hashvalueserializer" > +<beanclass= "Org.springframework.data.redis.serializer.StringRedisSerializer"/> -</property> $ $</bean> -</beans>
Step three: Write code tests
1 PackageCom.wisezone.spring_redis_demo;2 3 ImportOrg.springframework.context.support.AbstractApplicationContext;4 ImportOrg.springframework.context.support.FileSystemXmlApplicationContext;5 Importorg.springframework.data.redis.core.BoundHashOperations;6 Importorg.springframework.data.redis.core.BoundValueOperations;7 Importorg.springframework.data.redis.core.RedisTemplate;8 9 Ten Public classApplication { One A Public Static voidMain (string[] args) { - -Abstractapplicationcontext AAC =New theFilesystemxmlapplicationcontext ("Classpath:application.xml"); -redistemplate<string, string> redistemplate = Aac.getbean (redistemplate.class); - - //1. Value Practice + /*boundvalueoperations<string, string> valueoperations = redistemplate.boundvalueops ("Shanghai Weather"); - Valueoperations.set ("High temperature today, not suitable for travel"); + //valueoperations.expire (1, timeunit.seconds);//Set 1 seconds to automatically disappear (set expiration) A System.out.println (Valueoperations.get ());*/ at - - //2. Hash procedure -Boundhashoperations<string, Object, object> hashoperations = Redistemplate.boundhashops ("National capitals"); -Hashoperations.put ("China", "Beijing"); -Hashoperations.put ("Korea", "Seoul"); inHashoperations.put ("Japan", "Tokyo"); -Hashoperations.put ("Great Britain", "London"); toHashoperations.put ("France", "Paris"); +Hashoperations.put ("USA", "Washington"); - theSystem.out.println (Hashoperations.get ("China"))); *System.out.println (Hashoperations.get ("Korea")); $System.out.println (Hashoperations.get ("Japan")));Panax NotoginsengSystem.out.println (Hashoperations.get ("Great Britain")); -System.out.println (Hashoperations.get ("France"))); theSystem.out.println (Hashoperations.get ("United States")); + } A}
Console:
Spring Integrated Redis