spring与redis整合(二)--采用spring-data-redis方式

本文详细介绍了如何使用Spring-Data-Redis库来整合Spring和Redis,涵盖了JedisPoolConfig配置、JedisConnectionFactory设置、四种序列化方式、RedisTemplate与StringRedisTemplate的配置以及操作方法,包括execute方法、各种封装操作类的使用,以及注解注入的方式。
摘要由CSDN通过智能技术生成

一、简介

在 spring与redis整合(一)中,介绍了采用原生的jedis方式,进行redis与spring的整合。

在本文,将介绍采用spring封装的redisTemple(spring-date-redis)方式,进行redis操作。

二、配置

1、jedis池配置

redis.clients.jedis.JedisPoolConfig,参数有:

a)maxTotal,池中最多可以有多少个jedis实例,默认值是8

b)maxIdle,池中最多有多少个空闲的jedis实例,默认值是8

c)minIdle,池中最小有多少个空闲的jedis实例,默认值是0

d)maxWaitMillis,获取jedis实例的最大等待毫秒数,默认值是-1

如:

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"
      p:maxTotal="6000"
      p:maxIdle="300"
      p:minIdle="100"
      p:maxWaitMillis="1500"
/>
2、JedisConnectionFactory配置

org.springframework.data.redis.connection.jedis.JedisConnectionFactory,参数有:

a) hostname,redis安装地址;

b)port,端口;

c)poolConfig,jedis的池配置;

如:

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
      p:hostName="127.0.0.1"
      p:port="6379"
      p:database="0"
      p:poolConfig-ref="jedisPoolConfig"

/>

3、序列化方式

spring data redis有4种序列化方式,

a)org.springframework.data.redis.serializer.StringRedisSerializer ,字符串方式序列化,即

<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />

b)org.springframework.data.redis.serializer.JdkSerializationRedisSerializer ,jdk默认的序列化方式,即

<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

c)Jackson2JsonRedisSerializer ,jackson的json序列化方式,需要指定序列化的类型。如

redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Student>(Student.class));
ValueOperations<String,Student> valOps = redisTemplate.opsForValue();
valOps.set("ss2",new Student(2));
Student st = valOps.get("s2");

d)OxmSerializer, xml方式进行序列化,如

Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
Map<String,Object> properties = new HashMap<String, Object>();
properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);   //设置缩进
properties.put(Marshaller.JAXB_ENCODING,"utf-8");   //设置编码
jaxb2Marshaller.setMarshallerProperties(properties);//设置Marshaller属性
jaxb2Marshaller.setClassesToBeBound(Student.class);

redisTemplate.setValueSerializer(new OxmSerializer(jaxb2Marshaller,jaxb2Marshaller));
ValueOperations<String,Student> stuValOps = redisTemplate.opsForValue();
stuValOps.set("xs2",new Student(4));
Student xs2 = stuValOps.get("xs2");
System.out.println(xs2);

4、RedisTemplate配置

org.springframework.data.redis.core.RedisTemplate,redisTemplate是我们操作redis的入口之一,内有参数:

a)connectionFactory,jedis的连接工厂;

b)keySerializer, key的序列化方式;

c)valueSerializer,value的序列化方式;

d)hash

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值