好了费话不多说了,介绍下spring 结合redis是怎么操作数据的 这里我用了maven管理,由于简单嘛,依赖下包就行了..不用单独去依赖包,成了我的习惯


好了,下面是pom的代码

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>redis</groupId>
  5. <artifactId>redis</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <build>
  8. </build>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework.data</groupId>
  12. <artifactId>spring-data-redis</artifactId>
  13. <version>1.0.2.RELEASE</version>
  14. </dependency>
  15. </dependencies>
  16. </project>

在pom里添加了redis spring客户端的依赖,那么所有的jar包都会帮你自动下载下来,是不是很方便啊,哈


下面是spring-context.xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
  7. http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
  9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
  10. <!--注解说明 -->
  11. <context:annotation-config />
  12. <!-- 把标记了@Controller注解的类转换为bean -->
  13. <context:component-scan base-package="com.mkfree.**" />
  14. <!-- redis工厂 -->
  15. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
  16. p:host-name="192.168.9.140" p:port="6379" p:password="87980879" />
  17. <!-- redis服务封装 -->
  18. <bean id="redisService" class="com.mkfree.redis.test.RedisService">
  19. </bean>

这里配置了一个跟spring 集成的redis客户端,ip port password自己定哦,这里我在redis配置文件了定义了需求密码认证,你们先不要用吧,为了简单起见


下面是RedisService,里面包含了对redis的方法,还有更多的方法没有去使用,这里当一个入门的小例子吧

  1. package com.mkfree.redis.test;
  2. import java.util.Set;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  6. import redis.clients.jedis.Jedis;
  7. /**
  8. * 封装redis 缓存服务器服务接口
  9. * @author hk
  10. *
  11. * 2012-12-16 上午3:09:18
  12. */
  13. public class RedisService {
  14. /**
  15. * 通过key删除(字节)
  16. * @param key
  17. */
  18. public void del(byte [] key){
  19. this.getJedis().del(key);
  20. }
  21. /**
  22. * 通过key删除
  23. * @param key
  24. */
  25. public void del(String key){
  26. this.getJedis().del(key);
  27. }
  28. /**
  29. * 添加key value 并且设置存活时间(byte)
  30. * @param key
  31. * @param value
  32. * @param liveTime
  33. */
  34. public void set(byte [] key,byte [] value,int liveTime){
  35. this.set(key, value);
  36. this.getJedis().expire(key, liveTime);
  37. }
  38. /**
  39. * 添加key value 并且设置存活时间
  40. * @param key
  41. * @param value
  42. * @param liveTime
  43. */
  44. public void set(String key,String value,int liveTime){
  45. this.set(key, value);
  46. this.getJedis().expire(key, liveTime);
  47. }
  48. /**
  49. * 添加key value
  50. * @param key
  51. * @param value
  52. */
  53. public void set(String key,String value){
  54. this.getJedis().set(key, value);
  55. }
  56. /**添加key value (字节)(序列化)
  57. * @param key
  58. * @param value
  59. */
  60. public void set(byte [] key,byte [] value){
  61. this.getJedis().set(key, value);
  62. }
  63. /**
  64. * 获取redis value (String)
  65. * @param key
  66. * @return
  67. */
  68. public String get(String key){
  69. String value = this.getJedis().get(key);
  70. return value;
  71. }
  72. /**
  73. * 获取redis value (byte [] )(反序列化)
  74. * @param key
  75. * @return
  76. */
  77. public byte[] get(byte [] key){
  78. return this.getJedis().get(key);
  79. }
  80. /**
  81. * 通过正则匹配keys
  82. * @param pattern
  83. * @return
  84. */
  85. public Set<String> keys(String pattern){
  86. return this.getJedis().keys(pattern);
  87. }
  88. /**
  89. * 检查key是否已经存在
  90. * @param key
  91. * @return
  92. */
  93. public boolean exists(String key){
  94. return this.getJedis().exists(key);
  95. }
  96. /**
  97. * 清空redis 所有数据
  98. * @return
  99. */
  100. public String flushDB(){
  101. return this.getJedis().flushDB();
  102. }
  103. /**
  104. * 查看redis里有多少数据
  105. */
  106. public long dbSize(){
  107. return this.getJedis().dbSize();
  108. }
  109. /**
  110. * 检查是否连接成功
  111. * @return
  112. */
  113. public String ping(){
  114. return this.getJedis().ping();
  115. }
  116. /**
  117. * 获取一个jedis 客户端
  118. * @return
  119. */
  120. private Jedis getJedis(){
  121. if(jedis == null){
  122. return jedisConnectionFactory.getShardInfo().createResource();
  123. }
  124. return jedis;
  125. }
  126. private RedisService (){
  127. }
  128. //操作redis客户端
  129. private static Jedis jedis;
  130. @Autowired
  131. @Qualifier("jedisConnectionFactory")
  132. private JedisConnectionFactory jedisConnectionFactory;
  133. }

