1, using Spring-data-redis integration
Pom.xml used by the 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>com.x.redis</groupId> <artifactid>spring_redis</artifactid > <version>1.0-snapshot</version> <packaging>jar</packaging> <name>Spring_redis</name> <url >http://maven.apache.org</url><properties> <project.build.sourceencoding>utf-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <group Id>org.springframework.data</groupid> <artifactId>spring-data-redis</artifactId> &L T;version>1.0.2. release</version> </dependency> <dependency> <groupid>org.springframework</g Roupid> <artifactId>spring-core</artifactId> <version>3.1.2. release</version> </dependency> <dependency> <groupid>redis.clients</g Roupid> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <!--convert an existing Jakarta commons logging call to a lsf4j call. -<dependency> <groupId>org.slf4j</groupId> <artifactid >jcl-over-slf4j</artifactId> <version>1.6.1</version> </dependency> <!--Hack: Make sure the commons-logging jar package is not introduced, otherwise it will conflict with JCL-OVER-SLF4J > <dependency> <groupId>commons-logging</groupId> <artifa Ctid>commons-logging</artifactid> <version>1.1.1</version> <scope>provided</scope> </dependency> <!--slf4 Implementation of J: Logback, used to replace log4j. Faster, Stronger! -<dependency> <groupId>ch.qos.logback</groupId> <artif Actid>logback-classic</artifactid> <version>0.9. -</version> <scope>runtime</scope> </dependency> </DEPENDENCIES>&L T;/project>
Redis.properties:
# Redis settings#redis.host =192.168 . 20.101 #redis. Port =6380 #redis. Pass =foobaredredis.host =127.0 . 0.1 redis.port =6379 redis.pass = Redis.maxidle =300 redis.maxactive =600 redis.maxwait =1000 redis.testonborrow =true
Applicationcontext.xml:
<?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:jee="Http://www.springframework.org/schema/jee"xmlns:tx="Http://www.springframework.org/schema/tx"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder location="classpath:redis.properties"/> <context:component-scanBase-package="Com.x.redis.dao"> </context:component-scan> <bean id="Poolconfig" class="Redis.clients.jedis.JedisPoolConfig"> <property name="Maxidle"Value="${redis.maxidle}"/> <property name="maxactive"Value="${redis.maxactive}"/> <property name="maxwait"Value="${redis.maxwait}"/> <property name="Testonborrow"Value="${redis.testonborrow}"/> </bean> <bean id="ConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"P:host-name="${redis.host}"p:port="${redis.port}"p:password="${redis.pass}"p:pool-config-ref="Poolconfig"/> <bean id="redistemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="ConnectionFactory" ref="ConnectionFactory"/> </bean> <bean id="Userdao" class="Com.x.redis.dao.impl.userdaoimpl"/> </beans>
User entity class:
Public classUser {Private LongID; PrivateString name; Public LonggetId () {returnID; } Public voidSetId (LongID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; }}
Userdaoimpl:
1,spring's encapsulation of the DAO layer uses a template approach similar to the following code.
2,redistemplate is one of spring's encapsulation of Redis.
Public classUserdaoimpl implements Userdao {@AutowiredprotectedRedistemplate<serializable, serializable>redistemplate; Public voidSaveuser (final User user) {Redistemplate.execute (NewRediscallback<object>() {@Override PublicObject Doinredis (redisconnection connection) throws DataAccessException {connection.Set(Redistemplate.getstringserializer (). Serialize ("User.uid."+User.getid ()), Redistemplate.getstringserializer (). Serialize (User.getname ())); return NULL; } }); } @Override PublicUser GetUser (finalLongID) {returnRedistemplate.execute (NewRediscallback<user>() {@Override PublicUser Doinredis (redisconnection connection) throws DataAccessException {byte[] key = Redistemplate.getstringserializer (). Serialize ("User.uid."+ID); if(Connection.exists (key)) {byte[] value = connection.Get(key); String name=Redistemplate.getstringserializer (). Deserialize (value); User User=NewUser (); User.setname (name); User.setid (ID); returnuser; } return NULL; } }); }}
Test code:
Public Static voidMain (string[] args) {ApplicationContext ac=NewClasspathxmlapplicationcontext ("Classpath:/applicationcontext.xml"); Userdao Userdao= (Userdao) Ac.getbean ("Userdao"); User User1=NewUser (); User1.setid (1); User1.setname ("Obama"); Userdao.saveuser (user1); User User2= Userdao.getuser (1); System. out. println (User2.getname ()); }
Detailed reference: http://www.cnblogs.com/tankaixiong/p/3660075.html
Spring Integrated Redis