Spring:与Redis的集成

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

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

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

  在讲解以前,首先咱们要搭建好咱们的工程,在这里与以前不一样,本人此次采用的是maven工程进行工程搭建,在maven工程里头,能够经过对工程当中的pom.xml引入依赖,而对所依赖的jar包进行注入,包括自动从maven的主仓库下载到本地仓库当中,而且自动在工程当中创建好相应jar包的依赖路径,开发者不在须要关注所使用的jar在哪下载,而且有没有下载彻底等问题,十分便利。这里再也不对maven工程的使用进行赘述了,若是有不会的同窗,能够联系本人或者自行查阅java maven工程的使用,再或者经过直接手动下载所须要的jar包依赖,手动引入jar创建路径的方式进行工程搭建也能够。而使用的测试框架,是junit 4.12进行的,该框架可对无参数的函数进行单元测试,这里也再也不对该框架进行过多的介绍了,不了解的同窗,能够自行查阅。因为本人是基于maven工程的,这里直接上依赖的pom.xml了。redis

<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以下:spring

#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进行配置,以下:sql

<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接口,在此接口中没有任何的方法,此接口只是做为一个标识,表示本类的对象具有了序列化的能力而已。数据库

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

相关文章
相关标签/搜索