一个月没写过博客了,一直想记录一下之前学习的Redis的有关知识,但是因为四月太过于慵懒和忙碌,所以一直没有什么机会,今天就来讲讲,如何使用Spring当中的Spring-data-redis去与Redis这个Nosql数据库集成吧。

  首先先简单讲讲我理解的Nosql数据库吧,如果存在错误,还请一定联系本人指出,因为自己也是摸索阶段当中,希望能有人多多进行交流。所谓的Nosql中文全称为:非关系型数据库,即它不像Mysql那样关系型数据库,它存储的内容之间,可以是没有关联关系的。Mysql一张表存储的东西,就必须是属于这一张表的实例,且结构和字段是在表设计之初就设定好的,而Nosql的“表”(其实Nosql当中没有表这个概念)是可以存储各种各样的东西的,可以是一个链表,可以是一个hashmap,或者是其他形式的集合。而在Nosql当中的Redis,是一种基于内存的Nosql数据库,即其在启动的时候,可以把所有redis存储的东西都加载到内存当中,它是以Key-value的形式进行存储的,并且查询也是通过其Key进行的。它解决了大规模数据集合多重数据种类带来的挑战,基于内存的Nosql在查询方面相比传统的关系型数据库,更是它的一大优势。

  对Redis大概有了一定的了解和定位之后,接下来我们进入正题,在本文当中,主要讲解的是通过Spring-Date-Redis(SDR)来对Redis进行一些增删改查的操作,其中包括普通的字符串的增删改查,以及自定义对象的增删改查,即基于Spring的Redis Nosql数据库的Dao层实现是本文要讲解的核心内容

  在讲解之前,首先我们要搭建好我们的工程,在这里与之前不同,本人这次采用的是maven工程进行工程搭建,在maven工程里头,可以通过对工程当中的pom.xml引入依赖,而对所依赖的jar包进行注入,包括自动从maven的主仓库下载到本地仓库当中,并且自动在工程当中建立好相应jar包的依赖路径,开发者不在需要关注所使用的jar在哪下载,并且有没有下载完全等问题,十分便利。这里不再对maven工程的使用进行赘述了,如果有不会的同学,可以联系本人或者自行查阅java maven工程的使用,再或者通过直接手动下载所需要的jar包依赖,手动引入jar建立路径的方式进行工程搭建也可以。而使用的测试框架,是junit 4.12进行的,该框架可对无参数的函数进行单元测试,这里也不再对该框架进行过多的介绍了,不了解的同学,可以自行查阅。由于本人是基于maven工程的,这里直接上依赖的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/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>wellhold.bjtu</groupId>
<artifactId>Spring_redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>Spring_redis</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.5.RELEASE</spring.version>
</properties> <dependencies> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <!--spring单元测试依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency> <!-- Redis 相关依赖 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency> <!-- annotation依赖 -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency> </dependencies>
</project>

配置好依赖的jar包之后,Redis和mysql一样,也需要对数据库的相关参数进行配置,包括连接的主机地址,端口号等参数。我们通过编写一个.properties文件来进行相关参数的配置,redis.properties如下:

#redis中心
#绑定的主机地址
redis.host=127.0.0.1
#指定Redis监听端口,默认端口为6379
redis.port=6379
#授权密码(可以不使用)
redis.password=bjtu
#最大空闲数:空闲链接数大于maxIdle时,将进行回收
redis.maxIdle=100
#最大连接数:能够同时建立的“最大链接个数”
redis.maxTotal=200
#最大等待时间:单位ms
redis.maxWait=1000
#使用连接时,检测连接是否成功
redis.testOnBorrow=false
#当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
redis.timeout=10000

之后就是对我们的Spring进行配置了,通过我们的beans.xml进行配置,如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 自动扫描注解的bean -->
<context:component-scan base-package="wellhold.bjtu.Spring_redis" />
<context:annotation-config /> <!-- 读取redis.properties -->
<context:property-placeholder location="classpath:redis.properties"/> <!-- jedis连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="blockWhenExhausted" value="true" />
</bean> <!-- jedis连接工程的配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="password" value="${redis.password}" />
<property name="usePool" value="true"/>
<property name="timeout" value="${redis.timeout}"></property>
</bean> <!-- redisTemplate配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean> </beans>

简单的说一下在这个beans.xml当中的一些内容,首先,什么是jedis?从名字可以看出来jedis可以看成是java to redis的简写,是一个别人封装好的java与redis联用的jar包,可以利用别人封装好的jedis直接与redis联通,而jedis pool即是通过连接池的方式进行连接,在这里可以简单的看做是C3P0与Mysql之间的关系。而Spring-data-Redis,则是在Jedis再上一层的封装,这一层封装使得Spring可以直接与Jedis集成,且可通过Spring里头的RedisTemplate对象对Redis进行操作,使得用户操作起来更加简便。而在Serializer则是序列化类,是可以讲对象进行序列化的工具类,所谓的序列化就是将一个对象转换为二进制的数据流,这样就可以进行传输,或者保存到文件中。如果一个类的对象要想实现序列化,就必须实现serializable接口,在此接口中没有任何的方法,此接口只是作为一个标识,表示本类的对象具备了序列化的能力而已。

  完成工程各个框架和组件的配置之后,我们开始进行逻辑业务的实现。