下面是测试代码TestRedis.java

    1. package com.mkfree.redis.test;
    2. import java.util.Set;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;
    5. /**
    6. * redis spring 简单例子
    7. * @author hk
    8. *
    9. * 2012-12-22 上午10:40:15
    10. */
    11. public class TestRedis {
    12. public static void main(String[] args) throws InterruptedException {
    13. ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
    14. //这里已经配置好,属于一个redis的服务接口
    15. RedisService redisService = (RedisService) app.getBean("redisService");
    16. String ping = redisService.ping();//测试是否连接成功,连接成功输出PONG
    17. System.out.println(ping);
    18. //首先,我们看下redis服务里是否有数据
    19. long dbSizeStart = redisService.dbSize();
    20. System.out.println(dbSizeStart);
    21. redisService.set("username", "oyhk");//设值(查看了源代码,默认存活时间30分钟)
    22. String username = redisService.get("username");//取值
    23. System.out.println(username);
    24. redisService.set("username1", "oyhk1", 1);//设值,并且设置数据的存活时间(这里以秒为单位)
    25. String username1 = redisService.get("username1");
    26. System.out.println(username1);
    27. Thread.sleep(2000);//我睡眠一会,再去取,这个时间超过了,他的存活时间
    28. String liveUsername1 = redisService.get("username1");
    29. System.out.println(liveUsername1);//输出null
    30. //是否存在
    31. boolean exist = redisService.exists("username");
    32. System.out.println(exist);
    33. //查看keys
    34. Set<String> keys = redisService.keys("*");//这里查看所有的keys
    35. System.out.println(keys);//只有username username1(已经清空了)
    36. //删除
    37. redisService.set("username2", "oyhk2");
    38. String username2 = redisService.get("username2");
    39. System.out.println(username2);
    40. redisService.del("username2");
    41. String username2_2 = redisService.get("username2");
    42. System.out.println(username2_2);//如果为null,那么就是删除数据了
    43. //dbsize
    44. long dbSizeEnd = redisService.dbSize();
    45. System.out.println(dbSizeEnd);
    46. //清空reids所有数据
    47. //redisService.flushDB();
    48. }
    49. }

