一个月没写过博客了,一直想记录一下之前学习的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. Log4Net讲解

    声明:本文内容主要译自Nauman Leghari的Using log4net,亦加入了个人的一点心得(节3.1.4). 1           简介 1.1          Log4net的优点: ...

  2. arcgis的炸开多边形功能

    有时候我们使用dissolve工具,或其他操作会将空间不相连的多边形对应的属性合并到一起,如图: 在高级编辑工具中: 有这样一个工具,但是它能满足我的要求,但是他不是批量的,不过它使用起来比较方便. ...

  3. 2018牛客多校第三场 C.Shuffle Cards

    题意: 给出一段序列,每次将从第p个数开始的s个数移到最前面.求最终的序列是什么. 题解: Splay翻转模板题.存下板子. #include <bits/stdc++.h> using ...

  4. MySQL触发器写法

    触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/before) 4.触发事件(insert/update/dele ...

  5. POJ1062:昂贵的聘礼(dfs)

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 58108   Accepted: 17536 题目链接:http ...

  6. POJ3020:Antenna Placement(二分图匹配)

    Antnna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11093   Accepted: 5459 ...

  7. java禁止实例化的工具类

    public class Q { /** * @param args */ public static void main(String[] args) { new Person() } } clas ...

  8. 开发中常遇到的Python陷阱和注意点

    最近使用Python的过程中遇到了一些坑,例如用datetime.datetime.now()这个可变对象作为函数的默认参数,模块循环依赖等等. 在此记录一下,方便以后查询和补充. 避免可变对象作为默 ...

  9. Oracle查询字段内容为非数字的记录

    今天在一张3W多记录的表里查非数字的异常数据~数据库太水,记录一发,因为2.5使用人员误输入为2..5.... select t.routecardlist_id,trim(translate(RTR ...

  10. Windows Time Client

    Timezone: UTC Coordinated Universal Time ====Perform by Local / administrator must,configure Time se ...