搭建SpringDataRedis

SpringDataRedis:提供了在spring应用中通过简单的配置访问redis的服务,对redis底层开发包(Jedis,JRedis,andRJC)进行了高度封装,RedisTemplate提供了redis各种操作,异常处理及序列化,支持发布订阅

环境

操作系统: Win10
IDE:Eclipse
构建工具:Maven
JDK:1.8
Redis服务器及端口:192.168.142.155:6379 ,无密码

搭建
  • 创建一个Maven工程,工程名为SpringDataRedisDemo,工程类型为jar
  • 引入相关依赖
    pom.xml
<!-- Spring相关 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>	
	<version>4.2.4.RELEASE</version>	
</dependency>
<dependency> 
	<groupId>org.springframework</groupId>
	<artifactId>spring-beans</artifactId>	
	<version>4.2.4.RELEASE</version>		
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>	
	<version>4.2.4.RELEASE</version>		
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>		
	<version>4.2.4.RELEASE</version>	
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>	
	<version>4.2.4.RELEASE</version>		
</dependency>
<!-- redis相关 -->
<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>
  • 创建配置文件
    在这里插入图片描述
    redis-config.properties:

    redis.host=192.168.142.155
    redis.port=6379 
    redis.pass=
    redis.database=0 
    redis.maxIdle=300 
    redis.maxWait=3000 
    redis.testOnBorrow=true
    

applicationContext-redis.xml:

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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>
测试及使用
  • 创建测试类
    在这里插入图片描述
    TestValue.java:

    package 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.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
    public class TestValue {
    	@Autowired
    	private RedisTemplate redisTemplate;
    	@Test
    	public void setValue(){
    		redisTemplate.boundValueOps("myname").set("God");
    	}	
    	@Test
    	public void getValue(){
    		String str = (String) redisTemplate.boundValueOps("myname").get();
    		System.out.println(str);
    	}	
    	@Test
    	public void deleteValue(){
    		redisTemplate.delete("myname");;
    	}
    }
    
  • 测试(存一个key-value,再读取这个value)
    在这里插入图片描述
    在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值