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

相關文章
相關標籤/搜索