前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring全家桶之SpringData——SpringData Redis(附相关jar包)

Spring全家桶之SpringData——SpringData Redis(附相关jar包)

作者头像
时间静止不是简史
发布2020-07-27 10:40:55
4780
发布2020-07-27 10:40:55
举报
文章被收录于专栏:Java探索之路

介绍

简介

SpringDataRedis是Spring大家族中的一个成员,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。

安装

Redis单机版安装

整合环境搭建

创建项目

创建一个JavaProject ,导入如下jar

代码语言:javascript
复制
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
commons-logging-1.1.1.jar
commons-pool2-2.3.jar
jedis-2.7.2.jar
spring-aop-4.2.0.RELEASE.jar
spring-aspects-4.2.0.RELEASE.jar
spring-beans-4.2.0.RELEASE.jar
spring-context-4.2.0.RELEASE.jar
spring-core-4.2.0.RELEASE.jar
spring-data-redis-1.6.0.RELEASE.jar
spring-expression-4.2.0.RELEASE.jar
spring-test-4.2.0.RELEASE.jar
spring-tx-4.2.0.RELEASE.jar
jackson-annotations-2.8.0.jar
jackson-core-2.8.10.jar
jackson-databind-2.8.10.jar

redis配置文件

创建redis配置文件 applicationContext.xml , 添加如下内容

代码语言:javascript
复制
<?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>

redis连接参数配置文件

redis.properties

代码语言:javascript
复制
redis.pool.maxTotal=20
redis.pool.maxIdle=10
redis.pool.minIdle=5
#redis所在虚拟机ip。端口
redis.conn.hostName=192.168.179.131
redis.conn.port=6379

注 :这两个配置文件都在src目录下

实体类

需要实现序列化 ,因为要以json类型以字节的形式传输到redis所在虚拟机

代码语言:javascript
复制
package ah.szxy.pojo;

import java.io.Serializable;

public class Users implements Serializable{
	
	private Integer id;
	private String name;
	private Integer age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Users [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
}

测试类

存储对象时 ,更换JdkSerializationRedisSerializer序列化器 用于将对象以字节的形式存储到redis数据库中 . 序列化后的数据比使用工具包更占用redis内存 序列化和反序列化需要用同一个序列化器

代码语言:javascript
复制
package ah.szxy.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.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ah.szxy.pojo.Users;


/**
 * Redis测试
 * 
 * @author Administrator
 *
 */
@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);
	}

	/**
	 * 添加Users
	 * 
	 * 存储对象时 ,更换JdkSerializationRedisSerializer序列化器 用于将对象以字节的形式存储到redis数据库中
	 * 序列化后的数据比使用工具包更占用redis内存 序列化和反序列化需要用同一个序列化器
	 */
	@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);
	}

	/**
	 * 添加Users json格式
	 */
	@Test
	public void test5() {
		Users users = new Users();
		users.setAge(24);
		users.setId(2);
		users.setName("间桐樱");
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
		this.redisTemplate.opsForValue().set("UsersJson", users);
	}
	
	/**
	 * 获取Users json格式
	 */
	@Test
	public void test6() {
	
		this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));
		Users users = (Users) this.redisTemplate.opsForValue().get("UsersJson");
		System.out.println(users);
	}

	
}

引用RedisTemplate后 ,可以设置的序列化器如下

重要的几个功能如下

注 : 本例中使用的 Jackson2JsonRedisSerializer序列化器和 JacksonJsonRedisSerializer序列化器功能一样

该项目源码分享

链接:https://pan.baidu.com/s/1G_pxYPvCUmvcFToZOwwoYw

提取码:wqxb

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 安装
  • 整合环境搭建
    • 创建项目
      • redis配置文件
        • redis连接参数配置文件
          • 实体类
            • 测试类
            • 该项目源码分享
            相关产品与服务
            文件存储
            文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档