1、利用spring-data-redis整合
pom.xml:
<Project XMLNS = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apach/4.0. http://maven.apache.org/xsd/maven-4.0.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.x.redis </groupid> <artifactid> spring_redis </artifactid> <バージョン> <name> spring_redis </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding <artifactid> spring-data-redis </artifactid> <バージョン> 1.0.2.Release </version> </dependency> <dependency> <groupid> org.springframework </groupid> <artifactid> spring-core </artifactid> <バージョン> 3.1.2.releas <artifactid> jedis </artifactid> <bersion> 2.1.0 </version> </dependency> <dependency> groupid> junit </groupid> <artifactid> <artifactid> <バージョン> 4.8.2 </version> <scope> test </scope> </dendecency> <edentency> <groupid> <artifactid> slf4j-api </artifactid> <バージョン> 1.6.1 </version> </dependency> <! - 将现有的jakartaコモンズロギング的调用转换成lsf4j </dependency> <! - hack:确保commons-logging SLF4J:logback、用来取代log4j 。更快、更强! - > <依存関係> <groupId> ch.qos.logback </groupid> <artifactid> logback-classic </artifactid> <バージョン> 0.9.24 </version> <scope> runtime </scope> </dendection> </dependencies> </project>
除了log部分、只有一个スプリングコア和スプリングダタレディス了
项目文件目录结构:
ApplicationContext.xml:
1、コンテキスト:プロパティプレイスホルダー标签用来导入プロパティ文件。从而替换$ {redis.maxidle}这样的变量。
2、コンテキスト:Component-scan是为了在com.x.redis.dao报下的类能够实用春的注解注入的方式。
3、事实上我们只需要把jedispoolconfig配数来就好了、接下来就是春的封装了。所以直接看ユーザーdaoimpl的实现就明白了。
<?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. xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xds "場所= "classpath:redis.properties" /> <context:component-scan base-package = "com.x.redis.dao"> < /context:component-scan> <bean id = "poolconfig"> <プロパティ名= "maxidle" value = "$ {redis.maxidle}" <プロパティ名= "maxwait" value = "$ {redis.maxwait}" /> <プロパティ名= "testonborrow" value = "$ {redis.testonborrow}" /> < /bean> <bean id = "connectionFactory" p:host-name = "$ {redis.host}" P:password = "$ {redis.pass}" p:pool-config-ref = "poolconfig"/> <bean id = "redistemplate"> <プロパティ名= "connectionfactory" ref = "connectionfactory"/> </bean> <bean id = "userdao"/>> </beans> Redis.Properties:
#redis settings#redis.host = 192.168.20.101#redis.port = 6380#redis.pass = foobaredRedis.host = 127.0.0.1Redis.port = 6379Redis.pass = redis.maxidle = 300redis.maxactive = 600redis.maxwait
userdaoimpl:
1、春对dao层的封装很多用了类似于下面代码的模板方式。
2、redistemplate
パブリッククラスのuserdaoimplはuserdaoを実装しています{@autowired protected redistemplate <serializable、serializable> redistemplate; public void saveuser(最終ユーザーユーザー){redistemplate.execute(new rediscallback <object>(){@override public object doinredis(redisconnection connection)dataaccessexception {connection.set(redistemplate.getStringerializer()。 redistemplate.getStringserializer()。 } @Override public user getUser(final long id){return redistemplate.execute(new rediscallback <user>(){@Override public user doinredis(redisconnection connection)throws dataaccessexception {byte [] key = redistemplate.getStringerializer()。 byte = connection(key); }}其他:
ユーザー:
パブリッククラスユーザー{private long id;プライベート文字列名; public long getId(){return id; } public void setid(long id){this.id = id; } public string getname(){return name; } public void setName(string name){this.name = name; }}测试代码:
public static void main(string [] args){applicationContext ac = new classpathxmlapplicationContext( "classPath:/applicationContext.xml"); userdao userdao =(userdao)ac.getbean( "userdao"); user user1 = new user(); user1.setId(1); user1.setname( "obama"); userdao.saveuser(user1); user user2 = userdao.getuser(1); System.out.println(user2.getName()); }2、不利用spring-data-redis整合
个人觉得这样整合灵活度更大、能够更加明了的完成任务。
pom.xml:
<Project XMLNS = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apach/4.0. http://maven.apache.org/xsd/maven-4.0.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.d.work </groupid> <artifactid> redis_templete </artifactid> <バージョン> <name> redis_templete </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> </properties> <deprencid </groupid </groupid> <バージョン> 3.8.1 </version> <scope> test </scope> </dependency> <dependency> <expendency> redis.clients </groupid> <artifactid> jedis </artifactid> <バージョン> 2.1.0 </version <バージョン> 3.1.2.Release </version> </dependency> <dependency> <groupid> org.springframework </groupid> <artifactid> spring-beans </artifactid> <バージョン> 3.1.2.Release </version> </dependency> <sependency> <shipifid> </groupid> </</</</</</</</</</</</</</</</</</</</</</</</</groupid> </グループ<バージョン> 3.1.2.Release </version> </dependency> <dependency> <shipid> org.slf4j </groupid> <artifactid> slf4j-api </artifactid> <バージョン> 1.6.1 </バージョン> </依存関係> <! <GroupId> org.slf4j </groupid> <artifactid> jcl-over-slf4j </artifactid> <version> 1.6.1 </version> </dependency> <! - hack:确保jar包不被引入jar包不被引入、否则将和jcl-over-slf4j <artifactid> commons-logging </artifactid> <bersion> 1.1.1 </version> <scope> revide </scope> </dependency> <! - slf4j的实现:log4j 。更快、更强! - > <依存関係> <groupid> ch.qos.logback </groupid> <artifactid> <> <> <pricactid> <pridactid> <> <> <Scope>ランタイム</scope> </dependency> </dependencies> </project>
目录结构:
data-source.xml
1、コンテキスト:プロパティプレイスホルダー和コンテキスト:コンポーネントスキャン前面解释过啦。
2、配置了一个shardedjedispool、在jdeis
一个是分片形式、可以连接有主备的レディス服务端、一个是单个的。详细后续学习
3、因为不使用spring-data-redis的封装、所以自己要自己封装一个
<?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. xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xds "場所= "classpath:redis.properties" /> <context:component-scan base-package = "com.d.work.main"> < /context:component-scan> <context:component-scan base-package = "com.d.work.redis"> < /context: /context:component-scan> <bean id = "jedispoolconfig"> <""> < name = "maxidle" value = "8"/> <プロパティ名= "maxwait" value = "1000"/> <プロパティ名= "testonborrow" value = "true"/> <プロパティname = "testonreturn" value = "true"/> <! - <プロパティ名= "testwhileidle" value = "" true "/> scope = "Singleton"> <constructor-arg index = "0" ref = "jedispoolconfig" /> <constructor-arg index = "1"> <list> <bean> <constructor-arg name = "host" value = "$ {redis.host}" />> <constructor-arg name = "port" port = "octuntt" value = "$ {redis.timeout}"/> <constructor-arg name = "weight" value = "1"/> </bean> </list> </constructor-arg> </bean> </beans>redisdatasource:定义三个方法
パブリックインターフェイスredisdatasource {public abstract shardedjedis getredisclient(); public void returnResource(shardedjedis shardedjedis); public void returnResource(shardedjedis shardedjedis、boolean broken);}实现RedisDataSource:
1、注入配置好的shardedjedispool、这三个方法的作用:
@Repository( "RedisDataSource")Public Class RedisDataSourceImplはRedisDataSource {private static final logger log = loggerfactory.getlogger(redisdatasourceimpl.class); @autowired private shardedjedispool shardedjedispool; public shardedjedis getredisclient(){try {shardedjedis shardjedis = shardedjedispool.getResource(); shardjedisを返します。 } catch(Exception E){log.Error( "GetRedisclent Error"、e); } nullを返します。 } public void returnResource(shardedjedis shardedjedis){shardedjedispool.returnresource(shardedjedis); } public void returnResource(shardedjedis shardedjedis、boolean broken){if(broken){shardedjedispool.returnbrekenresource(shardedjedis); } else {shardedjedispool.returnresource(shardedjedis); }}}第二层的封装:redisclientTemplate、例子实现了放值和取值。最后代码提供了全部命令的实现。
代码就是映射性质的又一次调用ジェダス、用了个壊れた来做标示符、决定返还资源的方式。
这一层的目的主要也是让再上层的调用不需要关心プール中链接的取得和返还问题了。
@Repository( "redisclientTemplate")public class redisclientTemplate {private static final logger log = loggerfactory.getLogger(redisclienttemplate.class); @autowired private redisdatasource redisdatasource; public void disconnect(){shardedjedis shardedjedis = redisdatasource.getredisclient(); shardedjedis.disconnect(); } / ** *设置单个值 * * @param key * @param value * @return * / public string set(string key、string value){string result = null; shardedjedis shardedjedis = redisdatasource.getredisclient(); if(shardedjedis == null){return result; } boolean broken = false; try {result = shardedjedis.set(key、value); } catch(例外e){log.error(e.getmessage()、e);壊れた= true; }最後に{redisdatasource.returnresource(shardedjedis、broken); } return result; } / ** *获取单个值 * * @param key * @return * / public string get(string key){string result = null; shardedjedis shardedjedis = redisdatasource.getredisclient(); if(shardedjedis == null){return result; } boolean broken = false; try {result = shardedjedis.get(key); } catch(例外e){log.error(e.getmessage()、e);壊れた= true; }最後に{redisdatasource.returnresource(shardedjedis、broken); } return result; }}测试代码:
public static void main(string [] args){applicationContext ac = new classpathxmlapplicationContext( "classpath:/data-source.xml"); redisclientTemplate redisclient =(redisclientTemplate)ac.getBean( "redisclientTemplate"); redisclient.set( "a"、 "abc"); system.out.println(redisclient.get( "a")); }附上REDISCLINTTEMPLATE全部实现:
redisclientTemplate代码太多、附上下载地址:http://xiazai.vevb.com/201701/yuanma/redisclienttemplate_jb51.rar
以上就是本文的全部内容、希望对大家的学习有所帮助、也希望大家多多支持武林网。