http://projects.spring.io/spring-data-redis/
Directly on the code Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.xsd "> <context:property-placeholder location= "Classpath:redis.properties"/> <context:component-scan base-package= "Org.zlex.redis.dao"/> <bean id= " Jedisconnectionfactory "class=" Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> < Property Name= "HostName" value= "${redis.ip}"/> <property name= "port" value= "${redis.port}"/> T;property name= "Poolconfig" ref= "Jedispoolconfig"/> </bean>
<bean class= "Org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref= " Jedisconnectionfactory "/> <bean id= jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <p Roperty name= "maxactive" value= "${redis.pool.maxactive}"/> <property name= "Maxidle" value= Ol.maxidle} "/> <property name= maxwait" value= "${redis.pool.maxwait}"/> <property name= "TestOnB Orrow "value=" ${redis.pool.testonborrow} "/> </bean> </beans>
Redis.properties
redis.pool.maxactive=1024
redis.pool.maxidle=200
redis.pool.maxwait=1000
redis.pool.testonborrow= True
#IP
redis.ip=192.168.1.234
#Port
redis.port=6379
Entity classes
/**
* Aug, * *
*
package org.zlex.redis.domain;
Import java.io.Serializable;
/** * *
@author *
@version 1.0 * @since 1.0/Public
class User implements Serializable {
/**
* * *
/private static final long serialversionuid = -1267719235225203410l;
Private String uid;
Private String address;
/**
* @return the UID
*
/Public String Getuid () {return
uid;
}
/**
* @param uid
* The UID to set */public
void SetUid (String uid) {
this.uid = uid;
}
/**
* @return The address
/public String getaddress () {return address
;
}
/**
* @param address
* "To set * /public
void setaddress (String address) {
this.address = address;
}
DAO interface
/**
* Aug, * *
*
package Org.zlex.redis.dao;
Import Org.zlex.redis.domain.User;
/** * *
@author *
@version 1.0 * @since 1.0/Public
interface Userdao {
/**
* Param UID
* @param address
*
/Void Save (user user);
/**
* @param uid
* @return
/User Read (String uid);
/**
* @param uid
*
/void Delete (String uid);
Implementation class
/** * Aug, * * * package org.zlex.redis.dao.impl;
Import java.io.Serializable;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.dao.DataAccessException;
Import org.springframework.data.redis.connection.RedisConnection;
Import Org.springframework.data.redis.core.RedisCallback;
Import Org.springframework.data.redis.core.RedisTemplate;
Import Org.springframework.stereotype.Repository;
Import Org.zlex.redis.dao.UserDao;
Import Org.zlex.redis.domain.User;
/** * * @author * @version 1.0 * @since 1.0/@Repository ("Userdao") public class Userdaoimpl implements Userdao {
@Autowired private redistemplate<serializable, serializable> redistemplate;
@Override public void Save (final user user) {Redistemplate.execute (new rediscallback<object> () {@Override Public Object Doinredis (redisconnection connection) throws DataAccessException {Connection.set (redistemp
Late.getstringserializer (). Serialize ( "User.uid." + User.getuid ()), Redistemplate.getstringserializer (). Serialize (User.getaddress ());
return null;
}
}); }/* * (NON-JAVADOC) * * @see Org.zlex.redis.dao.userdao#read (java.lang.String)/@Override public User Read (Final String UID) {return Redistemplate.execute (new rediscallback<user> () {@Override public User Doinredis (redisconnection C Onnection) throws DataAccessException {byte[] key = Redistemplate.getstringserializer (). Serialize ("user.u
ID. "+ UID);
if (connection.exists (key)) {byte[] value = Connection.get (key);
String address = Redistemplate.getstringserializer (). Deserialize (value);
User user = new user ();
User.setaddress (address);
User.setuid (UID);
return user;
return null;
}
}); }/* * (NON-JAVADOC) * * @see Org.zlex.redis.dao.userdao#delete (java.lang.String)/@Override public void de Lete (Final String uid) {redistempLate.execute (New rediscallback<object> () {public Object Doinredis (redisconnection connection) {CONNECTION.D
El (Redistemplate.getstringserializer (). Serialize ("User.uid." + uid));
return null;
}
});
}
}
TestCase
Package Org.zlex.redis.dao.impl;
Import static org.junit.assert.*;
Import Org.junit.Before;
Import Org.junit.Test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.zlex.redis.dao.UserDao;
Import Org.zlex.redis.domain.User;
public class Userdaotest {private ApplicationContext app;
Private Userdao Userdao;
@Before public void before () throws Exception {app = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Userdao = (Userdao) app.getbean ("Userdao");
@Test public void Crud () {//--------------Create---------------String uid = "u123456";
String Address1 = "Shanghai";
User user = new user ();
User.setaddress (ADDRESS1);
User.setuid (UID);
Userdao.save (user);
---------------Read---------------user = Userdao.read (UID);
Assertequals (Address1, user.getaddress ());
--------------Update------------String address2 = "Beijing"; User.setaddress (addrESS2);
Userdao.save (user);
user = Userdao.read (UID);
Assertequals (Address2, user.getaddress ());
--------------Delete------------userdao.delete (UID);
user = Userdao.read (UID);
Assertnull (user);
}
}
Project Package Download