1 , 利用 Springdata-redis 整合
项目使用的 pom.xml:
<project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance 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> <projekt.build.sourceEncoding> utf-8 </project.build.SourceEnencoding> </properties> </gruppen> <gruppe> org.springramework.data </GroupId> <artifactId> Spring-data-redis </artifactId> <version> 1.0.2.Release </Version> </abhängig> <abhängigkeit> <GroupID> org.springFramework </Groupid> <artifactID> Spring-Core </artifactid> <version> 3.1.2. <artifactId> jedis </artifactId> <version> 2.1.0 </Version> </abhängig> <Depapech> <GroupId> junit </Groupid> <artifactID> junit </artifactId> <version> 4.8.2 </Version> <Scope> Test </scope> </abhängig> <deponciench> </gruppe> org. <artifactId> SLF4J-API </artifactId> <version> 1.6.1 </Version> </abhängig> <!-将现有的 Jakarta Commons Protokollierung 的调用转换成 lsf4j 的调用。-> <abhängig> <GroupId> org.slf4j </gruppen> <artifactid> jcl-over-Slf4j </artifactid> <artifactid> Jcl-Over-SLF4J </artifactId> 1.6. </abhängig> <!-hack : 确保 commons-logging 的 jar 包不被引入 , 否则将和 jcl-over-slf4j 冲突-> <abhängig> <gruppeId> commons-logging </gruppe> <artifactid> Commons-Logging </artifactid> <version> 1.1.1 </Version> </cepere> 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 部分 , 只有一个 Federkern 和 Springdata-redis 了
项目文件目录结构 :
applicationContext.xml:
1 , Kontext: Immobilien-Platzhalter 标签用来导入 Eigenschaften 文件。从而替换 $ {redis.maxidle} 这样的变量。
2 , Kontext: Komponenten-Scan 是为了在 com.x.redis.dao 报下的类能够实用 Spring 的注解注入的方式。
3 , 事实上我们只需要把 Jedispoolconfig 配数来就好了 , 接下来就是 Spring 的封装了。所以直接看 userDaoImpl 的实现就明白了。
<? xmlns: p = "http://www.springframework.org/schema/p" xmlns: context = "http://www.spingframework.org/schema/context" xmlns: Jee = "http://jewww.spingframeWramework xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: aop = "http://www.spingframework.org/schema/aop" xsi: Schemalocation = "http://wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww.- http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sping/sping/sping/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling.-Pring/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING: location = "classPath: redis.Properties" /> <context: component-scan base-package = "com.x.redis.dao"> < /context: component-scan> <bean id = "poolconfig" <Eigenschaft name = "maxidle" value = "$ {maxidle}}}" /> <Stiefname ". <Eigenschaft name = "maxwait" value = "$ {redis.maxwait}" /> <Eigenschaft name = "testonborrow" value = "$ {redis.testonborrow}" /> < /bean> <bean id = "ConnectionFactory" p: Host-Name = "$ {redis.host}" p: port = "{$ {{{{$ {$ {$ {port}" {$ {$ {{{port} "{{{{{{{$ {port}" {{{{{{{{$ {port} p: password = "$ {redis.pass}" p: pool-config-ref = "poolconfig"/> <bean id = "redItemplate"> <Eigenschaft name = "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=1000redis.testOnBorrow=true
UserdaoImpl:
1 , Feder 对 Dao 层的封装很多用了类似于下面代码的模板方式。
2 , Redistemplate 就是 Feder 对 Redis 的一个封装而已。
public class userDaoImpl implementiert userDao {@autowired Protected Redistemplate <Serialisierbar, serialisierbar> redistemplate; public void SaveUser (endgültiger Benutzerbenutzer) {redistemplate.execute (neuer rediscallback <Object> () {@Override public Object doinRedis (redisconnection Connection) löst DataAccessException {Connection.set (redistemplate.getStringSerializer (). Serialize ("user.uid (redistemplate)). redistemplate.getStringSerializer (). Serialize (user.getName ()); } @Override public User getUser (endgültige lange ID) {return redistemplate.execute (neuer rediscallback <Benutzer> () {@Override public user doinRedis (redisconnection -Verbindung) löscht DataAccessexception {byte [] key = redistemplate.getStringSerializer (). value = connection.get (Schlüssel). }}其他 :
Benutzer:
Public Class User {private long id; privater Zeichenfolge Name; 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 classPhodexmlApplicationContext ("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 , 不利用 Springdata-redis 整合
个人觉得这样整合灵活度更大 , 能够更加明了的完成任务。 能够更加明了的完成任务。
pom.xml:
<project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.d.work</groupId> <artifactId>Redis_Templete</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <Name> redis_templete </name> <URL> http://maven.apache.org </url> <properties> <projekt.build.sourceEncoding> utf-8 </project.build.SourceCoding> </properties> <deponcies> </gruppy> </gruppy> </gruppy> <artifactId>junit</artifactId> <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> </abhängig> <Depopentcy> <GroupID> org.springFramework </gruppen> <artifactId> Spring-Beer </artifactId> <version> 3.1.2. <artifactId> Spring-Context </artifactId> <version> 3.1.2.Release </Version> </abhängig> <depeopecy> <GroupId> org.slf4j </Groupid> <artifactId> SLF4J-API </artifactId> <version> 1.6.1 </Version> </vgl. --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.6.1</version> </dependency> <!-- Hack:确保commons-logging的jar包不被引入,否则将和jcl-over-slf4j冲突 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> <scope>provided</scope> </dependency> <!-- slf4j的实现:logback,用来取代log4j。更快、更强! --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>0.9.24</version> <Scope> Laufzeit </scope> </abhängig> </abhängigkeiten> </project>
目录结构 :
Data-Source.xml
1 , Kontext: Eigentumsplatzinhaber 和 Kontext: Komponenten-Scan 前面解释过啦。
2 , 配置了一个 shardedjedispool , 在 jdeis 里 还有个 Jedispool 。这两个的区别:
一个是分片形式 , 可以连接有主备的 Redis 服务端 , 一个是单个的。详细后续学习
3 , 因为不使用 Springdata-redis 的封装 , 所以自己要自己封装一个
<? xmlns: p = "http://www.springframework.org/schema/p" xmlns: context = "http://www.spingframework.org/schema/context" xmlns: Jee = "http://jewww.spingframeWramework xmlns: tx = "http://www.springframework.org/schema/tx" xmlns: aop = "http://www.spingframework.org/schema/aop" xsi: Schemalocation = "http://wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww.- http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sping/sping/sping/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling/spling.-Pring/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING/SPRING: location = "classPath: redis.properties" /> <context: component-scan base-package = "com.d.work name = "maxidle" value = "8"/> <Eigenschaft name = "maxwait" value = "1000"/> <Eigenschaft name = "testonborrow" value = "true"/> <Property name = "testonReturn" value = "true"/> <!-<Property name = "testwheIdle" value = "true"/> </> </lean> </lean> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1"> <list> <bean> <constructor-arg name="host" value="${redis.host}" /> <constructor-arg name="port" value="${redis.port}" /> <constructor-arg name="timeout" value = "$ {redis.timeout}"/> <constructor-arg name = "Weight" value = "1"/> </bean> </list> </constructor-arg> </bean> </beans>RedisdataSource: 定义三个方法
public interface redisdataSource {public Abstract Shardedjedis getRedisclient (); public void returnResource (Shardedjedis Shardedjedis); öffentliche void returnResource (Shardedjedis Shardedjedis, Boolean gebrochen);}实现 RedisdataSource:
1, 注入配置好的 Shardedjedispool , : :
@Repository ("redisDataSource") öffentliche Klasse RedisDataSourcEImPL implementiert redisdataSource {private statische endgültige logger logger log = loggerfactory.getLogger (redisdataSourcEImpl.class); @Autowired Private Shardedjedispool Shardedjedispool; public shardedjedis getRedisclient () {try {shardedjedis shardjedis = shardedjedispool.getResource (); Rückkehr Shardjedis; } catch (Ausnahme e) {log.Error ("getRedisclent -Fehler", e); } return null; } public void returnResource (shardedjedis shardedjedis) {Shardedjedispool.ReturnResource (Shardedjedis); } public void returnResource (shardedjedis shardedjedis, boolean gebrochen) {if (gebrochen) {shardedjedispool.returnbrokenResource (shardedjedis); } else {Shardedjedispool.ReturnResource (Shardedjedis); }}}第二层的封装 : RedisclientTemplate , 例子实现了放值和取值。最后代码提供了全部命令的实现。 例子实现了放值和取值。最后代码提供了全部命令的实现。
代码就是映射性质的又一次调用 jedis 的方法而已 , 用了个 gebrochen 来做标示符 , 决定返还资源的方式。 决定返还资源的方式。 决定返还资源的方式。
这一层的目的主要也是让再上层的调用不需要关心 Pool 中链接的取得和返还问题了。
@Repository ("redisclientTemplate") public class RedisclientTemplate {private statische logger 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 -Schlüssel, String -Wert) {String result = null; Shardedjedis shardedjedis = redisdatasource.getRedisclient (); if (shardedjedis == null) {return Ergebnis; } boolean kaputt = false; try {result = shardedjedis.set (Schlüssel, Wert); } catch (Ausnahme e) {log.Error (e.getMessage (), e); gebrochen = wahr; } endlich {redisDataSource.ReturnResource (Shardedjedis, gebrochen); } Rückgabeergebnis; } / ** * 获取单个值 * * @param key * @return * / public String get (string key) {String result = null; Shardedjedis shardedjedis = redisdatasource.getRedisclient (); if (shardedjedis == null) {return Ergebnis; } boolean kaputt = false; try {result = shardedjedis.get (Schlüssel); } catch (Ausnahme e) {log.Error (e.getMessage (), e); gebrochen = wahr; } endlich {redisDataSource.ReturnResource (Shardedjedis, gebrochen); } Rückgabeergebnis; }}测试代码 :
public static void main (string [] args) {applicationContext ac = new classPhodexMlApplicationContext ("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
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。 也希望大家多多支持武林网。