Spring:与Redis的集成的更多相关文章

  1. spring与redis集成之aop整合方案

    java使用redis缓存可以使用jedis框架,jedis操作简单,没有什么复杂的东西需要学习,网上资料很多,随便看看就会了. 将spring与redis缓存集成,其实也是使用jedis框架,只不过 ...

  2. spring+redis的集成,redis做缓存

    1.前言 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.我们都知道,在日常的应用中,数据库瓶颈是最容易出现的 ...

  3. spring boot通过Spring Data Redis集成redis

    在spring boot中,默认集成的redis是Spring Data Redis,Spring Data Redis针对redis提供了非常方便的操作模版RedisTemplate idea中新建 ...

  4. 7、Spring Boot 2.x 集成 Redis

    1.7 Spring Boot 2.x 集成 Redis 简介 继续上篇的MyBatis操作,详细介绍在Spring Boot中使用RedisCacheManager作为缓存管理器,集成业务于一体. ...

  5. Spring Boot Redis 集成配置(转)

    Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...

  6. spring boot redis 缓存(cache)集成

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  7. Spring Boot 如何快速集成 Redis 哨兵?

    上一篇:Spring Boot 如何快速集成 Redis? 前面的分享栈长介绍了如何使用 Spring Boot 快速集成 Redis,上一篇是单机版,也有粉丝留言说有没有 Redis Sentine ...

  8. spring+redis的集成,使用spring-data-redis来集成

    1.参考:https://www.cnblogs.com/qlqwjy/p/8562703.html 2.首先创建一个maven项目.然后加入依赖的jar包就行了.我加入的jar包很多,反正加入了也没 ...

  9. Shiro 集成Spring 使用 redis时 使用redisTemplate替代jedisPool(五)

    1.添加依赖架包: <dependency> <groupId>org.springframework.data</groupId> <artifactId& ...

随机推荐

  1. LeetCode -- Best Time to Buy and Sell Stock系列

    Question: Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pri ...

  2. 【电影影评】梦之安魂曲-败给了BGM和豆瓣影评

    首先,这部电影豆瓣8.7分,一般来说,豆瓣的打分是比较准确的.能反映一个片子的质量,而较少受到环境的影响.但是这种关系当然也不全对,比如某些片子可能特别让某一种人喜欢(如退役军人和军旅题材),而在某些 ...

  3. 【题解】NOI2015寿司晚宴

    想好久啊+不敢写啊……但果然人还是应当勇敢自信,只有坚定地去尝试,才会知道最后的结果.1A真的太开心啦,不过好像我的做法还是比较复杂的样子……理解起来应该算是比较容易好懂的类型,大家可以参考一下思路~ ...

  4. 【题解】SDOI2014数数

    真的很开心呢,总算是有一道完完全全由自己做出来的题目啦~ 这一道题目洛谷P3311和另一道JSOI文本生成器的题目是十分相像的,dp方面几乎相同.只是<=n的约束,让这道题目必须结合数位dp的方 ...

  5. Android 异步通信:图文详解Handler机制工作原理

    前言 在Android开发的多线程应用场景中,Handler机制十分常用 今天,我将图文详解 Handler机制 的工作原理,希望你们会喜欢 目录 1. 定义 一套 Android 消息传递机制 2. ...

  6. 洛谷 P2168 [NOI2015]荷马史诗 解题报告

    P2168 [NOI2015]荷马史诗 题目描述 追逐影子的人,自己就是影子 --荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷 ...

  7. 【NOIP模拟赛】与非 乱搞

    biubiu~~~ 正解是线段树维护真值表,但是我觉得对于这道题来说乱搞就够了....... 我们发现如果我们把每一个数都一开始取反就会发现对于最后结果来说 x=x^1,x nand x=x|x ,x ...

  8. 洛谷P1522 牛的旅行 Cow Tours

    ---恢复内容开始--- P1522 牛的旅行 Cow Tours189通过502提交题目提供者该用户不存在标签 图论 USACO难度 提高+/省选-提交该题 讨论 题解 记录 最新讨论 输出格式题目 ...

  9. POJ1637:Sightseeing tour(混合图的欧拉回路)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10581   Accepted: 4466 ...

  10. APP兼容性测试

    一.APP兼容性范围以及问题 1.硬件 各个硬件结构 2.软硬件之间 硬件dll库(C++) 软硬件之间的通信,各个厂商提供的ROM 3.软件 浏览器.操作系统.数据库.手机.功能兼容性(功能修改,二 ...