一個月沒寫過博客了,一直想記錄一下之前學習的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接口,在此接口中沒有任何的方法,此接口只是作為一個標識,表示本類的對象具備了序列化的能力而已。
完成工程各個框架和組件的配置之后,我們開始進行邏輯業務的實現。