Spring整合Redis

依赖:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.8.4.RELEASE</version>
</dependency>

xml文件配置 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 加载配置文件 -->
    <!--<context:property-placeholder location="classpath:redis.properties" />-->

    <!--
    注意:SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。
    redis 序列化策略 ,通常情况下key值采用String序列化策略,
    如果不指定序列化策略,StringRedisTemplate的key和value都将采用String序列化策略。
    但是RedisTemplate的key和value都将采用JDK序列化,这样就会出现采用不同template保存的数据序列化方式不一致出现问题 -->

    <!--redisTemplate 模板,访问redis数据库-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer" ref="stringRedisSerializer" />
        <property name="valueSerializer" ref="stringRedisSerializer"/>
        <property name="hashKeySerializer" ref="stringRedisSerializer" />
        <property name="hashValueSerializer" ref="stringRedisSerializer" />
        <!--开启事务  -->
        <property name="enableTransactionSupport" value="true"></property>
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="47.112.11.147"></property>
        <property name="port" value="6379"></property>
        <property name="password" value="123456"></property>
        <property name="poolConfig" ref="poolConfig"></property>
        <property name="usePool" value="true"></property>
        <property name="timeout" value="6000"></property>
    </bean>
    <!--使用p标签的方式-->
    <bean id="jedisConnectionFactory1"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:usePool="true" p:hostName="47.112.11.147" p:port="6379"
          p:timeout="6000" p:password="123456" p:poolConfig-ref="poolConfig"/>

    <!-- redis连接池配置相关配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲数 -->
        <property name="maxIdle" value="10"/>
        <!-- 最大空连接数 默认是8-->
        <property name="maxTotal" value="1024" />
        <!-- 最大等待时间 ,获取连接的最大等待毫秒数。如果设为小于0,则永远等待-->
        <property name="maxWaitMillis" value="-1" />
        <!-- 连接超时时是否阻塞,false时报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="true"/>
        <!-- 返回连接时,检测连接是否成功 -->
        <property name="testOnBorrow" value="true" />
    </bean>
    <bean id="stringRedisSerializer"  class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</beans>

代码:

package com.sdr;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestSDR {

    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:application.xml");
    RedisTemplate redisTemplate = applicationContext.getBean("redisTemplate", RedisTemplate.class);

    /**
     * 保存string
     */
    @Test
    public void testString1(){
        //在xml文件中已经配置了
        //redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.boundValueOps("name").set("zhangsan");
        System.out.println( redisTemplate.boundValueOps("name").get() );
    }
    @Test
    public void testString2(){
        //获取操作key value数据类型的操作对象
        ValueOperations<Object, Object> opsForValue = redisTemplate.opsForValue();
        opsForValue.set("name","ww");
        //获取指定key的值
        String name = (String) opsForValue.get("name");
        System.out.println( name );
    }

    /**
     * 保存列表
     */
    @Test
    public void setList(){
        redisTemplate.boundListOps("list").leftPush("cjh1");
        redisTemplate.boundListOps("list").leftPush("cjh2");
        redisTemplate.boundListOps("list").rightPush("cjh3");

        List<String> list = redisTemplate.boundListOps("list").range(0, -1);
        list.stream().forEach(item->{
            System.out.println(item);
        });
        redisTemplate.delete("list");
    }
    /**
     * 保存哈希
     */
    @Test
    public void setHash(){

        Map<String, Object> data = new HashMap<>();
        data.put("key1","ccc");
        data.put("key2","bbb");
        redisTemplate.boundHashOps("hash").putAll(data);

        Map<Object, Object> hash = redisTemplate.boundHashOps("hash").entries();
        for(Map.Entry item:hash.entrySet()){
            System.out.println(item.getKey()+":"+item.getValue());
        }

        redisTemplate.delete("hash");
    }

    /**
     * 保存有序集合
     */
    @Test
    public void setSortedSet(){

        redisTemplate.boundZSetOps("rank").add("name1",90.0);
        redisTemplate.boundZSetOps("rank").add("name2",100.0);
        redisTemplate.boundZSetOps("rank").add("name3",70.0);

        Set<String> rank = redisTemplate.boundZSetOps("rank").reverseRange(0, -1);
        rank.stream().forEach(item->{
            System.out.println(item);
        });

        redisTemplate.delete("rank");
    }

}

参考:java代码操作Redis数据库

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值