springDataRedis的运用

springDataRedis对redis的java客户端jedis进行了封装.使用windows系统reids服务端进行测试.
1.工程目录:
在这里插入图片描述
2.pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.study</groupId>
  <artifactId>xindanding</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>xindanding</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <!-- 集中定义依赖版本号 -->
  <properties>
    <spring.version>5.1.5.RELEASE</spring.version>
  </properties>

  <dependencies>
    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <!-- 缓存 -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.8.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.7.2.RELEASE</version>
    </dependency>


  </dependencies>
</project>

3.redis-config.properties

# Redis settings 
# server IP 
redis.host=127.0.0.1 
# server port 
redis.port=6379 
# server pass 
redis.pass= 
# use dbIndex 
redis.database=0 
redis.maxIdle=300 
redis.maxWait=3000 
redis.testOnBorrow=true 

4.applicationContext-redis.properties

<?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:p="http://www.springframework.org/schema/p" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:cache="http://www.springframework.org/schema/cache"
  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/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/cache  
            http://www.springframework.org/schema/cache/spring-cache.xsd">  
  
   <context:property-placeholder location="classpath*:properties/*.properties" />   
  
   <!-- redis 相关配置 --> 
   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
     <property name="maxIdle" value="${redis.maxIdle}" />   
     <property name="maxWaitMillis" value="${redis.maxWait}" />  
     <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
   </bean>  
  
   <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
       p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>  
   
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
    	<property name="connectionFactory" ref="JedisConnectionFactory" />  
   </bean>  
      
</beans>  

5.TestRedis测试类

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Set;

@RunWith(SpringJUnit4ClassRunner.class)     //5.1.5.REALEASE中
@ContextConfiguration(locations = "classpath:/spring/applicationContext-redis.xml")
public class TestRedis {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 测试String类型
     */
    @Test
    public void testString(){
       BoundValueOperations boundValueOperations =  redisTemplate.boundValueOps("company");
       boundValueOperations.set("alibaba");
       System.out.println(boundValueOperations.get());  //alibaba
        redisTemplate.delete("company");
        System.out.println(boundValueOperations.get());  //null
    }

    /**
     * 测试 Set类型
     */
    @Test
    public void testSet(){
        BoundSetOperations boundSetOperations = redisTemplate.boundSetOps("city");
        boundSetOperations.add("上海");
        boundSetOperations.add("北京");
        boundSetOperations.add("深圳");

        Set set = boundSetOperations.members();   //获取所有元素
        System.out.println(set);   //[北京, 深圳, 上海]
        boundSetOperations.remove("上海");    //  删除元素
        System.out.println(boundSetOperations.members()); //[北京, 深圳]
        redisTemplate.delete("city");   //删除集合city
        System.out.println(boundSetOperations.members());   //[]
    }


    /**
     * 测试ZSet    有序
     */
    @Test
    public void testZSet(){
        BoundZSetOperations<String, String> boundZSetOperations = redisTemplate.boundZSetOps("city");
        boundZSetOperations.add("五行", 1);
        boundZSetOperations.add("金", 0);
        System.out.println(boundZSetOperations.getKey());  //city
        System.out.println(boundZSetOperations.range(0, 1));  // [金, 五行]
        boundZSetOperations.remove("五行");
        System.out.println(boundZSetOperations.range(1,4));   //[金]   从顺序1取到4
        redisTemplate.delete("city");
        System.out.println(boundZSetOperations.range(0, 3)); //[]
    }


    /**
     * 测试List  可以有重复数据
     */
    @Test
    public void testList(){

        redisTemplate.delete("city");
        BoundListOperations boundListOperations = redisTemplate.boundListOps("city");
        boundListOperations.leftPush("上海");
        boundListOperations.leftPush("武汉");
        boundListOperations.leftPush("乌鲁木齐");
        System.out.println(boundListOperations.range(0,10));  //[乌鲁木齐, 武汉, 上海]   类似栈结构


        boundListOperations.rightPush("深圳");
        boundListOperations.rightPush("澳洲火灾");
        boundListOperations.rightPush("ETC收费");
        System.out.println(boundListOperations.range(0, 10)); //[深圳, 澳洲火灾, ETC收费]    类似队列

        boundListOperations.rightPop();   //右删除
        System.out.println(boundListOperations.range(0, 10)); //[深圳, 澳洲火灾]

        boundListOperations.leftPop();   //左删除
        System.out.println(boundListOperations.range(0, 10));  //[澳洲火灾]

        redisTemplate.delete("city");
        BoundListOperations boundListOperations1 = redisTemplate.boundListOps("city");
        boundListOperations1.rightPush("深圳");
        boundListOperations1.rightPush("澳洲火灾");
        boundListOperations1.rightPush("ETC收费");
        boundListOperations1.rightPush("深圳");
        boundListOperations1.rightPush("澳洲火灾");
        boundListOperations1.rightPush("ETC收费");
        boundListOperations1.remove(2, "深圳");   //删除两个深圳
        System.out.println(boundListOperations.range(0, 10)); //[澳洲火灾, ETC收费, 澳洲火灾, ETC收费]
        boundListOperations.remove(1, "澳洲火灾");
        System.out.println(boundListOperations.range(0, 10)); //[ETC收费, 澳洲火灾, ETC收费]
    }


    /**
     * 测试Hash   存储的是 map
     */
    @Test
    public void testHash(){
        redisTemplate.delete("city");
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps("city");
        boundHashOperations.put("s", "上海");
        boundHashOperations.put("c", "长沙");
        boundHashOperations.put("b", "北京");
        System.out.println(boundHashOperations.get("s")); //上海
        System.out.println(boundHashOperations.keys());        //[s, c, b]
        System.out.println(boundHashOperations.values());  //[上海, 长沙, 北京]
        boundHashOperations.delete("s");
        System.out.println(boundHashOperations.values());       //[长沙, 北京]
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值