Spring Data Redis(九)

Spring Data Redis

一、 Spring Data Redis 简介

Spring Data Redis与 Spring Data家族的其他数据操作如:Spring Data JDBC,以类似的结构进行封装,降低用户的学习成本;只要使用过其中一种Spring Data技术,对其他的数据源操作能依葫芦画瓢的进行。

二、 Redis 安装

1 安装环境

Redis 版本:3.0.0
环境:Linux

2 安装步骤

安装 gcc 编译器:
yum install gcc-c++

解压安装包:
tar -zxf redis-3.0.0.tar.gz

进入解压目录进行编译:
cd redis-3.0.0
make

将 Redis 安装到指定目录:
make PREFIX=/usr/local/redis install

启动 Redis,前置启动:
默认的是前置启动:./redis-server

启动 Redis,后置启动:
先将 redis.conf 文件拷贝到 redis 的安装目录
cp redis.conf /usr/local/redis/bin
编辑 redis.conf 文件修改:daemonize yes
启动: ./redis-server redis.conf
查看 redis 进程:ps aux|grep redis
关闭后置启动的 Redis:./redis-cli shutdown

三、 搭建整合环境

1 创建项目

在这里插入图片描述

2 整合配置

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	<!-- 配置读取properties文件的工具类 -->
	<context:property-placeholder location="classpath:redis.properties"/>
	
	<!-- Jedis连接池 -->
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.pool.maxTotal}"/>
		<property name="maxIdle" value="${redis.pool.maxIdle}"/>
		<property name="minIdle" value="${redis.pool.minIdle}"/>
	</bean>
	<!-- Jedis的连接工厂 -->
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="${redis.conn.hostName}"/>
		<property name="port" value="${redis.conn.port}"/>
		<property name="poolConfig" ref="poolConfig"/>
	</bean>
	<!-- Redis模板对象 -->
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory"/>
		<!-- 序列化器:能够把我们储存的key与value做序列化处理的对象 -->
		<!-- 配置默认的序列化器 -->
		<!-- keySerializer、valueSerializer 配置Redis中的String类型key与value的序列化器 -->
		<!-- HashKeySerializer、HashValueSerializer 配置Redis中的Hash类型key与value的序列化器 -->
		<property name="keySerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
		</property>
		<property name="valueSerializer">
			<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
		</property>
	</bean>
</beans>

3 测试整合环境

在这里插入图片描述
关闭 linux 防火墙,或者在防火墙中开启 6379 端口

4 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class RedisTest {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	/**
	 * 添加键值对
	 */
	@Test
	public void test1(){
		this.redisTemplate.opsForValue().set("key", "test");
	}
	
	/**
	 * 获取redis中的数据
	 */
	@Test
	public void test2(){
		String str = (String)this.redisTemplate.opsForValue().get("key");
		System.out.println(str);
	}
}

四、 Spring Data Redisd 存储实体对象


	/**
	 * 添加Users
	 */
	@Test
	public void test3(){
		Users users = new Users();
		users.setAge(30);
		users.setId(1);
		users.setName("张三");
		//更换序列化器
		this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
		this.redisTemplate.opsForValue().set("users", users);
	}
	
	/**
	 * 获取Users
	 * 
	 */
	@Test
	public void test4(){
		//更换序列化器
		this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
		Users users = (Users)this.redisTemplate.opsForValue().get("users");
		System.out.println(users);
	}

五、 Spring Data Redis 以 JSON 的格式存储实体对象

导入 jackson 包

/**
	 * 添加Users JSON格式
	 */
	@Test
	public void test5(){
		Users users = new Users();
		users.setAge(23);
		users.setId(2);
		users.setName("李四");
		
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
		this.redisTemplate.opsForValue().set("usersjson", users);
	}
	
	/**
	 * 获取Uesrs JSON格式
	 */
	@Test
	public void test6(){
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
		Users users = (Users)this.redisTemplate.opsForValue().get("usersjson");
		System.out.println(users);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老徐··

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值