1, -d 스프링-다타-빨간색 整合
项目使用的 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.apache.org/pom/0.0.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.x.redis </groupid> <artifactid> spring_redis </artifactid> <1.0-snap </spring </springing> <Url> http://maven.apache.org </url> <properties> <proffect.build.sourceencoding> utf-8 </project.build.sourceencoding> </properties> <pectionement> <groupid> org.springframework.data> <Artifactid> Spring-Data-Redis </artifactid> <bersion> 1.0.2. Release </version> </fectionency> <prectionement> <groupid> org.springframework </groupid> <artifactid> spring-core </artifactid> <version> 3.1.2. release> <pelaxency> <groupid> <Artifactid> jedis </artifactid> <bersion> 2.1.0 </version> </dependency> <pectomency> <groupid> junit </groupid> <artifactid> junit> junit> </artifactid> <버전> 4.8.2 </version> <cope> test> test> <groupid> </groupid> org. <Artifactid> slf4j-api </artifactid> <bersion> 1.6.1 </version> </dependency> <! <jakarta commons 로깅 的调用转换成 lsf4j 的调用。-> <groupID> org.slf4j </groupid> <artifactid> jcl-over-slf4j </artifactid> </dependency> <-해킹 : 确保 Commons-Logging 的 jar 包不被引入 包不被引入 包不被引入, 否则将和 jcl-over-slf4j 冲突-> <groupid> commons-logging </groupid> <artifactid> commons-logging </artifactid> <1.1.1 </version> scope> 제공 </scope> </scope> <!- SLF4J : logback, 用来取代 log4j 。更快、更强!-> <pectionency> <groupid> ch.qos.logback </groupid> <artifactid> logback classic </artifactid> <버전> 0.9.24 </version> runtime </scope> </fectionency> </fectionemies> </project>
除了 로그 , ,, 只有一个 스프링 코어 和 스프링-다타-레 디스 了
项目文件目录结构 :
ApplicationContext.xml :
1, 컨텍스트 : 부동산-소유자 标签用来导入 속성 文件。从而替换 $ {redis.maxidle} 这样的变量。
2, 컨텍스트 : Component-Scan 是为了在 com.x.redis.dao 报下的类能够实用 스프링 的注解注入的方式。
3, ed Jedispoolconfig 配数来就好了, 接下来就是 스프링 的封装了。所以直接看 userdaoimpl 的实现就明白了。
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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.springfrfringwork.org/schema/eee. xmlns : tx = "http://www.springframework.org/schema/tx"xmlns : aop = "http://www.springframework.org/schema/aop"xsi : schemalocation = "http://www.springframwork.org/schema/schema/springwork.org/schema/schemas http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.spramework.org/schema/context/sprring-context .xsd : context : context location = "classpath : redis.properties" /> <context : component-scan base-package = "com.x.redis.dao"> < /context : component-scan> <bean id = "poolconfig"> <property name = "maxidle"value = "$ {redis.maxidle}" />> <속성 이름 = "maxactive" "$ {maxive}" /> <property name = "maxwait"value = "$ {redis.maxwait}" /> <property name = "testonborring"value = "$ {redis.testonborring}" /> < /bean> <bean id = "ConnectionFactory"p : host-name = "$ {redis.host}"p : {redis.port} " p : password = "$ {redis.pass}"p : pool-config-ref = "poolconfig"/> <bean id = "redistemplate"> <property 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 = 600 redis.maxwait = 10000redis.testonbourment = true
userdaoimpl :
1, 스프링 对 dao 层的封装很多用了类似于下面代码的模板方式。
2, Redistemplate 就是 스프링 对 Redis 的一个封装而已。
Public Class userDaoimpl은 userDao {@autowired Protected redistemplate <SEALIALIZABLE, SERIALIZABLE> REDISTEMPLATE를 구현합니다. public void saveUser (최종 사용자) {redistemplate.execute (new readiscallback <botort> () {@override public object doinredis (readisconnection connection)는 dataAccessException {connection.set (redistemplate.getStringSeriorize.getStringsTreSerializer (). serialize ( "user.uid" + user.getid ()), redistemplate.getStringserializer (). Serialize (user.getName ()); } @Override public user getUser (Final Long ID) {return redistemplate.execute (new readiscallback <user> () {@override public user doinredis (readisconnection connection) dataAccessException {byte [] key = redistemplate.getStringserializer (). Serialize ( " + id); if (neh id); value.get (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 (문자열 이름) {this.name = 이름; }}测试代码 :
public static void main (String [] args) {ApplicationContext ac = new ClassPathXmlApplicationContext ( "classPath :/applicationContext.xml"); userDao userDao = (userDao) ac.getBean ( "userDao"); 사용자 user1 = 새 사용자 (); user1.setID (1); user1.setName ( "오바마"); userdao.saveuser (user1); user user2 = userdao.getuser (1); System.out.println (user2.getName ()); }2, -d 스프링-다타-빨간색 整合
个人觉得这样整合灵活度更大, 能够更加明了的完成任务。
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.apache.org/pom/0.0.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.d.work </groupid> <artifactid> redis_templete </artifactid> <버전> 1.0-snapshot> </snapShot> <name> redis_templete </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> </propertencies> <pectient> <groupid> junit> <Artifactid> junit </artifactid> <bersion> 3.8.1 </version> <copo> </scope> </spectency> <pectionement> <groupid> redis.clients </groupid> <artifactid> jedis </artifactid> <version> 2.1.0 </dectortency> <GroupSid> Org.spremwork. <Artifactid> Spring-Core </artifactid> <bersion> 3.1.2. Release </version> </fectionency> <pectomency> <groupId> org.springFramework </groupID> <artifActId> Spring-Beans </artifactid> <bersion> 3.1.2. Release </versionemency> <GroupD> <groupD.Sprongramwork. <아티 팩트> 스프링-컨텍스트 </artifactid> <bersion> 3.1.2.2. release </version </version </version> </dependency> <groupid> org.slf4J </groupId> <artifactId> slf4j-api </artifactid> <6.1 </version> <!-locharta jakarta jakonsger <pectionency> <groupId> org.slf4j </groupid> <artifactid> jcl-over-slf4j </artifactid> <버전> 1.6.1 </version> </fectionement> <!-hack : 确保 Commons-logging <Artifactid> Commons-logging </artifactid> <버전> 1.1.1 </version> <copo> 제공 </scope> </fectionency> <!-slf4j 的实现 : logback, 用来取代 log4j 。更快、更强!-> <groupid> ch.qos.logback </groupid> </version> </artifactic </artifactic </artifactic </artifactid>. <Scope> 런타임 </scope> </dependency> </dependencies> </project>
目录结构 :
Data-Source.xml
1, 컨텍스트 : 부동산 공정 담당자 和 컨텍스트 : 구성 요소 스캔 前面解释过啦。
2, ard Shardedjedispool, 在 jdeis 里 还有个 jedispool 。这两个的区别 :
一个是分片形式, 可以连接有主备的 re redis 服务端, 一个是单个的。详细后续学习
3, -d 스프링-다타-레디스 的封装, 所以自己要自己封装一个
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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.springfrfringwork.org/schema/eee. xmlns : tx = "http://www.springframework.org/schema/tx"xmlns : aop = "http://www.springframework.org/schema/aop"xsi : schemalocation = "http://www.springframwork.org/schema/schema/springwork.org/schema/schemas http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.spramework.org/schema/context/sprring-context .xsd : context : context location = "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 : <bean id = "jedispoolconfig"> <productive ">"jedispoolconfig "> /> <property name = "maxIdle"value = "8"/> <속성 이름 = "maxWait"value = "1000"/> <속성 이름 = "testonborring"value = "true"/> <property name = "true"value = "true"/> <!-<property name = "testwhileidle"value = "true"/>-> <a id = "shardedjedispool" 스코프 = "싱글 톤"> <생성자-arg index = "0"0 "ref ="jedispoolconfig " /> <constructor-arg index ="1 "> <list> <bean> <constructor-arg name ="host "value ="$ {redis.host} " /> <value ="port ""ports " 이름 = "timeout"value = "$ {redis.timeout}"/> <생성자-arg name = "weight"value = "1"/> </bean> </list> </constructor-arg> </bean> </beans>Redisdatasource : 定义三个方法
공개 인터페이스 redisdatasource {public Abstract Shardedjedis getRedisclient (); 공개 void returnResource (Shardedjedis Shardedjedis); 공개 void returnResource (Shardedjedis Shardedjedis, 부울 부러짐);}Redisdatasource :
1, 注入配置好的 Shardedjedispool, 这三个方法的作用 :
@repository ( "redisdatasource") 공개 클래스 redisdatasourceimpl은 redisdatasource {private static final logger log = loggerfactory.getLogger (redisdatasourceimpl.class); @autowired Private Shardedjedispool Shardedjedispool; public shardedjedis getredisclient () {try {shardedjedis shardjedis = shardedjedispool.getresource (); 반환 샤드 제드; } catch (예외 e) {log.error ( "getRedisclent Error", e); } return null; } public void returnResource (Shardedjedis Shardedjedis) {Shardedjedispool.returnresource (Shardedjedis); } public void returnResource (Shardedjedis Shardedjedis, 부울 Broken) {if (broken) {shardedjedispool.returnbrokenresource (Shardedjedis); } else {shardedjedispool.returnresource (Shardedjedis); }}}第二层的封装 : redisclientTemplate, 例子实现了放值和取值。最后代码提供了全部命令的实现。
代码就是映射性质的又一次调用 Jedis is is, 用了个 부러진 来做标示符 来做标示符, 决定返还资源的方式。
这一层的目的主要也是让再上层的调用不需要关心 수영장 中链接的取得和返还问题了。
@repository ( "redisclientTemplate") 공개 클래스 redisclientTemplate {private static final logger log = loggerfactory.getLogger (redisclientTemplate.class); @autowired Private redisdatasource redisdatasource; public void Disternect () {shardedjedis shardedjedis = redisdatasource.getRedisclient (); shardedjedis.disconnect (); } / ** * * * @param key * @param value * @return * / public String set (문자열 키, 문자열 값) {String result = null; Shardedjedis shardedjedis = redisdatasource.getRedisClient (); if (shardedjedis == null) {return result; } 부울 깨진 = 거짓; try {result = shardedjedis.set (key, value); } catch (예외 e) {log.error (e.getMessage (), e); 깨진 = 참; } 마침내 {redisdatasource.returnresource (Shardedjedis, Broken); } 반환 결과; } / ** * * * * @param key * @return * / public string get (String key) {String result = null; Shardedjedis shardedjedis = redisdatasource.getRedisClient (); if (shardedjedis == null) {return result; } 부울 깨진 = 거짓; try {result = shardedjedis.get (key); } catch (예외 e) {log.error (e.getMessage (), e); 깨진 = 참; } 마침내 {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
以上就是本文的全部内容 以上就是本文的全部内容, 希望对大家的学习有所帮助, 也希望大家多多支持武林网。