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