• springdata 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


  • 相关阅读:
    ASP.NET MVC深入浅出(被替换)
    第十七节: EF的CodeFirst模式的四种初始化策略和通过Migration进行数据的迁移
    第十六节: EF的CodeFirst模式通过Fluent API修改默认协定
    第十五节: EF的CodeFirst模式通过DataAnnotations修改默认协定
    第十四节: EF的三种模式(四) 之 原生正宗的 CodeFirst模式的默认约定
    第十三节: EF的三种模式(三) 之 来自数据库的CodeFirst模式
    C#读写记事本(txt)文件
    JSP生成验证码
    SQLServer创建用户、数据库、表、约束、存储过程、视图
    windows7 asp.net发布IIS 拒绝访问 解决方法
  • 原文地址:https://www.cnblogs.com/secbook/p/2655169.html
Copyright © 2020-2023  润新知