1 , 利用 spring-data-redis 整合
项目使用的 pom.xml :
<project xmlns = "http://maven.apache.org/pom/4.0.0" http://maven.apache.org/xsd/maven-4.0.0.xsd "> <Dodeversion> 4.0.0 </modelversion> <roupiD> com.x.redis </groupid> <StifactId> spring_redis </stifactid> <Url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> </perferies> <eperence> <eperalency> <rouperd> org.springframework.data </groupiD> <StifactId> spring-data-redis </stifactid> <sophy> 1.0.2.release </version> </sempendency> <reperency> <roupiD> org.springframework </rougeid> </groupiD> spring-core </stifactid> <StifactId> jedis </stifactid> <soph> 2.1.0 </version> </sependency> <reperency> <roupiD> Junit </roucpiD> <StifactId> Junit </stifactid> <splement> 4.8.2 </version> test </scope> </empency> <StifactId> slf4j-api </shintifactid> <الإصدار> 1.6.1 </version> </repreadency> <!-将现有的 Jakarta Commons Logging 的调用转换成 LSF4J 的调用。-> <redenced> </atsid> org.slf4j </groupid> </reperency> <!-اختراق : 确保 commons-logging 的 的 jar 包不被引入 否则将和 否则将和 jcl-over-slf4j 冲突-> <redence> <rouper> logging commons </rougiD> </scope> commons-logging </attifactid> <splem SLF4J : : تسجيل الدخول , , 用来取代 log4j 。更快、更强!-> <redence> <roupend> ch.qos.logback </groupId> <StifactId> تسجيل الدخول الكلاسيكي </artifactid> <splect> 0.9.24 </version>
除了 log 部分 , 只有一个 spring core 和 spring-data-redis 了
: :
ApplicationContext.xml :
1 , السياق: Property-Placeholder 标签用来导入 الخصائص 文件。从而替换 $ {redis.maxidle} 这样的变量。
2 , السياق: مكون المسح 是为了在 com.x.redis.dao 报下的类能够实用 Spring 的注解注入的方式。
3 , 事实上我们只需要把 jedispoolconfig 配数来就好了 , 接下来就是 spring 的封装了。所以直接看 userdaoimpl 的实现就明白了。
<؟ xmlns: p = "http://www.springframework.org/schema/p" xmlns: tx = "http://www.springframework.org/schema/tx" http://www.springframework.org/schema/beans/spring-beans.xsd <السياق: property-placeholder location = "classpath: redis.properties" /> <context: component-scan base-package = "com.x.redis.dao"> < /context: component-scan> <bean id = "poolconfig"> <property name = "maxidle" value = value = "$ {redis.maxactive}" /> <property name = "maxwait" value = "$ {redis.maxwait}" /> <property name = "testOnBorrow" value = "$ {redis.testonborour}" /> < /bean id = "connectionfactory" p: name-name = $ {red} P: port = "$ {redis.port}" p: password = "$ {redis.pass}" p: pool-config-ref = "poolconfig"/> <bean id = "redistemplate"> <property name = "connectionfactory" ref = "connectionfactory"/> </bean> redis.properties :
#redis إعدادات#redis.host = 192.168.20.101#redis.port = 6380#redis.pass = foobaredredis.host = 127.0.1
userDaoimpl :
1 , الربيع 对 داو 层的封装很多用了类似于下面代码的模板方式。
2 , redistemplate 就是 Spring 对 redis 的一个封装而已。
الفئة العامة userDaoImpl تنفذ userdao {autowired redistemplate المحمية <serializable ، serializable> redistemplate ؛ public void saveUser (المستخدم النهائي للمستخدم) {redistemplate.execute (جديد rediscallback <object> () {Override كائن عام doinredis (connection redisconnection) يلقي dataAccessException {connection.set redistemplate.getSterializer (). serialize (user.getName ()) ؛ } Override Public User GetUser (معرف طويل نهائي) {redistemplate.execute (جديد rediscallback <Sether> () {Override public user doinredis (connection redisconnection) يلقي dataAccessException {byte [] = key) }}: :
مستخدم:
مستخدم الفئة العامة {private long id ؛ اسم السلسلة الخاصة ؛ Public Long getId () {return id ؛ } public void setId (id long) {this.id = id ؛ } السلسلة العامة getName () {return name ؛ } public void setName (اسم السلسلة) {this.name = name ؛ }} : :
public static void main (string [] args) {applicationContext ac = new ClassPathxMlAppLicationContext ("classPath: /applicationContext.xml") ؛ userDao userDao = (userDao) ac.getBean ("userDao") ؛ المستخدم المستخدم 1 = مستخدم جديد () ؛ user1.setId (1) ؛ user1.setName ("أوباما") ؛ userDao.saveuser (user1) ؛ user user2 = userDao.getuser (1) ؛ System.out.println (user2getName ()) ؛ }2 , 不利用 spring-data-redis 整合
个人觉得这样整合灵活度更大 , 能够更加明了的完成任务。
pom.xml:
<project xmlns = "http://maven.apache.org/pom/4.0.0" http://maven.apache.org/xsd/maven-4.0.0.xsd "> <Dodeversion> 4.0.0 </modelversion> <roupiD> com.d.work </rougiD> </suntifactid> redis_templete </stifactid> <name> redis_templete </same> <Url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> </sependencies> <StifactId> junit </stifactid> <الإصدار> 3.8.1 </version> <scope> اختبار </scope> </sependency> <redence> <rouplyid> drefis.clients </rougiD> <SetifActId> jedis </spring> <spling> <StifactId> spring-core </artifactId> <soph> <StifactId> spring-context </stifactId> <sople> 3.1.2.release </version> </premitency> <reperency> <roupiD> org.slf4j </groupId> <StifactId> slf4j-api </stifactid> <Rependency> <roupEd> org.slf4j </rougiD> <StifactId> jcl-over-slf4j </artifactId> <sored> 1.6.1 </version> </dependency> <!-hack : commons logging 的 的 包不被引入 , 否则将和 否则将和 jcl-over-slf4j 冲突-> <! <SnifactId> commons-logging </intifactId> <sored> 1.1.1 </version> <scope> متوفر </scope> </repreadency> <!-slf4j 的实现 : تسجيل الدخول <scope> وقت التشغيل </scope> </reperence> </perendencies> </project>
: :
البيانات-source.xml
1 , السياق: صاحب property-placeholder 和 السياق: مكون المسح 前面解释过啦。
2 , 配置了一个 shardedjedispool , 在 jdeis 里 还有个 jedispool 。这两个的区别:
一个是分片形式 , 可以连接有主备的 redis 服务端 , 一个是单个的。详细后续学习
3 , 因为不使用 spring-data-redis 的封装 , 所以自己要自己封装一个
<؟ xmlns: p = "http://www.springframework.org/schema/p" xmlns: tx = "http://www.springframework.org/schema/tx" http://www.springframework.org/schema/beans/spring-beans.xsd <السياق: property-placeholder location = "classpath: redis.properties"/> <context: component-scan base-package = "com.d.work.main"> </context: component-scan> name = "maxactive" value = "50"/> <property name = "maxidle" value = "8"/> <property name = "maxwait" value = "1000"/> <property name = "testOnBorrow id = "shardedjedispool" scope = "singleton"> <constructor-arg index = "0" ref = "jedispoolconfig" /> <constructor-arg index = "1"> <list> <bule> <bustructor-arg name = "host" value = "$ {redis.host}" /> /> <constructor-arg name = "timeout" value = "$ {redis.timeout}"/> <constructor-arg name = "weight" value = "1"/> </bean> </list> </structructor-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") الطبقة العامة RedisDataSourceImpl تنفذ RedisDataSource {private static final logger log = loggerfactory.getLogger (redisDataSourceImpl.Class) ؛ @autowired shardedjedispool shardedjedispool ؛ public shardedjedis getRedisClient () {try {shardedjedis Shardjedis = shardedjedispool.getResource () ؛ إرجاع Shardjedis. } catch (استثناء e) {log.error ("getRedisclent Error" ، e) ؛ } إرجاع فارغ ؛ } public void returnResource (Shardedjedis Shardedjedis) {shardedjedispool.returnresource (shardedjedis) ؛ } public void returnResource (ShardedJedis Shardedjedis ، Boolean Broken) {if (broken) {ShardedJedispool.ReturnbrokenResource (ShardedJedis) ؛ } آخر {shardedjedispool.returnresource (shardedjedis) ؛ }}}: redisclienttemplate , 例子实现了放值和取值。最后代码提供了全部命令的实现。
代码就是映射性质的又一次调用 Jedis 的方法而已 , 用了个 用了个 来做标示符 , 决定返还资源的方式。 决定返还资源的方式。
这一层的目的主要也是让再上层的调用不需要关心 حمام السباحة 中链接的取得和返还问题了。
repository ("redisclienttemplate") الفئة العامة redisclientTemplate {private static final logger log = loggerfactory.getLogger (redisclienttemplate.class) ؛ @autowired redisdatasource redisdatasource ؛ public void disconnect () {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 ؛ } boolean broken = false ؛ حاول {result = shardedjedis.set (المفتاح ، القيمة) ؛ } catch (استثناء e) {log.error (e.getMessage () ، e) ؛ كسر = صحيح ؛ } أخيرًا {redisdatasource.returnresource (shardedjedis ، broken) ؛ } نتيجة الإرجاع ؛ } / ** * 获取单个值 * * @param مفتاح * @RETURN * / السلسلة العامة GET (مفتاح السلسلة) {String result = null ؛ Shardedjedis ShardedJedis = redisdatasource.getRedisClient () ؛ if (shardedjedis == null) {return result ؛ } boolean broken = false ؛ حاول {result = shardedjedis.get (مفتاح) ؛ } 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
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。