自学SpringData--04SpringData Redis

4 篇文章 0 订阅
2 篇文章 0 订阅

SpringData Redis

在这里插入图片描述
Spring Data Redis是Spring Data大家庭中的一部分,为redis到spring的一个应用提供了一个简单的配置和访问。并且提供了一个低版本和高版本的抽象来跟存储作交互,让用户很方便的去解决一些基础或者初级性的问题。

Redis安装

  • 1.# yum install -y gcc-c++
    由于是 c 语言编写,所以需要安装支持组件
  • 2.把压缩包上传到 linux 服务器上
    示例位置: /usr/local/tmp/ 下
  • 3.# cd /usr/local/tmp
    tar zxvf redis-3.0.6.tar.gz
    进入到/usr/local/tmp 下 运行解压命令
  • 4.# make
    进入到解压后的目录编译
  • 5.# make install PREFIX=/usr/local/redis
    安装,设置安装路径为/usr/local/redis 下
    进入到 src 下安装
  • 6.# ./redis-server
    前端启动,安装后不能进行其他操作
    Ctrl+c 退出
    命令要在 bin 目录下执行
  • 7.# cp /usr/local/tmp/redis-3.0.0/redis.conf /usr/local/redis/bin
    把解压目录下配置文件拷贝到安装目录的 bin 下
  • 8.# vi redis.conf
    修改 bin 下 redis.conf
    把 daemonize 由 no 修改成 yes,守护进程启动
  • 9.# ps aux|grep redis
    查看 redis 启动情况
  • 10…/redis-server redis.conf
    启动 redis 服务
  • 11.# ./redis-cli shutdown
    如果希望关闭,运行上面命令,不关闭不运行即可
  • 12.# ./redis-cli
    进入到自带客户端工具,测试 redis 是否可用
  • 13 # set name ‘smallming’
    添加一个 string ,key 为 name,value 为 smallming
  • 14# get name
    取出 name 中内容
    在这里插入图片描述
    在这里插入图片描述

Spring整合Spring Data Redis

创建项目

在这里插入图片描述

导入jar包

右键 Build Path
在这里插入图片描述

applicationContext.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:context="http://www.springframework.org/schema/context"
	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">
	<!-- 配置读取properties文件的工具类 -->
	<context:property-placeholder location="classpath:redis.properties"/>
	
	<!-- Jedis连接池 -->
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.pool.maxtTotal}"/>
		<property name="maxIdle" value="${redis.pool.maxtIdle}"/>
		<property name="minIdle" value="${redis.pool.minIdle}"/>
	</bean>
	
	<!-- Jedis连接工厂:创建Jedis对象的工厂 -->
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<!-- IP地址 -->
		<property name="hostName" value="${redis.hostname}"/>
		<!-- 端口 -->
		<property name="port" value="${redis.port}"/>
		<!-- 连接池 -->
		<property name="poolConfig" ref="poolConfig"/>
	</bean>
	
	<!-- Redis模板对象:是SpringDataRedis提供的用户操作Redis的对象 -->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
	<!-- 默认的序列化器:序列化器就是根据规则将存储的数据中的key与value做字符串的序列化处理 -->
	<!-- keySerializer、valueSerializer:对应的是Redis中的String类型 -->
	<!-- hashKeySerializer、hashValueSerializer:对应的是Redis中的Hash类型 -->
	<property name="keySerializer">
		<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
	</property>
	<property name="valueSerializer">
	    <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
	</property>
	</bean>
</beans>
redis.properties
jdbc.url=jdbc:mysql://localhost:3306/springData
jdbc.driver.class=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
测试整合环境

在这里插入图片描述
在这里插入图片描述
加入Junit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值