SpringDataRedis環境搭建(詳細圖文教程)


場景

Centos中Redis的下載編譯與安裝(超詳細):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103967334

Redis的啟動和關閉(前台啟動和后台啟動):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103972348

RedisDesktopManager客戶端可視化工具下載安裝與使用:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103983147

通過以上教程將Redis的環境搭建起來后,使用SpringDataRedis在Java中對Redis進行操作。

SpringDataRedis

SpringDataRedis是spring大家族中的一部分,提供了在spring應用中通過簡單的配置訪問redis服務,對redis底層開發包(Jedis,JRedis,andRJC)進行了高度封裝,RedisTemplate提供了redis各種操作,異常處理及序列化,支持發布訂閱

SpringDataRedis針對Jedis提供了如下功能:

1.連接池自動管理,提供了一個高度封裝的RedisTemplate類

2.針對Jedis客戶端中大量api進行了歸類封裝,將同一類型操作封裝為operation接口

ValueIoerations:簡單K-V操作

SetIOperations:set類型數據操作.

ZSetOperations:zset類型數據操作

HashOperations:針對map類型的數據操作

ListOperations:針對list類型的數據操作.

實現

打開IDEA新建project-Maven Project

 

 

依次輸入坐標,建成后目錄

 

 

然后打開pom.xml,添加spring和jedis以及junit的依賴

 

<?xml version="1.0" encoding="UTF-8"?>
<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>com.badao.redis</groupId>
    <artifactId>springDataRedis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- 緩存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>
    </dependencies>
        <build>
            <plugins>
                <!-- java編譯插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
</project>

 

然后在src/main/resources下創建properties文件夾,然后新建文件redis-config.properties

 

 

 

 

在此屬性文件中配置redis連接的ip和端口等信息

 

# Redis settings
# server IP
redis.host=192.168.40.133
# server port
redis.port=6379
# server pass
redis.pass=
# use dbIndex
redis.database=0
# 最大空閑數
redis.maxIdle=300
#連接時的最大等待數
redis.maxWait=3000
#在提取一個jedis實例時,是否提前進行驗證操作;如果為true,則得到的jedis實例均是可用的;
redis.testOnBorrow=true

 

同理再在src/main/resources下創建spring文件夾,然后新建文件applicationContext-redis.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/cache
            http://www.springframework.org/schema/cache/spring-cache.xsd
            http://www.springframework.org/schema/mvc ">

    <context:property-placeholder location="classpath*:properties/*.properties"/>

    <!-- redis 相關配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    </bean>

    <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="JedisConnectionFactory"/>
    </bean>

</beans>

 

然后在src/main下新建包,包中新建類,開啟Redis服務端后進行測試存取值

package com.badao.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-redis.xml")
public class SpringDataRedisTest {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue(){
        redisTemplate.boundValueOps("name").set("badao");
    }
    @Test
    public void getValue(){
        String str = (String) redisTemplate.boundValueOps("name").get();
        System.out.println(str);
    }

}

 

運行單元測試方法,先存值再取值結果

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM