1,spring-data-redis整合
项目使用的pom.xml:
<Project XMLNS =“ http://maven.apache.org/pom/4.0.0” xmlns:xsi =“ http://www.org/2001/xmlschema-chema-ingschema-ingschema-ingstance” 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.sourceencencoding> </properties> </properties> <dependencies> <dependencies> <dependence> <dependency> <gropsiD> <gropsId> <gropsId> <gropsId> org.springframefringframefringframefringframe.dataa </groupsId> <ArtifactId> spring-data-redis </artifactid> <版本> 1.0.2.release </distrace> </dependency> <dependency> <groudency> <groupId> org.springframework </groupid> <ARTIFACTID> spring-core </artifactid> spring-core </artifactid> </artifactid> <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> <版本> 1.6.1 </version> </dependency> <! - 将现有的jakarta commons记录的调用转换成lsf4j的调用。-> <glateency> <groupentigy> <groupId> org.slf4j </dependenty> <! - hack:hack:确保common-loging的jar包不被引入 slf4j的实现:logback,用来取代log4j。更快、更强! --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>0.9.24</version> <scope>runtime</scope> </dependency> </dependencies></project>
除了log部分部分
项目文件目录结构:
ApplicationContext.xml:
1,上下文:属性 - 持有人标签用来导入属性文件。从而替换$ {redis.maxidle}这样的变量。
2,上下文:组件扫描是为了在com.x.redis.dao报下的类能够实用春季
3,事实上我们只需要把jedispoolconfig配数来就好了,spring的封装了。所以直接看
<?xml版本=“ 1.0”编码=“ utf-8”?> <beans xmlns =“ http://www.springframework.org/schema/beans/beans” xmlns:xsi =“ XMLNS:p =“ http://www.springframework.org/schema/p” xmlns:context =“ http://www.springframework.org/schema/schema/context/context/context” XMLNS:tx =“ http://www.springframework.org/schema/tx” xmlns:aop =“ http://wwwwwwwwwwwwwwwwwwwwwwwwerwork.org/schema/schema/aop/aop” http://www.springframework.org/schema/schema/spring-beans.xsd http://www.springframeworkframework/context.org/schema/schema/context http :///wwwwwwwwwwwwwwwwwwwwwwwwwwwwww.framework.orgramework.orglext-lotter topperta位置=“ class path:redis.properties” /> <上下文:component-scan base-package =“ com.x.redis.dao”> < /context:component-scan> <bean id =“ pool conconfig”> <property name =“ maxidle” valit name =“ maxwait” value =“ $ {redis.maxwait}” /> <属性名称=“ testOnBorrow” value =“ $ {redis.testonBorrow}” /> < /> < /> < /> < /bean> <bean id =“ connectionfactory” p:host-name = host-name =“ $ {redis.host}” P:pool-config-ref =“ poolconfig”/> <bean ID =“ redistemplate”> <property name =“ connectionFactory” ref =“ connectionFactory”/> </> </> </> </bean> <bean ID =“ userdao”/> </beans> </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=1000redis.testOnBorrow=true
USERDAOIMPL:
1,春天对dao层的封装很多用了类似于下面代码的模板方式。
2,重新标志就是春季对redis的一个封装而已。
公共类UserDaoimpl实现了UserDao {@Autowired Protected redistemplate <serializable,serializable> redistemplate; public void saveuser(最终用户){redistemplate.execute(new rediscallback <obsect>(){@Override public obspoct doinredis(redisconnection connection)抛出dataAccessexception {connection.set.set.set.set(redistemplate.getStringStringSerialializer() redistemplate.getStringserializer()。serialize(user.getName()); } @Override公共用户getuser(final long ID){return redistemplate.execute(new rediscallback <user>(){@override public doinredis(redisconnection连接)抛出dataAccessexception {byte [byte [byte [byte = redistemplate = redistemplate.getStringSerialializiziziziziziziziziziziziziziziziziziaLIze.serialize.serialist iferist if inder.if) byte = connection.get(键); redistemplate。 }}}其他:
用户:
公共类用户{私人长ID;私有字符串名称; public long getid(){返回id; } public void setID(长ID){this.id = id; } public String getName(){返回名称; } 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”);用户用户1 = 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.org/2001/xmlschema-chema-ingschema-ingschema-ingstance” http://maven.apache.org/xsd/maven-4.0.0.0.xsd“> <modelversion> 4.0.0 </modelversion> <groupId> com.d.work </groupId> <ARTIFACTID> <ARTIFACTID> redis_tem_temed> redis_tem_temete </artifactid> <NAME> redis_templete </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceecceenc.sourceencecencOccOccOnding> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <版本> 3.1.2.release </version> </deverency> <dependency> <groupId> org.slf4j </groupid> <ArtifactId> slf4j-api </artifactid> </artifactid> <版本> 1.6.1 </verion> </distrion> </dependenty> <! <groupId> org.slf4j </groupId> <trifactid> jcl-over-slf4j </artifactid> <版本> <版本> 1.6.1 </version> </deverency> <!<! - hack:hack:确保commons-logging:确保commons-loging的<tratifactid> common-logging </artifactid> <版本> 1.1.1 </version> <scope>提供</scope> </deverency> <! - slf4j的实现:logback:logback,log4j> log4j> <dependention> <grouped> <groupID> <groupId> <groupId> ch.qos.qos.logback.logback> groupId> log afifactid> log acclastiD> <imbackIdy> 0.00.20.2.2 <scope> Runtime </scope> </depentency> </depentencies> </project>
目录结构:
data-source.xml
1,上下文:物业持有人和上下文:组件 - 扫描前面解释过啦。
2,配置了一个sharddjedispool,在
一个是分片形式,可以连接有主备的redis服务端,一个是单个的。详细后续学习,一个是单个的。详细后续学习
3,spring-data-redis的封装,所以自己要自己封装一个
<?xml版本=“ 1.0”编码=“ utf-8”?> <beans xmlns =“ http://www.springframework.org/schema/beans/beans” xmlns:xsi =“ XMLNS:p =“ http://www.springframework.org/schema/p” xmlns:context =“ http://www.springframework.org/schema/schema/context/context/context” XMLNS:tx =“ http://www.springframework.org/schema/tx” xmlns:aop =“ http://wwwwwwwwwwwwwwwwwwwwwwwwerwork.org/schema/schema/aop/aop” http://www.springframework.org/schema/schema/spring-beans.xsd http://www.springframeworkframework/context.org/schema/schema/context http :///wwwwwwwwwwwwwwwwwwwwwwwwwwwwww.framework.orgramework.orglext-lotter topperta位置=“ class path:redis.properties” /> <上下文:component-scan base-package =“ com.d.work.main”> < /context:component-scan> <context:component-scan基本base-package =“ com.d.work.dork.dork.dork.redis”> < /cancent:component-scan> component-scan> name =“ maxidle” value =“ 8”/> <属性名称=“ maxwait” value =“ 1000”/> <属性名称=“ testOnBorrow” value =“ true”/> <property name =“ testOnreturn” value =“ true”/> <! - <property name =“ testwhileidle =” testwhileidle =“ testwhileidle” value =“ <constructor-arg index =“ 0” ref =” value =“ $ {redis.timeout}”/> <constructor-arg name =“ stroge” value =“ 1”/> </bean> </list> </constructor-arg> </bean> </bean> </beans>REDISDATASOURCE:定义三个方法
public界面redisdatasource {公共摘要shardedjedis getredisclient();公共void returnResource(sharddjedis sharddjedis);公共void returnResource(shardedjedis sharddjedis,boolean broken);};}Redisdatasource:
1,shardjedispool,这三个方法的作用::
@repository(“ redisdatasource”)公共类RedisDataSourceImpl instlind redisdatasource {私有静态最终logger log = loggerfactory.getLogger(redisdatasourceimpl.class); @Autowired的私人sharddjedispool sharddjedispool; public shardedjedis getredisclient(){try {shardedjedis shardjedis = shardedjedispool.getResource();返回shardjedis; } catch(异常e){log.error(“ getredisclent error”,e); }返回null; } public void returnResource(sharddjedis shardedjedis){shardedjedispool.returnresource(shardedjedis); } public void returnResource(shardedjedis shardedjedis,boolean broken){if(broken){shardedjedispool.returnbrokenterrokenresource(shardedjedis); } else {shardedjedispool.returnresource(shardedjedis); }}}}第二层的封装:RedisClientTemplate,例子实现了放值和取值。最后代码提供了全部命令的实现。
代码就是映射性质的又一次调用吉迪斯的方法而已,用了个,用了个,决定返还资源的方式。,决定返还资源的方式。,决定返还资源的方式。
这一层的目的主要也是让再上层的调用不需要关心池中链接的取得和返还问题了。
@repository(“ redisClientTemplate”)public类redisClientTemplate {private static final logger log = loggerfactory.getLogger(redisClientTemplate.class); @Autowired私人redisdatasource redisdatasource; public void disconnect(){shardedjedis shardedjedis = redisdatasource.getRedisClient(); sharddjedis.disconnect(); } / ** * * * * * @param键 * @param value * @return * / public string set(字符串键,字符串value){字符串result = null; sharddjedis shardedjedis = redisdatasource.getRedisclient(); if(shardedjedis == null){返回结果; } boolean broken = false;尝试{结果= shardedjedis.set(key,value); } catch(异常E){log.error(e.getMessage(),e);破碎= true; }最后{redisdatasource.returnresource(shardedjedis,broken); }返回结果; } / ** * * * * * @param键 * @return * / public String get(String键){字符串result = null; sharddjedis shardedjedis = redisdatasource.getRedisclient(); if(shardedjedis == null){返回结果; } boolean broken = false;尝试{结果= shardedjedis.get(key); } catch(异常E){log.error(e.getMessage(),e);破碎= true; }最后{redisdatasource.returnresource(shardedjedis,broken); }返回结果; }}}测试代码:
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”)); }附上redisclientTemplate全部实现:
redisClientTemplate代码太多,附上下载地址:http://xiazai.vevb.com/201701/yuanma/redisclientTemplate_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。,也希望大家多多支持武林网。