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);
    }

}

运行单元测试方法,先存值再取值结果

原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/12531387.html

时间: 2024-08-01 06:34:05

SpringDataRedis环境搭建(详细图文教程)的相关文章

zookeeper集群环境搭建详细图文教程

zookeeper集群环境搭建详细图文教程 zhoubang @ 2018-01-02 [文档大纲] 友情介绍 软件环境 注意点 环境安装 1. 新建用于存储安装包以及软件安装的目录 2. 下载安装zookeeper 3. 解压zookeeper压缩包 单节点zookeeper配置 1. 配置zoo.cfg文件 2. 配置zookeeper的数据存储目录 3. 新建myid文件 4. 配置zookeeper集群节点 5. 启动zookeeper服务 6. 检查zookeeper服务是否成功启动

Android开发环境搭建(图文教程)

昨天又搭建了一次Android的开发环境,尝试了好几种方式,也遇到了一些问题,在此分享一下. 注意:官网公布的最新版本号的SDK和ADT(23.0.0),对于和Eclipse集成的开发环境是有BUG存在的.搭建完毕建立HelloWorld測试的时候,src和layout文件下的文件都为空.所以不妨下载之前版本号,我终于成功安装的就是使用了前一个版本号的SDK和ADT(22.6.2).环境搭建不包含主要的JDK的安装等,这个可自己百度. 以下介绍3种开发环境的搭建: 1.官网Android ADT

[转]Android开发环境搭建(图文教程)

转自:http://www.cnblogs.com/yxwkf/p/3853046.html 昨天又搭建了一次Android的开发环境,尝试了好几种方式,也遇到了一些问题,在此分享一下. 注意:官网公布的最新版本号的SDK和ADT(23.0.0),对于和Eclipse集成的开发环境是有BUG存在的.搭建完毕建立HelloWorld測试的时候,src和layout文件下的文件都为空.所以不妨下载之前版本号,我终于成功安装的就是使用了前一个版本号的SDK和ADT(22.6.2).环境搭建不包含主要的

windows下python+flask环境配置详细图文教程

本帖是本人在安装配置python和flask环境时所用到的资源下载及相关的教程进行了整理罗列,来方便后面的人员,省去搜索的时间.如果你在安装配置是存在问题可留言给我. 首先罗列一下python+flask环境所用的一些程序组件的下载地址: 1.python语言环境: http://www.python.org/download/ . 2.setuptools 组件: https://pypi.python.org/pypi/setuptools/0.9.6 . 3.pip 组件: https:/

SSH框架搭建 详细图文教程

转载请标明原文地址 一.什么是SSH? SSH是JavaEE中三种框架(Struts+Spring+Hibernate)的集成框架,是目前比较流行的一种Java Web开源框架. SSH主要用于Java Web的开发.现在SSH有SSH1和SSH2两种,区别在于Struts的版本是struts1.x还是2.x.本文介绍的是SSH1. 二.Struts Spring Hibernate各起什么作用? Struts:控制逻辑关系. Spring:解耦. Hibernate:操作数据库. 三.学习SS

阿里云Windows server 2008服务器搭建VPN 图文教程,购买境外服务器自建vpn,Win8/win10 连接VPN被阻止,出现812错误解决方法

阿里云Windows server 2008服务器搭建VPN 图文教程(超详细) 第一步:购买阿里云服务器,本文使用的是Windows Server 2008 R2 企业版64位中文版 IP地址:47.88.151.129,所属节点:亚太(新加坡) 服务器配置:2核,4GB,带宽10Mbps 第二步: 打开服务器管理器,点击添加角色,如下图: 本帖隐藏的内容然后弹出如下图所示,点击下一步: 点击后,如下图,勾选网络策略和网络服务,然后点击下一步: 接着继续点击下一步,直到弹出如下图所示的页面,勾

WiFi密码破解详细图文教程

每天都能看到有不少网友在回复论坛之前发布的一篇破解WiFi密码的帖子,并伴随各种疑问.今天流云就为大家准备一篇实战型的文章吧,详细图文从思维CDlinux U盘启动到中文设置,如何进行路由SSID扫描.WPA密码类型该如何破解.字典该怎样做(WEP加密的密码貌似可以直接破解不用字典)效果比BT8要强悍很多!这是一篇详细介绍WiFi密码破解的文章,准备好了吗? 好了,先说下提前要准备的东东吧:1.U盘一枚,最小1G空间.需进行格式化操作,提前保存内部文件.2.CDlinux镜像.帖子最后会提供一枚

Lamp环境的详细安装教程

原文:Lamp环境的详细安装教程 架构LAMP环境 1.布置LAMP环境之前的准备工作 在架构LAMP环境时,确保你的Linux系统已经安装了make.gcc.gcc-c++(使用rpm -q xxx 查看系统是否已经安装软件) 解压Lamp压缩包 下载地址:http://pan.baidu.com/s/1hq4hI5m 如果解麻烦的话,可以写一个自动解压脚本 1 cd /lamp #你解压的目录 2 3 ls *.tar.gz > ls.list 4 5 for tar in ‘cat ls.

Windows 2016 无域故障转移群集部署方法 超详细图文教程

转自:https://blog.csdn.net/demonson/article/details/81708809 Windows 2016 无域故障转移群集部署方法 超详细图文教程 故障转移群集是一个很实用的功能,而windows在2016版本开始,终于支持不用域做故障转移群集.在群集中,我们可以设定一个"群集IP"而客户端只需要根据这个"群集IP"就能连接当前群集的主服务器.而不必关心群集服务器之间的替换.而更棒的是,它是"去中心"的,它没