Spring-data-redis初体验

本文介绍了Spring-data-redis的初步使用,包括所需JAR包的准备,Spring配置文件的编写,以及测试代码的实现。重点讲解了如何解决远程访问Redis时遇到的权限和连接问题,并提供了项目目录结构及测试运行步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.准备Spring-data-redis需要的JAR包

学习Spring-data-redis最烦的莫过于准备工作了,我整理了最新的JAR包集合(可能有一些spring的jar包是多余了)放在如下地址,参考使用:http://pan.baidu.com/s/1hrmKTnA

         其中包含了一个Jedis-XXX.jar这个必须要加进去,这个是java对redis的接口封装

2.编写Spring配置文件

【applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    
	<bean id="jedisConnectionFactory" 
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
		p:use-pool="true" p:host-name="192.168.106.131" p:port="6379" p:password="centerm"/>

  	<bean id="stringRedisTemplate" 
  		class="org.springframework.data.redis.core.StringRedisTemplate" 
  		p:connection-factory-ref="jedisConnectionFactory"/>
    <!-- 扫描base-package下的文件,并注册bean,同时激活已注册的bean -->
    <context:component-scan base-package="com.centerm" />
</beans>

    上面这一段中的p:host-name="192.168.106.131" p:port="6379"是标明连接的主机IP和端口号,此外如果Redis服务器有设置访问密码需要加p:password="密码"

**设置主机IP地址可以看出,redis是支持远程访问的,但是直接访问还是会有错误(Connectionrefused(error) NOAUTH Authentication required),需要作如下修改:

    1.  到redis服务端安装目录下修改文件redis.conf(windows下名为redis.windows-service.conf),注释bind 127.0.0.1这一行

    2.  关闭redis服务器防火墙或对6379端口放行

    3.  重启redis服务(linux下: service redisrestart;windows下: net restart redis)

3.编写测试代码

项目目录结构如下:


1.      Redis工具类【TestRedis.java】

package com.centerm;

import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

/**
 * @author Fly
 * @ClassName TestRedis
 * @date 2016年3月1日下午1:49:14
 */
@Repository("testRedis")
public class TestRedis {
	@Autowired
	  private StringRedisTemplate redisTemplate;

	  public void addLink(String userId, String url) {
	    redisTemplate.opsForList().leftPush(userId, url);
	  }
	  
	  public void addHash(String userId,String url){
		  redisTemplate.opsForHash().put("flyHash", userId, url);
	  }
	  
	  public void addSet(String userId,String url){
		  redisTemplate.opsForSet().add(userId, url);
	  }
	  
	  public void addString(String userId,String url) {
		  redisTemplate.opsForValue().append(userId, url);
	  }
	  
	  public void addZSet(String userId,String url){
		  redisTemplate.opsForZSet().add(userId, url + "2", 2);
		  redisTemplate.opsForZSet().add(userId, url + "1", 1);
		  redisTemplate.opsForZSet().add(userId, url + "3", 3);
		  
		  Set<String> values = redisTemplate.opsForZSet().range(userId, 0, -1);
		  for(String str:values){
			  System.out.println(str);
		  }
	  }
}


2.      测试类基类【BaseTest.java】

package com.centerm;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class BaseTest
{
	
}


3.      主测试类【TestRedisTest.java】

package com.centerm;

import javax.annotation.Resource;
import org.junit.Test;
import com.centerm.BaseTest;
import com.centerm.TestRedis;

/**
 * @author Fly
 * @ClassName TestRedisTest
 * @date 2016年3月1日下午1:59:52
 */
public class TestRedisTest extends BaseTest{
	@Resource(name="testRedis")
	private TestRedis testRedis;
	
	@Test
	public void testRedisTst(){
		testRedis.addString(new String("baidu"), new String("http://www.baidu.com"));
//		testRedis.addLink(new String("baidu"), new String("http://www.baidu.com"));
//		testRedis.addSet(new String("baidu"), new String("http://www.baidu.com"));
//		testRedis.addHash(new String("baidu"), new String("http://www.baidu.com"));
//		testRedis.addZSet(new String("baidu"), new String("http://www.baidu.com"));
	}
}


【注意】applicationContext.xmltest模块也要放置一个

4.测试运行

1.      运行测试类:


2.      运行成功后到redis客户端查看结果


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱清清

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值