spring-data spring 的nosql的orm框架学习

使用了spring-data-redis对于redis的orm框架的学习,整理了一下的使用文档

1.在pom.xml添加一下依赖:

		<dependencies>		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.0.0.RC1</version>
		</dependency>
	</dependencies>
	<repositories>
	<repository>
		<id>spring-milestone</id>
		<name>Spring Maven MILESTONE Repository</name>
		<url>http://maven.springframework.org/milestone</url>
	</repository></repositories>

执行mvn eclipse:eclipse引入依赖包 

 

2.在工程的classpath目录添加spring的配置文件spring-redis.xml:

<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"
  xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="jedisFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="10.20.150.206" p:port="6379"/>

  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
      p:connection-factory-ref="jedisFactory"/>
</beans>

redis服务器的地址改为对应的服务化端口和地址

 

3.编写测试类代码:

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;

public class SpringDataRedis {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-redis.xml");
		RedisTemplate<String,String> template = (RedisTemplate<String,String>)ctx.getBean("redisTemplate");
		
		BoundValueOperations<String,String> bounds=template.boundValueOps("aadasfda");
		bounds.set("aaavadfdlue");
		boolean success = bounds.persist();
		System.out.println(success);
		
		String value = bounds.get();
		System.out.println(value);
		
		
		BoundListOperations<String, String> listBounds = template.boundListOps("111");
		listBounds.rightPush("a");
		listBounds.rightPush("b");
		listBounds.rightPush("c");
		
		getListValues();
	}
	
	public static void getListValues(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-redis.xml");
		RedisTemplate<String,String> template = (RedisTemplate<String,String>)ctx.getBean("redisTemplate");
		
		BoundListOperations<String, String> listBounds = template.boundListOps("111");
		System.out.println(listBounds.size());
		System.out.println(listBounds.leftPop());
	}
}

4.执行main函数输出:

2012-5-24 15:28:53 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: startup date [Thu May 24 15:28:53 CST 2012]; root of context hierarchy
2012-5-24 15:28:53 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-redis.xml]
2012-5-24 15:28:53 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1292d26: defining beans [jedisFactory,redisTemplate]; root of factory hierarchy
false
aaavadfdlue
2012-5-24 15:28:53 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ef137d: startup date [Thu May 24 15:28:53 CST 2012]; root of context hierarchy
2012-5-24 15:28:53 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-redis.xml]
2012-5-24 15:28:53 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@cdb06e: defining beans [jedisFactory,redisTemplate]; root of factory hierarchy
3
a

有兴趣的可以再看下关于进一步源码框架相关的一篇http://zhwj184.iteye.com/blog/1539857

 

参考使用文档:

http://static.springsource.org/spring-data/redis/docs/current/reference/index.html

https://github.com/SpringSource/spring-data-redis


posted @ 2012-06-02 08:32  MXi4oyu  阅读(351)  评论(0编辑  收藏  举报