Spring-data-redis: http://www.springsource.org/download/community? Project = spring % 2520 data % 2520 redis & version = 1.0.1.release
// Spring-redis.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: AOP = "http://www.springframework.org/schema/aop" xmlns: cache = "http://www.springframework.org/schema/cache" xmlns: context = "http://www.springframework.org/schema/context" xmlns: MVC = "http://www.springframework.org/schema/mvc" xmlns: oxm = "http://www.springframework.org/schema/oxm" xmlns: P = "HT TP: // www.springframework.org/schema/p "xmlns: util =" http://www.springframework.org/schema/util "xsi: schemalocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/s Chema/Context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd "> <context: annotation-config/> <context: component-scan base-package =" com. abin. lee. spring. redis "> </Context: component-scan> <context: Property-placeholder loca Tion = "classpath: COM/Abin/LEE/spring/redis. properties"/> <! -- Object pool configuration: --> <beanid = "jedispoolconfig" class = "redis. clients. jedis. jedispoolconfig "> <propertyname =" maxactive "value =" $ {redis. pool. maxactive} "/> <propertyname =" maxidle "value =" $ {redis. pool. maxidle} "/> <propertyname =" maxwait "value =" $ {redis. pool. maxwait} "/> <propertyname =" testonborrow "value =" $ {redis. pool. testonborrow} "/> </bean> <! -- Factory implementation: --> <beanid = "jedisconnectionfactory" class = "org. springframework. data. redis. connection. jedis. jedisconnectionfactory "> <propertyname =" hostname "value =" $ {redis. IP} "/> <propertyname =" Port "value =" $ {redis. port} "/> <propertyname =" poolconfig "ref =" jedispoolconfig "/> </bean> <! -- Template class: --> <beanclass = "org. springframework. Data. redis. Core. redistemplate" P: Connection-factory-ref = "jedisconnectionfactory"/> </beans>
// User. javapackage COM. abin. lee. spring. redis. pojo; import Java. io. serializable; public class user implements serializable {/***/Private Static final long serialversionuid = 2668307865623183776l; private string uid; private string address; public user () {super ();} public user (string uid, string address) {super (); this. uid = uid; this. address = address;} Public String getuid () {return uid;} public void setui D (string UID) {This. uid = uid;} Public String getaddress () {return address;} public void setaddress (string address) {This. address = address ;}@ overridepublic string tostring () {return "User [uid =" + uid + ", address =" + address + "]";} @ overridepublic int hashcode () {final int prime = 31; int result = 1; Result = prime * result + (address = NULL )? 0: Address. hashcode (); Result = prime * result + (uid = NULL )? 0: uid. hashcode (); return result ;}@ overridepublic Boolean equals (Object OBJ) {If (this = OBJ) return true; If (OBJ = NULL) return false; if (getclass ()! = Obj. getclass () return false; user Other = (User) OBJ; If (address = NULL) {If (other. Address! = NULL) return false;} else if (! Address. Equals (other. Address) return false; If (uid = NULL) {If (other. uid! = NULL) return false;} else if (! UID. Equals (other. UID) return false; return true ;}}
// Userdao. javapackage COM. abin. lee. spring. redis. dao; import COM. abin. lee. spring. redis. pojo. user; public interface userdao {/*** @ Param uid * @ Param address */void save (User user ); /*** @ Param uid * @ return */User Read (string UID);/*** @ Param uid */void Delete (string UID );}
// Userdaoimpl. javapackage COM. abin. lee. spring. 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 COM. abin. lee. spring. redis. dao. userdao; import COM. abin. lee. spring. redis. pojo. user; @ repository ("userdao") public class userdaoimpl implements userdao {@ autowiredprivate redistemplate <serializable, serializable> redistemplate; @ overridepublic void save (final user) repeated redistemplate.exe cute (New rediscallback <Object> () {@ overridepublic object doinredis (redisconnection connection) throws dataaccessexception {connection. set (redistemplate. getstringserializer (). serialize ("user. UID. "+ User. getuid (), redistemplate. getstringserializer (). serialize (user. getaddress (); return NULL ;}}) ;}@ overridepublic User Read (final string UID) {return redistemplate.exe cute (New rediscallback <user> () {@ overridepublic user doinredis (redisconnection connection) throws dataaccessexception {byte [] Key = redistemplate. getstringserializer (). serialize ("user. UID. "+ 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 ;}}) ;}@ overridepublic void Delete (final string UID) implements redistemplate.exe cute (New rediscallback <Object> () {public object doinredis (redisconnection connection) {connection. del (redistemplate. getstringserializer (). serialize ("user. UID. "+ UID); return NULL ;}});}}
// Userdaotest. javapackage COM. abin. lee. spring. redis. dao. test; import static Org. JUnit. assert. assertequals; import static Org. JUnit. assert. assertnull; import Org. JUnit. before; import Org. JUnit. test; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import COM. abin. lee. spring. redis. dao. userdao; import COM. abin. lee. spring. redis. pojo. user; public class userdaotest {private applicationcontext app; private userdao; @ beforepublic void before () throws exception {APP = new classpathxmlapplicationcontext ("com/Abin/LEE/spring/redis/spring-redis.xml"); userdao = (userdao) app. getbean ("userdao") ;}@ testpublic 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); system. out. println ("address1 =" + User. getaddress (); assertequals (address1, user. getaddress (); // -------------- update ------------ string address2 = "Beijing"; user. setaddress (address2); userdao. save (User); User = userdao. read (UID); system. out. println ("address2save =" + User. getaddress (); assertequals (address2, user. getaddress (); // -------------- Delete ------------ userdao. delete (UID); User = userdao. read (UID); system. out. println ("addressdel =" + User. getaddress (); assertnull (User );}}