spring 结合 redis 例子 (转)的更多相关文章

  1. Spring Boot 2 + Redis例子

    Redis是一个key-value数据库,支持存储的value类型包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型).在 ...

  2. spring和redis的整合-超越昨天的自己系列(7)

    超越昨天的自己系列(7) 扯淡:  最近一直在慢慢多学习各个组件,自己搭建出一些想法.是一个涉猎的过程,慢慢意识到知识是可以融汇贯通,举一反三的,不过前提好像是研究的比较深,有了自己的见解.自认为学习 ...

  3. spring和redis的整合

    spring和redis的整合-超越昨天的自己系列(7) 超越昨天的自己系列(7) 扯淡:  最近一直在慢慢多学习各个组件,自己搭建出一些想法.是一个涉猎的过程,慢慢意识到知识是可以融汇贯通,举一反三 ...

  4. Spring Data Redis实现消息队列——发布/订阅模式

    一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现. 定义:生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...

  5. Spring Data JPA例子[基于Spring Boot、Mysql]

    关于Spring Data Spring社区的一个顶级工程,主要用于简化数据(关系型&非关系型)访问,如果我们使用Spring Data来开发程序的话,那么可以省去很多低级别的数据访问操作,如 ...

  6. spring配置redis注解缓存

    前几天在spring整合Redis的时候使用了手动的方式,也就是可以手动的向redis添加缓存与清除缓存,参考:http://www.cnblogs.com/qlqwjy/p/8562703.html ...

  7. Spring Data Redis学习

    本文是从为知笔记上复制过来的,懒得调整格式了,为知笔记版本是带格式的,内容也比这里全.点这里 为知笔记版本 Spring Data Redis 学习 Version 1.8.4.Release 前言 ...

  8. Spring Boot使用Spring Data Redis操作Redis(单机/集群)

    说明:Spring Boot简化了Spring Data Redis的引入,只要引入spring-boot-starter-data-redis之后会自动下载相应的Spring Data Redis和 ...

  9. Spring Data Redis入门示例:基于Jedis及底层API (二)

    使用底层API:RedisConnectionFactory和RedisConnection可以直接操作Redis,下面是一个简单的例子: ### Maven依赖 <properties> ...

随机推荐

  1. @PathVariable、@RequestParam、@RequestBody注解

    讲解更加详细的参考资料 https://blog.csdn.net/u011410529/article/details/66974974 https://www.cnblogs.com/soul-w ...

  2. C语言小笔记(1)

    枚举类型的大小是4,和一个int整形大小一样 就是最后一个逗号后面的表达式的值,比如: int a=1,b; b=(a+1,a+2,a+3); 那么b的值就是a+3,也就是4 函数名   :print ...

  3. 阿里云安装Cloudera Manager(草稿)

    选择三台同一局域网的阿里云服务器 最初使用阿里云.京东云.百度云的三台不同的服务器,遇到一些问题,没有解决,公网速度也没有保障,还是选择同一局域网的服务器吧 CM有三种不同的安装方式: 通过 Clou ...

  4. 【和孩子一起学编程】 python笔记--第一天

    [该随笔记录本人在阅读过程写的笔记和一些问题,格式比较随意,不定时更新] 由于该书使用的python版本为2.5,本人使用的为3.6. 第一章: 遇到的第一个问题: 1.3节 输出指令: print ...

  5. VC2010 CString.Format使用报错 error C2664

    error C2664: “void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)”: 不能将参数 1 ...

  6. 2017 NOIp 初赛体验

    很菜...我还是太蒟蒻了. d 老师太强了... 应该能有七十几分 初赛稳了 Update: 五十几分...

  7. 重新学习MySQL数据库11:以Java的视角来聊聊SQL注入

    本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...

  8. 架构师技能树skill-map

    # 架构师技能树 ## 系统架构能力 ### 基本理论- 扩展性设计- 可用性设计- 可靠性设计- 一致性设计- 负载均衡设计- 过载保护设计 ### 协议设计- 二进制协议- 文本协议 ### 接入 ...

  9. HTML5: HTML5 Web SQL 数据库

    ylbtech-HTML5: HTML5 Web SQL 数据库 1.返回顶部 1. HTML5 Web SQL 数据库 Web SQL 数据库 API 并不是 HTML5 规范的一部分,但是它是一个 ...

  10. cs224d 作业 problem set2 (一) 用tensorflow纯手写实现sofmax 函数,线性判别分析,命名实体识别

    Hi Dear Today we will use tensorflow to implement the softmax regression and linear classifier algor ...